예제 #1
0
    def test_raise_extension(self):
        node = Node(mock.Mock(), None, meta={"templates": []})
        node.is_leaf = False
        with self.assertRaises(TemplateRuntimeError) as excp:
            jinja_filter(node, ERROR_TEMPLATE)

        self.assertEqual(excp.exception.message, "This is an error")
예제 #2
0
    def test_context(self):
        path = mock.Mock()
        path.name = "thisfile.html"
        node = Node(path, None, meta={"templates": []})
        node.is_leaf = False
        result = jinja_filter(node, "{{ node }}")
        self.assertEqual(result, "<Node: thisfile.html>")

        result = jinja_filter(node, "{{ undef_value }}")
        self.assertEqual(result, "")
예제 #3
0
    def test_default_block(self):
        template_name = "bob.j2"
        with TemporaryDirectory() as tmp_dir:
            with pathlib.Path(tmp_dir, template_name).open("w") as tmpl:
                tmpl.write(BASE_TEMPLATE)

            node = Node(mock.Mock(), None, meta={"templates": [tmp_dir], "extends": "bob.j2"})
            node.is_leaf = False
            result = jinja_filter(node, PLAIN_TEMPLATE)
            self.assertEqual(result, "<p>Title</p>\n")

            node = Node(mock.Mock(), None, meta={"templates": [tmp_dir],
                                                 "extends": "bob.j2", "default_block": "content"})
            node.is_leaf = False
            result = jinja_filter(node, PLAIN_TEMPLATE)
            self.assertEqual(result, "<p>Title</p>\n\n<p>0</p><p>1</p><p>2</p>")
예제 #4
0
    def test_metaselect(self):
        node = Node(mock.Mock(),  None, meta={"templates": []})
        node._content_start = 0
        for i in [True, True, False, True, None]:
            new_node = Node(mock.Mock(), node, meta={"bob": i})
            new_node._content_start = 0
        result = jinja_filter(node, METASELECT_TEMPLATE)

        self.assertEqual(result, "TrueTrueTrue")
예제 #5
0
    def test_metasort(self):
        node = Node(mock.Mock(), None, meta={"templates": []})
        node._content_start = 0
        for i in [3, 5, 27, 2, 1]:
            new_node = Node(mock.Mock(), node, meta={"bob": i})
            new_node._content_start = 0
        result = jinja_filter(node, METASORT_TEMPLATE)

        self.assertEqual(result, "123527")
예제 #6
0
    def test_metareject(self):
        node = Node(mock.Mock(),  None, meta={"templates": []})
        node._content_start = 0
        for i in [True, True, False, True, None]:
            new_node = Node(mock.Mock(), node, meta={"bob": i})
            new_node._content_start = 0
        result = jinja_filter(node, METAREJECT_TEMPLATE)

        # dict key order isn't stable, but it'll be one of these
        self.assertIn(result, ["FalseNone", "NoneFalse"])
예제 #7
0
    def test_extends_with_multiple_template_dirs(self):
        template_name = "bob.j2"
        with TemporaryDirectory() as tmp1_dir, TemporaryDirectory() as tmp2_dir:
            with pathlib.Path(tmp1_dir, template_name).open("w") as tmpl:
                tmpl.write(BASE_TEMPLATE)

            with pathlib.Path(tmp2_dir, template_name).open("w") as tmpl:
                tmpl.write("empty")

            node = Node(mock.Mock(), None,
                        meta={"templates": [tmp1_dir, tmp2_dir], "extends": "bob.j2"})
            node.is_leaf = False
            result = jinja_filter(node, CONTENT_BLOCK)
            self.assertEqual(result, "<p>Title</p>\n<p>0</p><p>1</p><p>2</p>")

            # switch order of template dirs,
            node = Node(mock.Mock(), None,
                        meta={"templates": [tmp2_dir, tmp1_dir], "extends": "bob.j2"})
            node.is_leaf = False
            result = jinja_filter(node, CONTENT_BLOCK)
            self.assertEqual(result, "empty")
예제 #8
0
 def test_typogrify_filter(self):
     node = Node(mock.Mock(), None, meta={"templates": []})
     node.is_leaf = False
     result = jinja_filter(node, TYPOG_TEMPLATE)
     self.assertEqual(result, "\nHello &#8212; how are&nbsp;you?\n")
예제 #9
0
 def test_markdown_filter(self):
     node = Node(mock.Mock(), None, meta={"templates": []})
     node.is_leaf = False
     result = jinja_filter(node, MD_TEMPLATE)
     self.assertEqual(result, "<h2>Hello</h2>\n<p>This is <em>text</em></p>")
예제 #10
0
 def test_template(self):
     node = Node(mock.Mock(), None, meta={"templates": []})
     node.is_leaf = False
     result = jinja_filter(node, PLAIN_TEMPLATE)
     self.assertEqual(result, "<p>0</p><p>1</p><p>2</p>")