PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/test/test_pep292.py

https://bitbucket.org/glix/python
Python | 194 lines | 175 code | 16 blank | 3 comment | 3 complexity | c8de0771805701f31464f813d83c27cc MD5 | raw file
  1. # Copyright (C) 2004 Python Software Foundation
  2. # Author: barry@python.org (Barry Warsaw)
  3. # License: http://www.opensource.org/licenses/PythonSoftFoundation.php
  4. import unittest
  5. from string import Template
  6. class Bag:
  7. pass
  8. class Mapping:
  9. def __getitem__(self, name):
  10. obj = self
  11. for part in name.split('.'):
  12. try:
  13. obj = getattr(obj, part)
  14. except AttributeError:
  15. raise KeyError(name)
  16. return obj
  17. class TestTemplate(unittest.TestCase):
  18. def test_regular_templates(self):
  19. s = Template('$who likes to eat a bag of $what worth $$100')
  20. self.assertEqual(s.substitute(dict(who='tim', what='ham')),
  21. 'tim likes to eat a bag of ham worth $100')
  22. self.assertRaises(KeyError, s.substitute, dict(who='tim'))
  23. def test_regular_templates_with_braces(self):
  24. s = Template('$who likes ${what} for ${meal}')
  25. d = dict(who='tim', what='ham', meal='dinner')
  26. self.assertEqual(s.substitute(d), 'tim likes ham for dinner')
  27. self.assertRaises(KeyError, s.substitute,
  28. dict(who='tim', what='ham'))
  29. def test_escapes(self):
  30. eq = self.assertEqual
  31. s = Template('$who likes to eat a bag of $$what worth $$100')
  32. eq(s.substitute(dict(who='tim', what='ham')),
  33. 'tim likes to eat a bag of $what worth $100')
  34. s = Template('$who likes $$')
  35. eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $')
  36. def test_percents(self):
  37. eq = self.assertEqual
  38. s = Template('%(foo)s $foo ${foo}')
  39. d = dict(foo='baz')
  40. eq(s.substitute(d), '%(foo)s baz baz')
  41. eq(s.safe_substitute(d), '%(foo)s baz baz')
  42. def test_stringification(self):
  43. eq = self.assertEqual
  44. s = Template('tim has eaten $count bags of ham today')
  45. d = dict(count=7)
  46. eq(s.substitute(d), 'tim has eaten 7 bags of ham today')
  47. eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today')
  48. s = Template('tim has eaten ${count} bags of ham today')
  49. eq(s.substitute(d), 'tim has eaten 7 bags of ham today')
  50. def test_tupleargs(self):
  51. eq = self.assertEqual
  52. s = Template('$who ate ${meal}')
  53. d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))
  54. eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
  55. eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
  56. def test_SafeTemplate(self):
  57. eq = self.assertEqual
  58. s = Template('$who likes ${what} for ${meal}')
  59. eq(s.safe_substitute(dict(who='tim')), 'tim likes ${what} for ${meal}')
  60. eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}')
  61. eq(s.safe_substitute(dict(what='ham', meal='dinner')),
  62. '$who likes ham for dinner')
  63. eq(s.safe_substitute(dict(who='tim', what='ham')),
  64. 'tim likes ham for ${meal}')
  65. eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),
  66. 'tim likes ham for dinner')
  67. def test_invalid_placeholders(self):
  68. raises = self.assertRaises
  69. s = Template('$who likes $')
  70. raises(ValueError, s.substitute, dict(who='tim'))
  71. s = Template('$who likes ${what)')
  72. raises(ValueError, s.substitute, dict(who='tim'))
  73. s = Template('$who likes $100')
  74. raises(ValueError, s.substitute, dict(who='tim'))
  75. def test_delimiter_override(self):
  76. class PieDelims(Template):
  77. delimiter = '@'
  78. s = PieDelims('@who likes to eat a bag of @{what} worth $100')
  79. self.assertEqual(s.substitute(dict(who='tim', what='ham')),
  80. 'tim likes to eat a bag of ham worth $100')
  81. def test_idpattern_override(self):
  82. class PathPattern(Template):
  83. idpattern = r'[_a-z][._a-z0-9]*'
  84. m = Mapping()
  85. m.bag = Bag()
  86. m.bag.foo = Bag()
  87. m.bag.foo.who = 'tim'
  88. m.bag.what = 'ham'
  89. s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')
  90. self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
  91. def test_pattern_override(self):
  92. class MyPattern(Template):
  93. pattern = r"""
  94. (?P<escaped>@{2}) |
  95. @(?P<named>[_a-z][._a-z0-9]*) |
  96. @{(?P<braced>[_a-z][._a-z0-9]*)} |
  97. (?P<invalid>@)
  98. """
  99. m = Mapping()
  100. m.bag = Bag()
  101. m.bag.foo = Bag()
  102. m.bag.foo.who = 'tim'
  103. m.bag.what = 'ham'
  104. s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')
  105. self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
  106. class BadPattern(Template):
  107. pattern = r"""
  108. (?P<badname>.*) |
  109. (?P<escaped>@{2}) |
  110. @(?P<named>[_a-z][._a-z0-9]*) |
  111. @{(?P<braced>[_a-z][._a-z0-9]*)} |
  112. (?P<invalid>@) |
  113. """
  114. s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')
  115. self.assertRaises(ValueError, s.substitute, {})
  116. self.assertRaises(ValueError, s.safe_substitute, {})
  117. def test_unicode_values(self):
  118. s = Template('$who likes $what')
  119. d = dict(who=u't\xffm', what=u'f\xfe\fed')
  120. self.assertEqual(s.substitute(d), u't\xffm likes f\xfe\x0ced')
  121. def test_keyword_arguments(self):
  122. eq = self.assertEqual
  123. s = Template('$who likes $what')
  124. eq(s.substitute(who='tim', what='ham'), 'tim likes ham')
  125. eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')
  126. eq(s.substitute(dict(who='fred', what='kung pao'),
  127. who='tim', what='ham'),
  128. 'tim likes ham')
  129. s = Template('the mapping is $mapping')
  130. eq(s.substitute(dict(foo='none'), mapping='bozo'),
  131. 'the mapping is bozo')
  132. eq(s.substitute(dict(mapping='one'), mapping='two'),
  133. 'the mapping is two')
  134. def test_keyword_arguments_safe(self):
  135. eq = self.assertEqual
  136. raises = self.assertRaises
  137. s = Template('$who likes $what')
  138. eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')
  139. eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')
  140. eq(s.safe_substitute(dict(who='fred', what='kung pao'),
  141. who='tim', what='ham'),
  142. 'tim likes ham')
  143. s = Template('the mapping is $mapping')
  144. eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),
  145. 'the mapping is bozo')
  146. eq(s.safe_substitute(dict(mapping='one'), mapping='two'),
  147. 'the mapping is two')
  148. d = dict(mapping='one')
  149. raises(TypeError, s.substitute, d, {})
  150. raises(TypeError, s.safe_substitute, d, {})
  151. def test_delimiter_override(self):
  152. eq = self.assertEqual
  153. raises = self.assertRaises
  154. class AmpersandTemplate(Template):
  155. delimiter = '&'
  156. s = AmpersandTemplate('this &gift is for &{who} &&')
  157. eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')
  158. raises(KeyError, s.substitute)
  159. eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')
  160. eq(s.safe_substitute(), 'this &gift is for &{who} &')
  161. s = AmpersandTemplate('this &gift is for &{who} &')
  162. raises(ValueError, s.substitute, dict(gift='bud', who='you'))
  163. eq(s.safe_substitute(), 'this &gift is for &{who} &')
  164. def test_main():
  165. from test import test_support
  166. test_classes = [TestTemplate,]
  167. test_support.run_unittest(*test_classes)
  168. if __name__ == '__main__':
  169. test_main()