Permalink
Browse files

Rounded out the template tag implementation.

Added some more tests and fixed a problem revealed in the process when
moving up more than one level at a time.
  • Loading branch information...
1 parent 55094ba commit 960f5d524dea9550902d082b2d46db2650eead57 Malcolm Tredinnick committed Jun 7, 2010
Showing with 59 additions and 10 deletions.
  1. +5 −2 acacia/templatetags/acacia.py
  2. +54 −8 acacia/tests/test_templatetags.py
@@ -33,8 +33,11 @@ def render(self, context):
pieces.append(u"<ul>\n<li>%s" % escape(node.name))
current_level += 1
else:
- pieces.append(u"</li></ul></li>\n<li>%s" % escape(node.name))
- current_level -= 1
+ while diff:
+ pieces.append(u"</li></ul>")
+ diff += 1
+ current_level -= 1
+ pieces.append(u"</li>\n<li>%s" % escape(node.name))
if len(pieces) == 1:
# No content in the tree means no output.
return u""
@@ -57,6 +57,21 @@ def test_empty_tree(self):
"""
self.failIf(convert("{% load acacia %}{% treetrunk acacia.Topic %}"))
+ def test_html_sensitive_content(self):
+ setup_from_node_strings([">/&amp;/<"])
+ output = convert("{% load acacia %}{% treetrunk acacia.Topic 5 %}")
+ output = output.replace("\n", "")
+ expected = """
+ <ul>
+ <li>&gt;
+ <ul><li>&amp;amp;
+ <ul><li>&lt;</li></ul>
+ </li></ul>
+ </li></ul>""".replace("\n", "").replace(" ", "")
+ self.failUnlessEqual(output, expected, "\nGot %s\n\nExpected %s" %
+ (output, expected))
+
+
class TreeTrunkSingleRootTests(test.TestCase):
def setUp(self):
# pylint: disable-msg=C0103
@@ -164,13 +179,44 @@ def setUp(self):
]
setup_from_node_strings(nodes)
-# TODO: Tests
-# - multiple roots in tree; trees of different depths
-# - default level value
-# - level value less than depth of tree
-# - level value more than depth of tree
-# - level value equal to depth of tree
-# - Tag with unicode name containing HTML special characters (e.g. < and
-# &amp;)
+ def test_one_level(self):
+ output = convert("{% load acacia %}{% treetrunk acacia.Topic 1 %}")
+ output = output.replace("\n", "")
+ expected = """
+ <ul>
+ <li>root1</li>
+ <li>root2</li>
+ <li>root3</li>
+ </ul>""".replace("\n", "").replace(" ", "")
+ self.failUnlessEqual(output, expected, "\nGot %s\n\nExpected %s" %
+ (output, expected))
+ def test_three_levels(self):
+ output = convert("{% load acacia %}{% treetrunk acacia.Topic 3 %}")
+ output = output.replace("\n", "")
+ expected = """
+ <ul>
+ <li>root1
+ <ul><li>child1
+ <ul><li>grandchild1</li>
+ <li>grandchild2</li>
+ <li>grandchild3</li></ul></li>
+ <li>child2
+ <ul><li>grandchild1</li>
+ <li>grandchild2</li></ul></li>
+ <li>child3
+ <ul><li>grandchild1</li></ul></li>
+ <li>child4</li>
+ </ul></li>
+ <li>root2
+ <ul><li>child1
+ <ul><li>grandchild1</li></ul></li>
+ <li>child2
+ <ul><li>grandchild1</li>
+ <li>grandchild2</li></ul></li>
+ </ul></li>
+ <li>root3</li>
+ </ul>""".replace("\n", "").replace(" ", "")
+ self.failUnlessEqual(output, expected, "\nGot %s\n\nExpected %s" %
+ (output, expected))

0 comments on commit 960f5d5

Please sign in to comment.