Skip to content

Commit

Permalink
Add new function
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 28, 2018
1 parent eca8efe commit 8cc6939
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,41 @@ def test_nonexistent_function(self):
self.parseEqual(u'%foo{bar}', u'%foo{bar}')


class TestFunctionIfDefEmpty(unittest.TestCase):

def setUp(self):
self.values = {
'empty_string': u'',
'false': False,
'non_empty_string': u'test',
'none': None,
}

def parseEqual(self, a, b):
self.assertEqual(tmep.parse(a, self.values), b)

# empty_string
def test_empty_string(self):
self.parseEqual(u'%ifdefempty{empty_string,trueval}', u'trueval')

# false
def test_false(self):
self.parseEqual(u'%ifdefempty{false,trueval}', u'trueval')

# non_empty_string
def test_non_empty_string(self):
self.parseEqual(u'%ifdefempty{non_empty_string,trueval,falseval}',
u'falseval')

# none
def test_none(self):
self.parseEqual(u'%ifdefempty{none,trueval}', u'trueval')

# nonexistent
def test_nonexistent(self):
self.parseEqual(u'%ifdefempty{nonexistent,trueval,falseval}',
u'falseval')


if __name__ == '__main__':
unittest.main()
20 changes: 20 additions & 0 deletions tmep/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ def tmpl_ifdef(self, field, trueval=u'', falseval=u''):
else:
return falseval

def tmpl_ifdefempty(self, field, trueval=u'', falseval=u''):
"""If field exists and is emtpy return trueval or the field (default)
otherwise, emit return falseval (if provided).
* synopsis: ``%ifdefempty{field,text}`` or \
``%ifdefempty{field,text,falsetext}``
* description: If field exists and is empty, then return truetext. \
Otherwise, returns falsetext. The field should be \
entered without $.
:param field: The name of the field
:param trueval: The string if the condition is true
:param falseval: The string if the condition is false
:return: The string, based on condition
"""
if field in self.values and not self.values[field]:
return trueval
else:
return falseval

@staticmethod
def tmpl_left(s, chars):
"""Get the leftmost characters of a string.
Expand Down

0 comments on commit 8cc6939

Please sign in to comment.