]> Devi Nivas Git - chevron.git/commitdiff
Add tests for `chevron.renderer.render(keep=True)`
authorDylan Brotherston <22044664+Dylan-Brotherston@users.noreply.github.com>
Sun, 21 Mar 2021 05:07:47 +0000 (16:07 +1100)
committerDylan Brotherston <22044664+Dylan-Brotherston@users.noreply.github.com>
Sun, 21 Mar 2021 05:07:47 +0000 (16:07 +1100)
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.

chevron/renderer.py
test_spec.py

index 9e38a2dbaa3af087826e8fb186452e21029d4fa6..65a00f6d504afe229dda2a373ac4ce4dcb12f7b6 100644 (file)
@@ -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
index 73e7301567a0e09d52649748de144aa18807f09e..905e1052bbe787d6f6661575c27ea91682d6783a 100755 (executable)
@@ -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__":