PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/templates/smartif.py

https://code.google.com/p/mango-py/
Python | 53 lines | 32 code | 12 blank | 9 comment | 0 complexity | 84baada609cb49463ee6618737bc0d44 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.template.smartif import IfParser, Literal
  2. from django.utils import unittest
  3. class SmartIfTests(unittest.TestCase):
  4. def assertCalcEqual(self, expected, tokens):
  5. self.assertEqual(expected, IfParser(tokens).parse().eval({}))
  6. # We only test things here that are difficult to test elsewhere
  7. # Many other tests are found in the main tests for builtin template tags
  8. # Test parsing via the printed parse tree
  9. def test_not(self):
  10. var = IfParser(["not", False]).parse()
  11. self.assertEqual("(not (literal False))", repr(var))
  12. self.assertTrue(var.eval({}))
  13. self.assertFalse(IfParser(["not", True]).parse().eval({}))
  14. def test_or(self):
  15. var = IfParser([True, "or", False]).parse()
  16. self.assertEqual("(or (literal True) (literal False))", repr(var))
  17. self.assertTrue(var.eval({}))
  18. def test_in(self):
  19. list_ = [1,2,3]
  20. self.assertCalcEqual(True, [1, 'in', list_])
  21. self.assertCalcEqual(False, [1, 'in', None])
  22. self.assertCalcEqual(False, [None, 'in', list_])
  23. def test_not_in(self):
  24. list_ = [1,2,3]
  25. self.assertCalcEqual(False, [1, 'not', 'in', list_])
  26. self.assertCalcEqual(True, [4, 'not', 'in', list_])
  27. self.assertCalcEqual(False, [1, 'not', 'in', None])
  28. self.assertCalcEqual(True, [None, 'not', 'in', list_])
  29. def test_precedence(self):
  30. # (False and False) or True == True <- we want this one, like Python
  31. # False and (False or True) == False
  32. self.assertCalcEqual(True, [False, 'and', False, 'or', True])
  33. # True or (False and False) == True <- we want this one, like Python
  34. # (True or False) and False == False
  35. self.assertCalcEqual(True, [True, 'or', False, 'and', False])
  36. # (1 or 1) == 2 -> False
  37. # 1 or (1 == 2) -> True <- we want this one
  38. self.assertCalcEqual(True, [1, 'or', 1, '==', 2])
  39. self.assertCalcEqual(True, [True, '==', True, 'or', True, '==', False])
  40. self.assertEqual("(or (and (== (literal 1) (literal 2)) (literal 3)) (literal 4))",
  41. repr(IfParser([1, '==', 2, 'and', 3, 'or', 4]).parse()))