From: Dylan Brotherston <22044664+Dylan-Brotherston@users.noreply.github.com> Date: Sun, 21 Mar 2021 05:07:47 +0000 (+1100) Subject: Add tests for `chevron.renderer.render(keep=True)` X-Git-Url: https://git.devinivas.org/?a=commitdiff_plain;h=410a9c101492099ae3d32a313c162f9f340f8ff7;p=chevron.git Add tests for `chevron.renderer.render(keep=True)` The first test checks that tags are kept. Including that fact that kept tags are normalised. The second test checks that tags from partials are kept. --- diff --git a/chevron/renderer.py b/chevron/renderer.py index 9e38a2d..65a00f6 100644 --- a/chevron/renderer.py +++ b/chevron/renderer.py @@ -230,7 +230,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', # If we're a variable tag elif tag == 'variable': # Add the html escaped key to the output - thing = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_ldel) + thing = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_rdel) if thing is True and key == '.': # if we've coerced into a boolean by accident # (inverted tags do this) @@ -243,7 +243,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', # If we're a no html escape tag elif tag == 'no escape': # Just lookup the key and add it - thing = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_ldel) + thing = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_rdel) if not isinstance(thing, unicode_type): thing = unicode(str(thing), 'utf-8') output += thing @@ -251,7 +251,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', # If we're a section tag elif tag == 'section': # Get the sections scope - scope = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_ldel) + scope = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_rdel) # If the scope is a callable (as described in # https://mustache.github.io/mustache.5.html) @@ -343,7 +343,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', # If we're an inverted section elif tag == 'inverted section': # Add the flipped scope to the scopes - scope = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_ldel) + scope = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_rdel) scopes.insert(0, not scope) # If we're a partial diff --git a/test_spec.py b/test_spec.py index 73e7301..905e105 100755 --- a/test_spec.py +++ b/test_spec.py @@ -499,6 +499,59 @@ class ExpandedCoverage(unittest.TestCase): self.assertEqual(resultEmpty, expected) os.chdir('..') + # https://github.com/noahmorrison/chevron/pull/94 + def test_keep(self): + args = { + 'template': '{{ first }} {{ second }} {{ third }}', + 'data': { + "first": "1st", + "third": "3rd", + }, + } + + result = chevron.render(**args) + expected = '1st 3rd' + self.assertEqual(result, expected) + + args['keep'] = True + + result = chevron.render(**args) + expected = '1st {{ second }} 3rd' + self.assertEqual(result, expected) + + args['template'] = '{{first}} {{second}} {{third}}' + result = chevron.render(**args) + expected = '1st {{ second }} 3rd' + self.assertEqual(result, expected) + + args['template'] = '{{ first }} {{ second }} {{ third }}' + result = chevron.render(**args) + expected = '1st {{ second }} 3rd' + self.assertEqual(result, expected) + + # https://github.com/noahmorrison/chevron/pull/94 + def test_keep_from_partials(self): + args = { + 'template': '{{ first }} {{> with_missing_key }} {{ third }}', + 'data': { + "first": "1st", + "third": "3rd", + }, + 'partials_dict': { + 'with_missing_key': '{{missing_key}}', + }, + } + + result = chevron.render(**args) + expected = '1st 3rd' + self.assertEqual(result, expected) + + args['keep'] = True + + result = chevron.render(**args) + expected = '1st {{ missing_key }} 3rd' + self.assertEqual(result, expected) + # Run unit tests from command line if __name__ == "__main__":