test_templite.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. # -*- coding: utf8 -*-
  2. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  3. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  4. """Tests for coverage.templite."""
  5. import re
  6. from coverage.templite import Templite, TempliteSyntaxError, TempliteValueError
  7. from tests.coveragetest import CoverageTest
  8. # pylint: disable=unused-variable
  9. class AnyOldObject(object):
  10. """Simple testing object.
  11. Use keyword arguments in the constructor to set attributes on the object.
  12. """
  13. def __init__(self, **attrs):
  14. for n, v in attrs.items():
  15. setattr(self, n, v)
  16. class TempliteTest(CoverageTest):
  17. """Tests for Templite."""
  18. run_in_temp_dir = False
  19. def try_render(self, text, ctx=None, result=None):
  20. """Render `text` through `ctx`, and it had better be `result`.
  21. Result defaults to None so we can shorten the calls where we expect
  22. an exception and never get to the result comparison.
  23. """
  24. actual = Templite(text).render(ctx or {})
  25. # If result is None, then an exception should have prevented us getting
  26. # to here.
  27. assert result is not None
  28. self.assertEqual(actual, result)
  29. def assertSynErr(self, msg):
  30. """Assert that a `TempliteSyntaxError` will happen.
  31. A context manager, and the message should be `msg`.
  32. """
  33. pat = "^" + re.escape(msg) + "$"
  34. return self.assertRaisesRegex(TempliteSyntaxError, pat)
  35. def test_passthrough(self):
  36. # Strings without variables are passed through unchanged.
  37. self.assertEqual(Templite("Hello").render(), "Hello")
  38. self.assertEqual(
  39. Templite("Hello, 20% fun time!").render(),
  40. "Hello, 20% fun time!"
  41. )
  42. def test_variables(self):
  43. # Variables use {{var}} syntax.
  44. self.try_render("Hello, {{name}}!", {'name':'Ned'}, "Hello, Ned!")
  45. def test_undefined_variables(self):
  46. # Using undefined names is an error.
  47. with self.assertRaises(Exception):
  48. self.try_render("Hi, {{name}}!")
  49. def test_pipes(self):
  50. # Variables can be filtered with pipes.
  51. data = {
  52. 'name': 'Ned',
  53. 'upper': lambda x: x.upper(),
  54. 'second': lambda x: x[1],
  55. }
  56. self.try_render("Hello, {{name|upper}}!", data, "Hello, NED!")
  57. # Pipes can be concatenated.
  58. self.try_render("Hello, {{name|upper|second}}!", data, "Hello, E!")
  59. def test_reusability(self):
  60. # A single Templite can be used more than once with different data.
  61. globs = {
  62. 'upper': lambda x: x.upper(),
  63. 'punct': '!',
  64. }
  65. template = Templite("This is {{name|upper}}{{punct}}", globs)
  66. self.assertEqual(template.render({'name':'Ned'}), "This is NED!")
  67. self.assertEqual(template.render({'name':'Ben'}), "This is BEN!")
  68. def test_attribute(self):
  69. # Variables' attributes can be accessed with dots.
  70. obj = AnyOldObject(a="Ay")
  71. self.try_render("{{obj.a}}", locals(), "Ay")
  72. obj2 = AnyOldObject(obj=obj, b="Bee")
  73. self.try_render("{{obj2.obj.a}} {{obj2.b}}", locals(), "Ay Bee")
  74. def test_member_function(self):
  75. # Variables' member functions can be used, as long as they are nullary.
  76. class WithMemberFns(AnyOldObject):
  77. """A class to try out member function access."""
  78. def ditto(self):
  79. """Return twice the .txt attribute."""
  80. return self.txt + self.txt
  81. obj = WithMemberFns(txt="Once")
  82. self.try_render("{{obj.ditto}}", locals(), "OnceOnce")
  83. def test_item_access(self):
  84. # Variables' items can be used.
  85. d = {'a':17, 'b':23}
  86. self.try_render("{{d.a}} < {{d.b}}", locals(), "17 < 23")
  87. def test_loops(self):
  88. # Loops work like in Django.
  89. nums = [1,2,3,4]
  90. self.try_render(
  91. "Look: {% for n in nums %}{{n}}, {% endfor %}done.",
  92. locals(),
  93. "Look: 1, 2, 3, 4, done."
  94. )
  95. # Loop iterables can be filtered.
  96. def rev(l):
  97. """Return the reverse of `l`."""
  98. l = l[:]
  99. l.reverse()
  100. return l
  101. self.try_render(
  102. "Look: {% for n in nums|rev %}{{n}}, {% endfor %}done.",
  103. locals(),
  104. "Look: 4, 3, 2, 1, done."
  105. )
  106. def test_empty_loops(self):
  107. self.try_render(
  108. "Empty: {% for n in nums %}{{n}}, {% endfor %}done.",
  109. {'nums':[]},
  110. "Empty: done."
  111. )
  112. def test_multiline_loops(self):
  113. self.try_render(
  114. "Look: \n{% for n in nums %}\n{{n}}, \n{% endfor %}done.",
  115. {'nums':[1,2,3]},
  116. "Look: \n\n1, \n\n2, \n\n3, \ndone."
  117. )
  118. def test_multiple_loops(self):
  119. self.try_render(
  120. "{% for n in nums %}{{n}}{% endfor %} and "
  121. "{% for n in nums %}{{n}}{% endfor %}",
  122. {'nums': [1,2,3]},
  123. "123 and 123"
  124. )
  125. def test_comments(self):
  126. # Single-line comments work:
  127. self.try_render(
  128. "Hello, {# Name goes here: #}{{name}}!",
  129. {'name':'Ned'}, "Hello, Ned!"
  130. )
  131. # and so do multi-line comments:
  132. self.try_render(
  133. "Hello, {# Name\ngoes\nhere: #}{{name}}!",
  134. {'name':'Ned'}, "Hello, Ned!"
  135. )
  136. def test_if(self):
  137. self.try_render(
  138. "Hi, {% if ned %}NED{% endif %}{% if ben %}BEN{% endif %}!",
  139. {'ned': 1, 'ben': 0},
  140. "Hi, NED!"
  141. )
  142. self.try_render(
  143. "Hi, {% if ned %}NED{% endif %}{% if ben %}BEN{% endif %}!",
  144. {'ned': 0, 'ben': 1},
  145. "Hi, BEN!"
  146. )
  147. self.try_render(
  148. "Hi, {% if ned %}NED{% if ben %}BEN{% endif %}{% endif %}!",
  149. {'ned': 0, 'ben': 0},
  150. "Hi, !"
  151. )
  152. self.try_render(
  153. "Hi, {% if ned %}NED{% if ben %}BEN{% endif %}{% endif %}!",
  154. {'ned': 1, 'ben': 0},
  155. "Hi, NED!"
  156. )
  157. self.try_render(
  158. "Hi, {% if ned %}NED{% if ben %}BEN{% endif %}{% endif %}!",
  159. {'ned': 1, 'ben': 1},
  160. "Hi, NEDBEN!"
  161. )
  162. def test_complex_if(self):
  163. class Complex(AnyOldObject):
  164. """A class to try out complex data access."""
  165. def getit(self):
  166. """Return it."""
  167. return self.it
  168. obj = Complex(it={'x':"Hello", 'y': 0})
  169. self.try_render(
  170. "@"
  171. "{% if obj.getit.x %}X{% endif %}"
  172. "{% if obj.getit.y %}Y{% endif %}"
  173. "{% if obj.getit.y|str %}S{% endif %}"
  174. "!",
  175. { 'obj': obj, 'str': str },
  176. "@XS!"
  177. )
  178. def test_loop_if(self):
  179. self.try_render(
  180. "@{% for n in nums %}{% if n %}Z{% endif %}{{n}}{% endfor %}!",
  181. {'nums': [0,1,2]},
  182. "@0Z1Z2!"
  183. )
  184. self.try_render(
  185. "X{%if nums%}@{% for n in nums %}{{n}}{% endfor %}{%endif%}!",
  186. {'nums': [0,1,2]},
  187. "X@012!"
  188. )
  189. self.try_render(
  190. "X{%if nums%}@{% for n in nums %}{{n}}{% endfor %}{%endif%}!",
  191. {'nums': []},
  192. "X!"
  193. )
  194. def test_nested_loops(self):
  195. self.try_render(
  196. "@"
  197. "{% for n in nums %}"
  198. "{% for a in abc %}{{a}}{{n}}{% endfor %}"
  199. "{% endfor %}"
  200. "!",
  201. {'nums': [0,1,2], 'abc': ['a', 'b', 'c']},
  202. "@a0b0c0a1b1c1a2b2c2!"
  203. )
  204. def test_whitespace_handling(self):
  205. self.try_render(
  206. "@{% for n in nums %}\n"
  207. " {% for a in abc %}{{a}}{{n}}{% endfor %}\n"
  208. "{% endfor %}!\n",
  209. {'nums': [0, 1, 2], 'abc': ['a', 'b', 'c']},
  210. "@\n a0b0c0\n\n a1b1c1\n\n a2b2c2\n!\n"
  211. )
  212. self.try_render(
  213. "@{% for n in nums -%}\n"
  214. " {% for a in abc -%}\n"
  215. " {# this disappears completely -#}\n"
  216. " {{a -}}\n"
  217. " {{n -}}\n"
  218. " {% endfor %}\n"
  219. "{% endfor %}!\n",
  220. {'nums': [0, 1, 2], 'abc': ['a', 'b', 'c']},
  221. "@a0b0c0\na1b1c1\na2b2c2\n!\n"
  222. )
  223. def test_non_ascii(self):
  224. self.try_render(
  225. u"{{where}} ollǝɥ",
  226. { 'where': u'ǝɹǝɥʇ' },
  227. u"ǝɹǝɥʇ ollǝɥ"
  228. )
  229. def test_exception_during_evaluation(self):
  230. # TypeError: Couldn't evaluate {{ foo.bar.baz }}:
  231. msg = "Couldn't evaluate None.bar"
  232. with self.assertRaisesRegex(TempliteValueError, msg):
  233. self.try_render(
  234. "Hey {{foo.bar.baz}} there", {'foo': None}, "Hey ??? there"
  235. )
  236. def test_bad_names(self):
  237. with self.assertSynErr("Not a valid name: 'var%&!@'"):
  238. self.try_render("Wat: {{ var%&!@ }}")
  239. with self.assertSynErr("Not a valid name: 'filter%&!@'"):
  240. self.try_render("Wat: {{ foo|filter%&!@ }}")
  241. with self.assertSynErr("Not a valid name: '@'"):
  242. self.try_render("Wat: {% for @ in x %}{% endfor %}")
  243. def test_bogus_tag_syntax(self):
  244. with self.assertSynErr("Don't understand tag: 'bogus'"):
  245. self.try_render("Huh: {% bogus %}!!{% endbogus %}??")
  246. def test_malformed_if(self):
  247. with self.assertSynErr("Don't understand if: '{% if %}'"):
  248. self.try_render("Buh? {% if %}hi!{% endif %}")
  249. with self.assertSynErr("Don't understand if: '{% if this or that %}'"):
  250. self.try_render("Buh? {% if this or that %}hi!{% endif %}")
  251. def test_malformed_for(self):
  252. with self.assertSynErr("Don't understand for: '{% for %}'"):
  253. self.try_render("Weird: {% for %}loop{% endfor %}")
  254. with self.assertSynErr("Don't understand for: '{% for x from y %}'"):
  255. self.try_render("Weird: {% for x from y %}loop{% endfor %}")
  256. with self.assertSynErr("Don't understand for: '{% for x, y in z %}'"):
  257. self.try_render("Weird: {% for x, y in z %}loop{% endfor %}")
  258. def test_bad_nesting(self):
  259. with self.assertSynErr("Unmatched action tag: 'if'"):
  260. self.try_render("{% if x %}X")
  261. with self.assertSynErr("Mismatched end tag: 'for'"):
  262. self.try_render("{% if x %}X{% endfor %}")
  263. with self.assertSynErr("Too many ends: '{% endif %}'"):
  264. self.try_render("{% if x %}{% endif %}{% endif %}")
  265. def test_malformed_end(self):
  266. with self.assertSynErr("Don't understand end: '{% end if %}'"):
  267. self.try_render("{% if x %}X{% end if %}")
  268. with self.assertSynErr("Don't understand end: '{% endif now %}'"):
  269. self.try_render("{% if x %}X{% endif now %}")