test_if.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. from django.template import TemplateSyntaxError
  2. from django.test import SimpleTestCase
  3. from ..utils import TestObj, setup
  4. class IfTagTests(SimpleTestCase):
  5. @setup({'if-tag01': '{% if foo %}yes{% else %}no{% endif %}'})
  6. def test_if_tag01(self):
  7. output = self.engine.render_to_string('if-tag01', {'foo': True})
  8. self.assertEqual(output, 'yes')
  9. @setup({'if-tag02': '{% if foo %}yes{% else %}no{% endif %}'})
  10. def test_if_tag02(self):
  11. output = self.engine.render_to_string('if-tag02', {'foo': False})
  12. self.assertEqual(output, 'no')
  13. @setup({'if-tag03': '{% if foo %}yes{% else %}no{% endif %}'})
  14. def test_if_tag03(self):
  15. output = self.engine.render_to_string('if-tag03')
  16. self.assertEqual(output, 'no')
  17. @setup({'if-tag04': '{% if foo %}foo{% elif bar %}bar{% endif %}'})
  18. def test_if_tag04(self):
  19. output = self.engine.render_to_string('if-tag04', {'foo': True})
  20. self.assertEqual(output, 'foo')
  21. @setup({'if-tag05': '{% if foo %}foo{% elif bar %}bar{% endif %}'})
  22. def test_if_tag05(self):
  23. output = self.engine.render_to_string('if-tag05', {'bar': True})
  24. self.assertEqual(output, 'bar')
  25. @setup({'if-tag06': '{% if foo %}foo{% elif bar %}bar{% endif %}'})
  26. def test_if_tag06(self):
  27. output = self.engine.render_to_string('if-tag06')
  28. self.assertEqual(output, '')
  29. @setup({'if-tag07': '{% if foo %}foo{% elif bar %}bar{% else %}nothing{% endif %}'})
  30. def test_if_tag07(self):
  31. output = self.engine.render_to_string('if-tag07', {'foo': True})
  32. self.assertEqual(output, 'foo')
  33. @setup({'if-tag08': '{% if foo %}foo{% elif bar %}bar{% else %}nothing{% endif %}'})
  34. def test_if_tag08(self):
  35. output = self.engine.render_to_string('if-tag08', {'bar': True})
  36. self.assertEqual(output, 'bar')
  37. @setup({'if-tag09': '{% if foo %}foo{% elif bar %}bar{% else %}nothing{% endif %}'})
  38. def test_if_tag09(self):
  39. output = self.engine.render_to_string('if-tag09')
  40. self.assertEqual(output, 'nothing')
  41. @setup({'if-tag10': '{% if foo %}foo{% elif bar %}bar{% elif baz %}baz{% else %}nothing{% endif %}'})
  42. def test_if_tag10(self):
  43. output = self.engine.render_to_string('if-tag10', {'foo': True})
  44. self.assertEqual(output, 'foo')
  45. @setup({'if-tag11': '{% if foo %}foo{% elif bar %}bar{% elif baz %}baz{% else %}nothing{% endif %}'})
  46. def test_if_tag11(self):
  47. output = self.engine.render_to_string('if-tag11', {'bar': True})
  48. self.assertEqual(output, 'bar')
  49. @setup({'if-tag12': '{% if foo %}foo{% elif bar %}bar{% elif baz %}baz{% else %}nothing{% endif %}'})
  50. def test_if_tag12(self):
  51. output = self.engine.render_to_string('if-tag12', {'baz': True})
  52. self.assertEqual(output, 'baz')
  53. @setup({'if-tag13': '{% if foo %}foo{% elif bar %}bar{% elif baz %}baz{% else %}nothing{% endif %}'})
  54. def test_if_tag13(self):
  55. output = self.engine.render_to_string('if-tag13')
  56. self.assertEqual(output, 'nothing')
  57. # Filters
  58. @setup({'if-tag-filter01': '{% if foo|length == 5 %}yes{% else %}no{% endif %}'})
  59. def test_if_tag_filter01(self):
  60. output = self.engine.render_to_string('if-tag-filter01', {'foo': 'abcde'})
  61. self.assertEqual(output, 'yes')
  62. @setup({'if-tag-filter02': '{% if foo|upper == \'ABC\' %}yes{% else %}no{% endif %}'})
  63. def test_if_tag_filter02(self):
  64. output = self.engine.render_to_string('if-tag-filter02')
  65. self.assertEqual(output, 'no')
  66. # Equality
  67. @setup({'if-tag-eq01': '{% if foo == bar %}yes{% else %}no{% endif %}'})
  68. def test_if_tag_eq01(self):
  69. output = self.engine.render_to_string('if-tag-eq01')
  70. self.assertEqual(output, 'yes')
  71. @setup({'if-tag-eq02': '{% if foo == bar %}yes{% else %}no{% endif %}'})
  72. def test_if_tag_eq02(self):
  73. output = self.engine.render_to_string('if-tag-eq02', {'foo': 1})
  74. self.assertEqual(output, 'no')
  75. @setup({'if-tag-eq03': '{% if foo == bar %}yes{% else %}no{% endif %}'})
  76. def test_if_tag_eq03(self):
  77. output = self.engine.render_to_string('if-tag-eq03', {'foo': 1, 'bar': 1})
  78. self.assertEqual(output, 'yes')
  79. @setup({'if-tag-eq04': '{% if foo == bar %}yes{% else %}no{% endif %}'})
  80. def test_if_tag_eq04(self):
  81. output = self.engine.render_to_string('if-tag-eq04', {'foo': 1, 'bar': 2})
  82. self.assertEqual(output, 'no')
  83. @setup({'if-tag-eq05': '{% if foo == \'\' %}yes{% else %}no{% endif %}'})
  84. def test_if_tag_eq05(self):
  85. output = self.engine.render_to_string('if-tag-eq05')
  86. self.assertEqual(output, 'no')
  87. # Inequality
  88. @setup({'if-tag-noteq01': '{% if foo != bar %}yes{% else %}no{% endif %}'})
  89. def test_if_tag_noteq01(self):
  90. output = self.engine.render_to_string('if-tag-noteq01')
  91. self.assertEqual(output, 'no')
  92. @setup({'if-tag-noteq02': '{% if foo != bar %}yes{% else %}no{% endif %}'})
  93. def test_if_tag_noteq02(self):
  94. output = self.engine.render_to_string('if-tag-noteq02', {'foo': 1})
  95. self.assertEqual(output, 'yes')
  96. @setup({'if-tag-noteq03': '{% if foo != bar %}yes{% else %}no{% endif %}'})
  97. def test_if_tag_noteq03(self):
  98. output = self.engine.render_to_string('if-tag-noteq03', {'foo': 1, 'bar': 1})
  99. self.assertEqual(output, 'no')
  100. @setup({'if-tag-noteq04': '{% if foo != bar %}yes{% else %}no{% endif %}'})
  101. def test_if_tag_noteq04(self):
  102. output = self.engine.render_to_string('if-tag-noteq04', {'foo': 1, 'bar': 2})
  103. self.assertEqual(output, 'yes')
  104. @setup({'if-tag-noteq05': '{% if foo != "" %}yes{% else %}no{% endif %}'})
  105. def test_if_tag_noteq05(self):
  106. output = self.engine.render_to_string('if-tag-noteq05')
  107. self.assertEqual(output, 'yes')
  108. # Comparison
  109. @setup({'if-tag-gt-01': '{% if 2 > 1 %}yes{% else %}no{% endif %}'})
  110. def test_if_tag_gt_01(self):
  111. output = self.engine.render_to_string('if-tag-gt-01')
  112. self.assertEqual(output, 'yes')
  113. @setup({'if-tag-gt-02': '{% if 1 > 1 %}yes{% else %}no{% endif %}'})
  114. def test_if_tag_gt_02(self):
  115. output = self.engine.render_to_string('if-tag-gt-02')
  116. self.assertEqual(output, 'no')
  117. @setup({'if-tag-gte-01': '{% if 1 >= 1 %}yes{% else %}no{% endif %}'})
  118. def test_if_tag_gte_01(self):
  119. output = self.engine.render_to_string('if-tag-gte-01')
  120. self.assertEqual(output, 'yes')
  121. @setup({'if-tag-gte-02': '{% if 1 >= 2 %}yes{% else %}no{% endif %}'})
  122. def test_if_tag_gte_02(self):
  123. output = self.engine.render_to_string('if-tag-gte-02')
  124. self.assertEqual(output, 'no')
  125. @setup({'if-tag-lt-01': '{% if 1 < 2 %}yes{% else %}no{% endif %}'})
  126. def test_if_tag_lt_01(self):
  127. output = self.engine.render_to_string('if-tag-lt-01')
  128. self.assertEqual(output, 'yes')
  129. @setup({'if-tag-lt-02': '{% if 1 < 1 %}yes{% else %}no{% endif %}'})
  130. def test_if_tag_lt_02(self):
  131. output = self.engine.render_to_string('if-tag-lt-02')
  132. self.assertEqual(output, 'no')
  133. @setup({'if-tag-lte-01': '{% if 1 <= 1 %}yes{% else %}no{% endif %}'})
  134. def test_if_tag_lte_01(self):
  135. output = self.engine.render_to_string('if-tag-lte-01')
  136. self.assertEqual(output, 'yes')
  137. @setup({'if-tag-lte-02': '{% if 2 <= 1 %}yes{% else %}no{% endif %}'})
  138. def test_if_tag_lte_02(self):
  139. output = self.engine.render_to_string('if-tag-lte-02')
  140. self.assertEqual(output, 'no')
  141. # Contains
  142. @setup({'if-tag-in-01': '{% if 1 in x %}yes{% else %}no{% endif %}'})
  143. def test_if_tag_in_01(self):
  144. output = self.engine.render_to_string('if-tag-in-01', {'x': [1]})
  145. self.assertEqual(output, 'yes')
  146. @setup({'if-tag-in-02': '{% if 2 in x %}yes{% else %}no{% endif %}'})
  147. def test_if_tag_in_02(self):
  148. output = self.engine.render_to_string('if-tag-in-02', {'x': [1]})
  149. self.assertEqual(output, 'no')
  150. @setup({'if-tag-not-in-01': '{% if 1 not in x %}yes{% else %}no{% endif %}'})
  151. def test_if_tag_not_in_01(self):
  152. output = self.engine.render_to_string('if-tag-not-in-01', {'x': [1]})
  153. self.assertEqual(output, 'no')
  154. @setup({'if-tag-not-in-02': '{% if 2 not in x %}yes{% else %}no{% endif %}'})
  155. def test_if_tag_not_in_02(self):
  156. output = self.engine.render_to_string('if-tag-not-in-02', {'x': [1]})
  157. self.assertEqual(output, 'yes')
  158. # AND
  159. @setup({'if-tag-and01': '{% if foo and bar %}yes{% else %}no{% endif %}'})
  160. def test_if_tag_and01(self):
  161. output = self.engine.render_to_string('if-tag-and01', {'foo': True, 'bar': True})
  162. self.assertEqual(output, 'yes')
  163. @setup({'if-tag-and02': '{% if foo and bar %}yes{% else %}no{% endif %}'})
  164. def test_if_tag_and02(self):
  165. output = self.engine.render_to_string('if-tag-and02', {'foo': True, 'bar': False})
  166. self.assertEqual(output, 'no')
  167. @setup({'if-tag-and03': '{% if foo and bar %}yes{% else %}no{% endif %}'})
  168. def test_if_tag_and03(self):
  169. output = self.engine.render_to_string('if-tag-and03', {'foo': False, 'bar': True})
  170. self.assertEqual(output, 'no')
  171. @setup({'if-tag-and04': '{% if foo and bar %}yes{% else %}no{% endif %}'})
  172. def test_if_tag_and04(self):
  173. output = self.engine.render_to_string('if-tag-and04', {'foo': False, 'bar': False})
  174. self.assertEqual(output, 'no')
  175. @setup({'if-tag-and05': '{% if foo and bar %}yes{% else %}no{% endif %}'})
  176. def test_if_tag_and05(self):
  177. output = self.engine.render_to_string('if-tag-and05', {'foo': False})
  178. self.assertEqual(output, 'no')
  179. @setup({'if-tag-and06': '{% if foo and bar %}yes{% else %}no{% endif %}'})
  180. def test_if_tag_and06(self):
  181. output = self.engine.render_to_string('if-tag-and06', {'bar': False})
  182. self.assertEqual(output, 'no')
  183. @setup({'if-tag-and07': '{% if foo and bar %}yes{% else %}no{% endif %}'})
  184. def test_if_tag_and07(self):
  185. output = self.engine.render_to_string('if-tag-and07', {'foo': True})
  186. self.assertEqual(output, 'no')
  187. @setup({'if-tag-and08': '{% if foo and bar %}yes{% else %}no{% endif %}'})
  188. def test_if_tag_and08(self):
  189. output = self.engine.render_to_string('if-tag-and08', {'bar': True})
  190. self.assertEqual(output, 'no')
  191. # OR
  192. @setup({'if-tag-or01': '{% if foo or bar %}yes{% else %}no{% endif %}'})
  193. def test_if_tag_or01(self):
  194. output = self.engine.render_to_string('if-tag-or01', {'foo': True, 'bar': True})
  195. self.assertEqual(output, 'yes')
  196. @setup({'if-tag-or02': '{% if foo or bar %}yes{% else %}no{% endif %}'})
  197. def test_if_tag_or02(self):
  198. output = self.engine.render_to_string('if-tag-or02', {'foo': True, 'bar': False})
  199. self.assertEqual(output, 'yes')
  200. @setup({'if-tag-or03': '{% if foo or bar %}yes{% else %}no{% endif %}'})
  201. def test_if_tag_or03(self):
  202. output = self.engine.render_to_string('if-tag-or03', {'foo': False, 'bar': True})
  203. self.assertEqual(output, 'yes')
  204. @setup({'if-tag-or04': '{% if foo or bar %}yes{% else %}no{% endif %}'})
  205. def test_if_tag_or04(self):
  206. output = self.engine.render_to_string('if-tag-or04', {'foo': False, 'bar': False})
  207. self.assertEqual(output, 'no')
  208. @setup({'if-tag-or05': '{% if foo or bar %}yes{% else %}no{% endif %}'})
  209. def test_if_tag_or05(self):
  210. output = self.engine.render_to_string('if-tag-or05', {'foo': False})
  211. self.assertEqual(output, 'no')
  212. @setup({'if-tag-or06': '{% if foo or bar %}yes{% else %}no{% endif %}'})
  213. def test_if_tag_or06(self):
  214. output = self.engine.render_to_string('if-tag-or06', {'bar': False})
  215. self.assertEqual(output, 'no')
  216. @setup({'if-tag-or07': '{% if foo or bar %}yes{% else %}no{% endif %}'})
  217. def test_if_tag_or07(self):
  218. output = self.engine.render_to_string('if-tag-or07', {'foo': True})
  219. self.assertEqual(output, 'yes')
  220. @setup({'if-tag-or08': '{% if foo or bar %}yes{% else %}no{% endif %}'})
  221. def test_if_tag_or08(self):
  222. output = self.engine.render_to_string('if-tag-or08', {'bar': True})
  223. self.assertEqual(output, 'yes')
  224. @setup({'if-tag-or09': '{% if foo or bar or baz %}yes{% else %}no{% endif %}'})
  225. def test_if_tag_or09(self):
  226. """
  227. multiple ORs
  228. """
  229. output = self.engine.render_to_string('if-tag-or09', {'baz': True})
  230. self.assertEqual(output, 'yes')
  231. # NOT
  232. @setup({'if-tag-not01': '{% if not foo %}no{% else %}yes{% endif %}'})
  233. def test_if_tag_not01(self):
  234. output = self.engine.render_to_string('if-tag-not01', {'foo': True})
  235. self.assertEqual(output, 'yes')
  236. @setup({'if-tag-not02': '{% if not not foo %}no{% else %}yes{% endif %}'})
  237. def test_if_tag_not02(self):
  238. output = self.engine.render_to_string('if-tag-not02', {'foo': True})
  239. self.assertEqual(output, 'no')
  240. @setup({'if-tag-not06': '{% if foo and not bar %}yes{% else %}no{% endif %}'})
  241. def test_if_tag_not06(self):
  242. output = self.engine.render_to_string('if-tag-not06')
  243. self.assertEqual(output, 'no')
  244. @setup({'if-tag-not07': '{% if foo and not bar %}yes{% else %}no{% endif %}'})
  245. def test_if_tag_not07(self):
  246. output = self.engine.render_to_string('if-tag-not07', {'foo': True, 'bar': True})
  247. self.assertEqual(output, 'no')
  248. @setup({'if-tag-not08': '{% if foo and not bar %}yes{% else %}no{% endif %}'})
  249. def test_if_tag_not08(self):
  250. output = self.engine.render_to_string('if-tag-not08', {'foo': True, 'bar': False})
  251. self.assertEqual(output, 'yes')
  252. @setup({'if-tag-not09': '{% if foo and not bar %}yes{% else %}no{% endif %}'})
  253. def test_if_tag_not09(self):
  254. output = self.engine.render_to_string('if-tag-not09', {'foo': False, 'bar': True})
  255. self.assertEqual(output, 'no')
  256. @setup({'if-tag-not10': '{% if foo and not bar %}yes{% else %}no{% endif %}'})
  257. def test_if_tag_not10(self):
  258. output = self.engine.render_to_string('if-tag-not10', {'foo': False, 'bar': False})
  259. self.assertEqual(output, 'no')
  260. @setup({'if-tag-not11': '{% if not foo and bar %}yes{% else %}no{% endif %}'})
  261. def test_if_tag_not11(self):
  262. output = self.engine.render_to_string('if-tag-not11')
  263. self.assertEqual(output, 'no')
  264. @setup({'if-tag-not12': '{% if not foo and bar %}yes{% else %}no{% endif %}'})
  265. def test_if_tag_not12(self):
  266. output = self.engine.render_to_string('if-tag-not12', {'foo': True, 'bar': True})
  267. self.assertEqual(output, 'no')
  268. @setup({'if-tag-not13': '{% if not foo and bar %}yes{% else %}no{% endif %}'})
  269. def test_if_tag_not13(self):
  270. output = self.engine.render_to_string('if-tag-not13', {'foo': True, 'bar': False})
  271. self.assertEqual(output, 'no')
  272. @setup({'if-tag-not14': '{% if not foo and bar %}yes{% else %}no{% endif %}'})
  273. def test_if_tag_not14(self):
  274. output = self.engine.render_to_string('if-tag-not14', {'foo': False, 'bar': True})
  275. self.assertEqual(output, 'yes')
  276. @setup({'if-tag-not15': '{% if not foo and bar %}yes{% else %}no{% endif %}'})
  277. def test_if_tag_not15(self):
  278. output = self.engine.render_to_string('if-tag-not15', {'foo': False, 'bar': False})
  279. self.assertEqual(output, 'no')
  280. @setup({'if-tag-not16': '{% if foo or not bar %}yes{% else %}no{% endif %}'})
  281. def test_if_tag_not16(self):
  282. output = self.engine.render_to_string('if-tag-not16')
  283. self.assertEqual(output, 'yes')
  284. @setup({'if-tag-not17': '{% if foo or not bar %}yes{% else %}no{% endif %}'})
  285. def test_if_tag_not17(self):
  286. output = self.engine.render_to_string('if-tag-not17', {'foo': True, 'bar': True})
  287. self.assertEqual(output, 'yes')
  288. @setup({'if-tag-not18': '{% if foo or not bar %}yes{% else %}no{% endif %}'})
  289. def test_if_tag_not18(self):
  290. output = self.engine.render_to_string('if-tag-not18', {'foo': True, 'bar': False})
  291. self.assertEqual(output, 'yes')
  292. @setup({'if-tag-not19': '{% if foo or not bar %}yes{% else %}no{% endif %}'})
  293. def test_if_tag_not19(self):
  294. output = self.engine.render_to_string('if-tag-not19', {'foo': False, 'bar': True})
  295. self.assertEqual(output, 'no')
  296. @setup({'if-tag-not20': '{% if foo or not bar %}yes{% else %}no{% endif %}'})
  297. def test_if_tag_not20(self):
  298. output = self.engine.render_to_string('if-tag-not20', {'foo': False, 'bar': False})
  299. self.assertEqual(output, 'yes')
  300. @setup({'if-tag-not21': '{% if not foo or bar %}yes{% else %}no{% endif %}'})
  301. def test_if_tag_not21(self):
  302. output = self.engine.render_to_string('if-tag-not21')
  303. self.assertEqual(output, 'yes')
  304. @setup({'if-tag-not22': '{% if not foo or bar %}yes{% else %}no{% endif %}'})
  305. def test_if_tag_not22(self):
  306. output = self.engine.render_to_string('if-tag-not22', {'foo': True, 'bar': True})
  307. self.assertEqual(output, 'yes')
  308. @setup({'if-tag-not23': '{% if not foo or bar %}yes{% else %}no{% endif %}'})
  309. def test_if_tag_not23(self):
  310. output = self.engine.render_to_string('if-tag-not23', {'foo': True, 'bar': False})
  311. self.assertEqual(output, 'no')
  312. @setup({'if-tag-not24': '{% if not foo or bar %}yes{% else %}no{% endif %}'})
  313. def test_if_tag_not24(self):
  314. output = self.engine.render_to_string('if-tag-not24', {'foo': False, 'bar': True})
  315. self.assertEqual(output, 'yes')
  316. @setup({'if-tag-not25': '{% if not foo or bar %}yes{% else %}no{% endif %}'})
  317. def test_if_tag_not25(self):
  318. output = self.engine.render_to_string('if-tag-not25', {'foo': False, 'bar': False})
  319. self.assertEqual(output, 'yes')
  320. @setup({'if-tag-not26': '{% if not foo and not bar %}yes{% else %}no{% endif %}'})
  321. def test_if_tag_not26(self):
  322. output = self.engine.render_to_string('if-tag-not26')
  323. self.assertEqual(output, 'yes')
  324. @setup({'if-tag-not27': '{% if not foo and not bar %}yes{% else %}no{% endif %}'})
  325. def test_if_tag_not27(self):
  326. output = self.engine.render_to_string('if-tag-not27', {'foo': True, 'bar': True})
  327. self.assertEqual(output, 'no')
  328. @setup({'if-tag-not28': '{% if not foo and not bar %}yes{% else %}no{% endif %}'})
  329. def test_if_tag_not28(self):
  330. output = self.engine.render_to_string('if-tag-not28', {'foo': True, 'bar': False})
  331. self.assertEqual(output, 'no')
  332. @setup({'if-tag-not29': '{% if not foo and not bar %}yes{% else %}no{% endif %}'})
  333. def test_if_tag_not29(self):
  334. output = self.engine.render_to_string('if-tag-not29', {'foo': False, 'bar': True})
  335. self.assertEqual(output, 'no')
  336. @setup({'if-tag-not30': '{% if not foo and not bar %}yes{% else %}no{% endif %}'})
  337. def test_if_tag_not30(self):
  338. output = self.engine.render_to_string('if-tag-not30', {'foo': False, 'bar': False})
  339. self.assertEqual(output, 'yes')
  340. @setup({'if-tag-not31': '{% if not foo or not bar %}yes{% else %}no{% endif %}'})
  341. def test_if_tag_not31(self):
  342. output = self.engine.render_to_string('if-tag-not31')
  343. self.assertEqual(output, 'yes')
  344. @setup({'if-tag-not32': '{% if not foo or not bar %}yes{% else %}no{% endif %}'})
  345. def test_if_tag_not32(self):
  346. output = self.engine.render_to_string('if-tag-not32', {'foo': True, 'bar': True})
  347. self.assertEqual(output, 'no')
  348. @setup({'if-tag-not33': '{% if not foo or not bar %}yes{% else %}no{% endif %}'})
  349. def test_if_tag_not33(self):
  350. output = self.engine.render_to_string('if-tag-not33', {'foo': True, 'bar': False})
  351. self.assertEqual(output, 'yes')
  352. @setup({'if-tag-not34': '{% if not foo or not bar %}yes{% else %}no{% endif %}'})
  353. def test_if_tag_not34(self):
  354. output = self.engine.render_to_string('if-tag-not34', {'foo': False, 'bar': True})
  355. self.assertEqual(output, 'yes')
  356. @setup({'if-tag-not35': '{% if not foo or not bar %}yes{% else %}no{% endif %}'})
  357. def test_if_tag_not35(self):
  358. output = self.engine.render_to_string('if-tag-not35', {'foo': False, 'bar': False})
  359. self.assertEqual(output, 'yes')
  360. # Various syntax errors
  361. @setup({'if-tag-error01': '{% if %}yes{% endif %}'})
  362. def test_if_tag_error01(self):
  363. with self.assertRaises(TemplateSyntaxError):
  364. self.engine.get_template('if-tag-error01')
  365. @setup({'if-tag-error02': '{% if foo and %}yes{% else %}no{% endif %}'})
  366. def test_if_tag_error02(self):
  367. with self.assertRaises(TemplateSyntaxError):
  368. self.engine.render_to_string('if-tag-error02', {'foo': True})
  369. @setup({'if-tag-error03': '{% if foo or %}yes{% else %}no{% endif %}'})
  370. def test_if_tag_error03(self):
  371. with self.assertRaises(TemplateSyntaxError):
  372. self.engine.render_to_string('if-tag-error03', {'foo': True})
  373. @setup({'if-tag-error04': '{% if not foo and %}yes{% else %}no{% endif %}'})
  374. def test_if_tag_error04(self):
  375. with self.assertRaises(TemplateSyntaxError):
  376. self.engine.render_to_string('if-tag-error04', {'foo': True})
  377. @setup({'if-tag-error05': '{% if not foo or %}yes{% else %}no{% endif %}'})
  378. def test_if_tag_error05(self):
  379. with self.assertRaises(TemplateSyntaxError):
  380. self.engine.render_to_string('if-tag-error05', {'foo': True})
  381. @setup({'if-tag-error06': '{% if abc def %}yes{% endif %}'})
  382. def test_if_tag_error06(self):
  383. with self.assertRaises(TemplateSyntaxError):
  384. self.engine.get_template('if-tag-error06')
  385. @setup({'if-tag-error07': '{% if not %}yes{% endif %}'})
  386. def test_if_tag_error07(self):
  387. with self.assertRaises(TemplateSyntaxError):
  388. self.engine.get_template('if-tag-error07')
  389. @setup({'if-tag-error08': '{% if and %}yes{% endif %}'})
  390. def test_if_tag_error08(self):
  391. with self.assertRaises(TemplateSyntaxError):
  392. self.engine.get_template('if-tag-error08')
  393. @setup({'if-tag-error09': '{% if or %}yes{% endif %}'})
  394. def test_if_tag_error09(self):
  395. with self.assertRaises(TemplateSyntaxError):
  396. self.engine.get_template('if-tag-error09')
  397. @setup({'if-tag-error10': '{% if == %}yes{% endif %}'})
  398. def test_if_tag_error10(self):
  399. with self.assertRaises(TemplateSyntaxError):
  400. self.engine.get_template('if-tag-error10')
  401. @setup({'if-tag-error11': '{% if 1 == %}yes{% endif %}'})
  402. def test_if_tag_error11(self):
  403. with self.assertRaises(TemplateSyntaxError):
  404. self.engine.get_template('if-tag-error11')
  405. @setup({'if-tag-error12': '{% if a not b %}yes{% endif %}'})
  406. def test_if_tag_error12(self):
  407. with self.assertRaises(TemplateSyntaxError):
  408. self.engine.get_template('if-tag-error12')
  409. @setup({'else-if-tag-error01': '{% if foo is bar %} yes {% else if foo is not bar %} no {% endif %}'})
  410. def test_else_if_tag_error01(self):
  411. error_message = 'Malformed template tag at line 1: "else if foo is not bar"'
  412. with self.assertRaisesMessage(TemplateSyntaxError, error_message):
  413. self.engine.get_template('else-if-tag-error01')
  414. @setup({'if-tag-shortcircuit01': '{% if x.is_true or x.is_bad %}yes{% else %}no{% endif %}'})
  415. def test_if_tag_shortcircuit01(self):
  416. """
  417. If evaluations are shortcircuited where possible
  418. """
  419. output = self.engine.render_to_string('if-tag-shortcircuit01', {'x': TestObj()})
  420. self.assertEqual(output, 'yes')
  421. @setup({'if-tag-shortcircuit02': '{% if x.is_false and x.is_bad %}yes{% else %}no{% endif %}'})
  422. def test_if_tag_shortcircuit02(self):
  423. """
  424. The is_bad() function should not be evaluated. If it is, an
  425. exception is raised.
  426. """
  427. output = self.engine.render_to_string('if-tag-shortcircuit02', {'x': TestObj()})
  428. self.assertEqual(output, 'no')
  429. @setup({'if-tag-badarg01': '{% if x|default_if_none:y %}yes{% endif %}'})
  430. def test_if_tag_badarg01(self):
  431. """Nonexistent args"""
  432. output = self.engine.render_to_string('if-tag-badarg01')
  433. self.assertEqual(output, '')
  434. @setup({'if-tag-badarg02': '{% if x|default_if_none:y %}yes{% endif %}'})
  435. def test_if_tag_badarg02(self):
  436. output = self.engine.render_to_string('if-tag-badarg02', {'y': 0})
  437. self.assertEqual(output, '')
  438. @setup({'if-tag-badarg03': '{% if x|default_if_none:y %}yes{% endif %}'})
  439. def test_if_tag_badarg03(self):
  440. output = self.engine.render_to_string('if-tag-badarg03', {'y': 1})
  441. self.assertEqual(output, 'yes')
  442. @setup({'if-tag-badarg04': '{% if x|default_if_none:y %}yes{% else %}no{% endif %}'})
  443. def test_if_tag_badarg04(self):
  444. output = self.engine.render_to_string('if-tag-badarg04')
  445. self.assertEqual(output, 'no')
  446. @setup({'if-tag-single-eq': '{% if foo = bar %}yes{% else %}no{% endif %}'})
  447. def test_if_tag_single_eq(self):
  448. # A single equals sign is a syntax error.
  449. with self.assertRaises(TemplateSyntaxError):
  450. self.engine.render_to_string('if-tag-single-eq', {'foo': 1})
  451. @setup({'template': '{% if foo is True %}yes{% else %}no{% endif %}'})
  452. def test_if_is_match(self):
  453. output = self.engine.render_to_string('template', {'foo': True})
  454. self.assertEqual(output, 'yes')
  455. @setup({'template': '{% if foo is True %}yes{% else %}no{% endif %}'})
  456. def test_if_is_no_match(self):
  457. output = self.engine.render_to_string('template', {'foo': 1})
  458. self.assertEqual(output, 'no')
  459. @setup({'template': '{% if foo is bar %}yes{% else %}no{% endif %}'})
  460. def test_if_is_variable_missing(self):
  461. output = self.engine.render_to_string('template', {'foo': 1})
  462. self.assertEqual(output, 'no')
  463. @setup({'template': '{% if foo is bar %}yes{% else %}no{% endif %}'})
  464. def test_if_is_both_variables_missing(self):
  465. output = self.engine.render_to_string('template', {})
  466. self.assertEqual(output, 'yes')
  467. @setup({'template': '{% if foo is not None %}yes{% else %}no{% endif %}'})
  468. def test_if_is_not_match(self):
  469. # For this to act as a regression test, it's important not to use
  470. # foo=True because True is (not None)
  471. output = self.engine.render_to_string('template', {'foo': False})
  472. self.assertEqual(output, 'yes')
  473. @setup({'template': '{% if foo is not None %}yes{% else %}no{% endif %}'})
  474. def test_if_is_not_no_match(self):
  475. output = self.engine.render_to_string('template', {'foo': None})
  476. self.assertEqual(output, 'no')
  477. @setup({'template': '{% if foo is not bar %}yes{% else %}no{% endif %}'})
  478. def test_if_is_not_variable_missing(self):
  479. output = self.engine.render_to_string('template', {'foo': False})
  480. self.assertEqual(output, 'yes')
  481. @setup({'template': '{% if foo is not bar %}yes{% else %}no{% endif %}'})
  482. def test_if_is_not_both_variables_missing(self):
  483. output = self.engine.render_to_string('template', {})
  484. self.assertEqual(output, 'no')