]> Devi Nivas Git - chevron.git/commitdiff
Add test for custom falsy handling (#35)
authorDaniel Morrison <dan@morrison.ph>
Sat, 20 Apr 2019 23:33:29 +0000 (19:33 -0400)
committerDaniel Morrison <dan@morrison.ph>
Sat, 20 Apr 2019 23:33:29 +0000 (19:33 -0400)
test_spec.py

index 9030ce0edf2f5320b7c1e93db23fb24fedff0b3d..4dfde6fab3061d7f8498564ec0a0adaea68bf334 100755 (executable)
@@ -333,6 +333,46 @@ class ExpandedCoverage(unittest.TestCase):
 
         self.assertEqual(result, expected)
 
+    # https://github.com/noahmorrison/chevron/issues/35
+    def test_custom_falsy(self):
+        class CustomData(dict):
+            class LowercaseBool:
+                _CHEVRON_return_scope_when_falsy = True
+
+                def __init__(self, value):
+                    self.value = value
+
+                def __bool__(self):
+                    return self.value
+                __nonzero__ = __bool__
+
+                def __str__(self):
+                    if self.value:
+                        return 'true'
+                    return 'false'
+
+            def __getitem__(self, key):
+                item = dict.__getitem__(self, key)
+                if isinstance(item, dict):
+                    return CustomData(item)
+                if isinstance(item, bool):
+                    return self.LowercaseBool(item)
+                return item
+
+        args = {
+            'data': CustomData({ 
+                'truthy': True,
+                'falsy': False,
+            }),
+            'template': '{{ truthy }} {{ falsy }}',
+        }
+
+
+        result = chevron.render(**args)
+        expected = 'true false'
+
+        self.assertEqual(result, expected)
+
     # https://github.com/noahmorrison/chevron/issues/39
     def test_nest_loops_with_same_key(self):
         args = {