diff options
| author | Luke Plant <L.Plant.98@cantab.net> | 2009-12-09 22:40:36 +0000 |
|---|---|---|
| committer | Luke Plant <L.Plant.98@cantab.net> | 2009-12-09 22:40:36 +0000 |
| commit | 2c2f5aee4d44836779fcd74c7782368914f9cfd1 (patch) | |
| tree | 686d60b434ffe9ebcc019ef61208e5cd45921ab9 /tests/regressiontests/templates/smartif.py | |
| parent | 25020ddb05543fff1c37d77b49bd937fd2bbb170 (diff) | |
Implemented 'smart if' template tag, allowing filters and various operators to be used in the 'if' tag
Thanks to Chris Beaven for the initial patch, Fredrik Lundh for the basis
of the parser methodology and Russell Keith-Magee for code reviews.
There are some BACKWARDS INCOMPATIBILITIES in rare cases - in particular, if
you were using the keywords 'and', 'or' or 'not' as variable names within
the 'if' expression, which was previously allowed in some cases.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11806 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/templates/smartif.py')
| -rw-r--r-- | tests/regressiontests/templates/smartif.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/regressiontests/templates/smartif.py b/tests/regressiontests/templates/smartif.py new file mode 100644 index 0000000000..0114753830 --- /dev/null +++ b/tests/regressiontests/templates/smartif.py @@ -0,0 +1,46 @@ +import unittest +from django.template.smartif import IfParser, Literal + +class SmartIfTests(unittest.TestCase): + + def assertCalcEqual(self, expected, tokens): + self.assertEqual(expected, IfParser(tokens).parse().eval({})) + + # We only test things here that are difficult to test elsewhere + # Many other tests are found in the main tests for builtin template tags + # Test parsing via the printed parse tree + def test_not(self): + var = IfParser(["not", False]).parse() + self.assertEqual("(not (literal False))", repr(var)) + self.assert_(var.eval({})) + + self.assertFalse(IfParser(["not", True]).parse().eval({})) + + def test_or(self): + var = IfParser([True, "or", False]).parse() + self.assertEqual("(or (literal True) (literal False))", repr(var)) + self.assert_(var.eval({})) + + def test_in(self): + list_ = [1,2,3] + self.assertCalcEqual(True, [1, 'in', list_]) + self.assertCalcEqual(False, [1, 'in', None]) + self.assertCalcEqual(False, [None, 'in', list_]) + + def test_precedence(self): + # (False and False) or True == True <- we want this one, like Python + # False and (False or True) == False + self.assertCalcEqual(True, [False, 'and', False, 'or', True]) + + # True or (False and False) == True <- we want this one, like Python + # (True or False) and False == False + self.assertCalcEqual(True, [True, 'or', False, 'and', False]) + + # (1 or 1) == 2 -> False + # 1 or (1 == 2) -> True <- we want this one + self.assertCalcEqual(True, [1, 'or', 1, '==', 2]) + + self.assertCalcEqual(True, [True, '==', True, 'or', True, '==', False]) + + self.assertEqual("(or (and (== (literal 1) (literal 2)) (literal 3)) (literal 4))", + repr(IfParser([1, '==', 2, 'and', 3, 'or', 4]).parse())) |
