Imported Upstream version 12.1.0
[contrib/python-twisted.git] / twisted / web / test / test_stan.py
1
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5 """
6 Tests for L{twisted.web._stan} portion of the L{twisted.web.template}
7 implementation.
8 """
9
10 from twisted.web.template import Comment, CDATA, CharRef, Tag
11 from twisted.trial.unittest import TestCase
12
13 def proto(*a, **kw):
14     """
15     Produce a new tag for testing.
16     """
17     return Tag('hello')(*a, **kw)
18
19
20 class TestTag(TestCase):
21     """
22     Tests for L{Tag}.
23     """
24     def test_fillSlots(self):
25         """
26         L{Tag.fillSlots} returns self.
27         """
28         tag = proto()
29         self.assertIdentical(tag, tag.fillSlots(test='test'))
30
31
32     def test_cloneShallow(self):
33         """
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
36         stops.
37         """
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"
43         tag.lineNumber = 6
44         tag.columnNumber = 12
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")
57
58
59     def test_cloneDeep(self):
60         """
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.
64         """
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"
71         tag.lineNumber = 6
72         tag.columnNumber = 12
73         clone = tag.clone()
74         self.assertEqual(clone.attributes['hello'], 'world')
75         self.assertNotIdentical(clone.attributes, tag.attributes)
76         self.assertNotIdentical(clone.children, tag.children)
77         # sanity check
78         self.assertIdentical(tag.children[1], innerTag)
79         # clone should have sub-clone
80         self.assertNotIdentical(clone.children[1], innerTag)
81         # sanity check
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")
91
92
93     def test_clear(self):
94         """
95         L{Tag.clear} removes all children from a tag, but leaves its attributes
96         in place.
97         """
98         tag = proto("these are", "children", "cool", andSoIs='this-attribute')
99         tag.clear()
100         self.assertEqual(tag.children, [])
101         self.assertEqual(tag.attributes, {'andSoIs': 'this-attribute'})
102
103
104     def test_suffix(self):
105         """
106         L{Tag.__call__} accepts Python keywords with a suffixed underscore as
107         the DOM attribute of that literal suffix.
108         """
109         proto = Tag('div')
110         tag = proto()
111         tag(class_='a')
112         self.assertEqual(tag.attributes, {'class': 'a'})
113
114
115     def test_commentRepr(self):
116         """
117         L{Comment.__repr__} returns a value which makes it easy to see what's in
118         the comment.
119         """
120         self.assertEqual(repr(Comment(u"hello there")),
121                           "Comment(u'hello there')")
122
123
124     def test_cdataRepr(self):
125         """
126         L{CDATA.__repr__} returns a value which makes it easy to see what's in
127         the comment.
128         """
129         self.assertEqual(repr(CDATA(u"test data")),
130                           "CDATA(u'test data')")
131
132
133     def test_charrefRepr(self):
134         """
135         L{CharRef.__repr__} returns a value which makes it easy to see what
136         character is referred to.
137         """
138         snowman = ord(u"\N{SNOWMAN}")
139         self.assertEqual(repr(CharRef(snowman)), "CharRef(9731)")