Skip to content

Commit

Permalink
Add tests for chevron.renderer.render(keep=True)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dylan-Brotherston committed Mar 21, 2021
1 parent 444b952 commit 410a9c1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
8 changes: 4 additions & 4 deletions chevron/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -243,15 +243,15 @@ 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

# 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)
Expand Down Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,59 @@ def test_disabled_partials(self):
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__":
Expand Down

0 comments on commit 410a9c1

Please sign in to comment.