From: Daniel Morrison Date: Sat, 20 Apr 2019 23:33:29 +0000 (-0400) Subject: Add test for custom falsy handling (#35) X-Git-Url: https://git.devinivas.org/?a=commitdiff_plain;h=259ef5a4f721766d1538bd2182b86fba01cb1e93;p=chevron.git Add test for custom falsy handling (#35) --- diff --git a/test_spec.py b/test_spec.py index 9030ce0..4dfde6f 100755 --- a/test_spec.py +++ b/test_spec.py @@ -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 = {