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")
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, "")
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>")
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")
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")
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"])
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")
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 — how are you?\n")
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>")
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>")