2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
6 Tests for L{twisted.web._stan} portion of the L{twisted.web.template}
10 from twisted.web.template import Comment, CDATA, CharRef, Tag
11 from twisted.trial.unittest import TestCase
15 Produce a new tag for testing.
17 return Tag('hello')(*a, **kw)
20 class TestTag(TestCase):
24 def test_fillSlots(self):
26 L{Tag.fillSlots} returns self.
29 self.assertIdentical(tag, tag.fillSlots(test='test'))
32 def test_cloneShallow(self):
34 L{Tag.clone} copies all attributes and children of a tag, including its
35 render attribute. If the shallow flag is C{False}, that's where it
38 innerList = ["inner list"]
39 tag = proto("How are you", innerList,
40 hello="world", render="aSampleMethod")
41 tag.fillSlots(foo='bar')
42 tag.filename = "foo/bar"
45 clone = tag.clone(deep=False)
46 self.assertEqual(clone.attributes['hello'], 'world')
47 self.assertNotIdentical(clone.attributes, tag.attributes)
48 self.assertEqual(clone.children, ["How are you", innerList])
49 self.assertNotIdentical(clone.children, tag.children)
50 self.assertIdentical(clone.children[1], innerList)
51 self.assertEqual(tag.slotData, clone.slotData)
52 self.assertNotIdentical(tag.slotData, clone.slotData)
53 self.assertEqual(clone.filename, "foo/bar")
54 self.assertEqual(clone.lineNumber, 6)
55 self.assertEqual(clone.columnNumber, 12)
56 self.assertEqual(clone.render, "aSampleMethod")
59 def test_cloneDeep(self):
61 L{Tag.clone} copies all attributes and children of a tag, including its
62 render attribute. In its normal operating mode (where the deep flag is
63 C{True}, as is the default), it will clone all sub-lists and sub-tags.
65 innerTag = proto("inner")
66 innerList = ["inner list"]
67 tag = proto("How are you", innerTag, innerList,
68 hello="world", render="aSampleMethod")
69 tag.fillSlots(foo='bar')
70 tag.filename = "foo/bar"
74 self.assertEqual(clone.attributes['hello'], 'world')
75 self.assertNotIdentical(clone.attributes, tag.attributes)
76 self.assertNotIdentical(clone.children, tag.children)
78 self.assertIdentical(tag.children[1], innerTag)
79 # clone should have sub-clone
80 self.assertNotIdentical(clone.children[1], innerTag)
82 self.assertIdentical(tag.children[2], innerList)
83 # clone should have sub-clone
84 self.assertNotIdentical(clone.children[2], innerList)
85 self.assertEqual(tag.slotData, clone.slotData)
86 self.assertNotIdentical(tag.slotData, clone.slotData)
87 self.assertEqual(clone.filename, "foo/bar")
88 self.assertEqual(clone.lineNumber, 6)
89 self.assertEqual(clone.columnNumber, 12)
90 self.assertEqual(clone.render, "aSampleMethod")
95 L{Tag.clear} removes all children from a tag, but leaves its attributes
98 tag = proto("these are", "children", "cool", andSoIs='this-attribute')
100 self.assertEqual(tag.children, [])
101 self.assertEqual(tag.attributes, {'andSoIs': 'this-attribute'})
104 def test_suffix(self):
106 L{Tag.__call__} accepts Python keywords with a suffixed underscore as
107 the DOM attribute of that literal suffix.
112 self.assertEqual(tag.attributes, {'class': 'a'})
115 def test_commentRepr(self):
117 L{Comment.__repr__} returns a value which makes it easy to see what's in
120 self.assertEqual(repr(Comment(u"hello there")),
121 "Comment(u'hello there')")
124 def test_cdataRepr(self):
126 L{CDATA.__repr__} returns a value which makes it easy to see what's in
129 self.assertEqual(repr(CDATA(u"test data")),
130 "CDATA(u'test data')")
133 def test_charrefRepr(self):
135 L{CharRef.__repr__} returns a value which makes it easy to see what
136 character is referred to.
138 snowman = ord(u"\N{SNOWMAN}")
139 self.assertEqual(repr(CharRef(snowman)), "CharRef(9731)")