test_stan.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.web._stan} portion of the L{twisted.web.template}
  5. implementation.
  6. """
  7. from __future__ import absolute_import, division
  8. from twisted.web.template import Comment, CDATA, CharRef, Tag
  9. from twisted.trial.unittest import TestCase
  10. from twisted.python.compat import _PY3
  11. def proto(*a, **kw):
  12. """
  13. Produce a new tag for testing.
  14. """
  15. return Tag('hello')(*a, **kw)
  16. class TagTests(TestCase):
  17. """
  18. Tests for L{Tag}.
  19. """
  20. def test_fillSlots(self):
  21. """
  22. L{Tag.fillSlots} returns self.
  23. """
  24. tag = proto()
  25. self.assertIdentical(tag, tag.fillSlots(test='test'))
  26. def test_cloneShallow(self):
  27. """
  28. L{Tag.clone} copies all attributes and children of a tag, including its
  29. render attribute. If the shallow flag is C{False}, that's where it
  30. stops.
  31. """
  32. innerList = ["inner list"]
  33. tag = proto("How are you", innerList,
  34. hello="world", render="aSampleMethod")
  35. tag.fillSlots(foo='bar')
  36. tag.filename = "foo/bar"
  37. tag.lineNumber = 6
  38. tag.columnNumber = 12
  39. clone = tag.clone(deep=False)
  40. self.assertEqual(clone.attributes['hello'], 'world')
  41. self.assertNotIdentical(clone.attributes, tag.attributes)
  42. self.assertEqual(clone.children, ["How are you", innerList])
  43. self.assertNotIdentical(clone.children, tag.children)
  44. self.assertIdentical(clone.children[1], innerList)
  45. self.assertEqual(tag.slotData, clone.slotData)
  46. self.assertNotIdentical(tag.slotData, clone.slotData)
  47. self.assertEqual(clone.filename, "foo/bar")
  48. self.assertEqual(clone.lineNumber, 6)
  49. self.assertEqual(clone.columnNumber, 12)
  50. self.assertEqual(clone.render, "aSampleMethod")
  51. def test_cloneDeep(self):
  52. """
  53. L{Tag.clone} copies all attributes and children of a tag, including its
  54. render attribute. In its normal operating mode (where the deep flag is
  55. C{True}, as is the default), it will clone all sub-lists and sub-tags.
  56. """
  57. innerTag = proto("inner")
  58. innerList = ["inner list"]
  59. tag = proto("How are you", innerTag, innerList,
  60. hello="world", render="aSampleMethod")
  61. tag.fillSlots(foo='bar')
  62. tag.filename = "foo/bar"
  63. tag.lineNumber = 6
  64. tag.columnNumber = 12
  65. clone = tag.clone()
  66. self.assertEqual(clone.attributes['hello'], 'world')
  67. self.assertNotIdentical(clone.attributes, tag.attributes)
  68. self.assertNotIdentical(clone.children, tag.children)
  69. # sanity check
  70. self.assertIdentical(tag.children[1], innerTag)
  71. # clone should have sub-clone
  72. self.assertNotIdentical(clone.children[1], innerTag)
  73. # sanity check
  74. self.assertIdentical(tag.children[2], innerList)
  75. # clone should have sub-clone
  76. self.assertNotIdentical(clone.children[2], innerList)
  77. self.assertEqual(tag.slotData, clone.slotData)
  78. self.assertNotIdentical(tag.slotData, clone.slotData)
  79. self.assertEqual(clone.filename, "foo/bar")
  80. self.assertEqual(clone.lineNumber, 6)
  81. self.assertEqual(clone.columnNumber, 12)
  82. self.assertEqual(clone.render, "aSampleMethod")
  83. def test_clear(self):
  84. """
  85. L{Tag.clear} removes all children from a tag, but leaves its attributes
  86. in place.
  87. """
  88. tag = proto("these are", "children", "cool", andSoIs='this-attribute')
  89. tag.clear()
  90. self.assertEqual(tag.children, [])
  91. self.assertEqual(tag.attributes, {'andSoIs': 'this-attribute'})
  92. def test_suffix(self):
  93. """
  94. L{Tag.__call__} accepts Python keywords with a suffixed underscore as
  95. the DOM attribute of that literal suffix.
  96. """
  97. proto = Tag('div')
  98. tag = proto()
  99. tag(class_='a')
  100. self.assertEqual(tag.attributes, {'class': 'a'})
  101. def test_commentReprPy2(self):
  102. """
  103. L{Comment.__repr__} returns a value which makes it easy to see what's
  104. in the comment.
  105. """
  106. self.assertEqual(repr(Comment(u"hello there")),
  107. "Comment(u'hello there')")
  108. def test_cdataReprPy2(self):
  109. """
  110. L{CDATA.__repr__} returns a value which makes it easy to see what's in
  111. the comment.
  112. """
  113. self.assertEqual(repr(CDATA(u"test data")),
  114. "CDATA(u'test data')")
  115. def test_commentReprPy3(self):
  116. """
  117. L{Comment.__repr__} returns a value which makes it easy to see what's
  118. in the comment.
  119. """
  120. self.assertEqual(repr(Comment(u"hello there")),
  121. "Comment('hello there')")
  122. def test_cdataReprPy3(self):
  123. """
  124. L{CDATA.__repr__} returns a value which makes it easy to see what's in
  125. the comment.
  126. """
  127. self.assertEqual(repr(CDATA(u"test data")),
  128. "CDATA('test data')")
  129. if not _PY3:
  130. test_commentReprPy3.skip = "Only relevant on Python 3."
  131. test_cdataReprPy3.skip = "Only relevant on Python 3."
  132. else:
  133. test_commentReprPy2.skip = "Only relevant on Python 2."
  134. test_cdataReprPy2.skip = "Only relevant on Python 2."
  135. def test_charrefRepr(self):
  136. """
  137. L{CharRef.__repr__} returns a value which makes it easy to see what
  138. character is referred to.
  139. """
  140. snowman = ord(u"\N{SNOWMAN}")
  141. self.assertEqual(repr(CharRef(snowman)), "CharRef(9731)")