Skip to content

Commit

Permalink
Sync with beets 8d0e76383b8b9fb7ba747047050fe41d7fcfcb3e
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 27, 2018
1 parent 5bbc1be commit 9219b22
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
20 changes: 14 additions & 6 deletions tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from __future__ import division, absolute_import, print_function

import unittest
import tmep
import six
from tmep import tmpl as functemplate


def _normexpr(expr):
Expand All @@ -46,7 +46,7 @@ def _normexpr(expr):

def _normparse(text):
"""Parse a template and then normalize the resulting Expression."""
return _normexpr(tmep.tmpl._parse(text))
return _normexpr(functemplate._parse(text))


class ParseTest(unittest.TestCase):
Expand All @@ -56,7 +56,7 @@ def test_empty_string(self):
def _assert_symbol(self, obj, ident):
"""Assert that an object is a Symbol with the given identifier.
"""
self.assertTrue(isinstance(obj, tmep.tmpl.Symbol),
self.assertTrue(isinstance(obj, functemplate.Symbol),
u"not a Symbol: %s" % repr(obj))
self.assertEqual(obj.ident, ident,
u"wrong identifier: %s vs. %s" %
Expand All @@ -66,7 +66,7 @@ def _assert_call(self, obj, ident, numargs):
"""Assert that an object is a Call with the given identifier and
argument count.
"""
self.assertTrue(isinstance(obj, tmep.tmpl.Call),
self.assertTrue(isinstance(obj, functemplate.Call),
u"not a Call: %s" % repr(obj))
self.assertEqual(obj.ident, ident,
u"wrong identifier: %s vs. %s" %
Expand Down Expand Up @@ -227,6 +227,11 @@ def test_sep_with_symbols(self):
self.assertEqual(parts[2], u',')
self._assert_symbol(parts[3], u"bar")

def test_newline_at_end(self):
parts = list(_normparse(u'foo\n'))
self.assertEqual(len(parts), 1)
self.assertEqual(parts[0], u'foo\n')


class EvalTest(unittest.TestCase):
def _eval(self, template):
Expand All @@ -238,7 +243,7 @@ def _eval(self, template):
u'lower': six.text_type.lower,
u'len': len,
}
return tmep.tmpl.Template(template).substitute(values, functions)
return functemplate.Template(template).substitute(values, functions)

def test_plain_text(self):
self.assertEqual(self._eval(u"foo"), u"foo")
Expand Down Expand Up @@ -281,5 +286,8 @@ def test_function_call_with_empty_arg(self):
self.assertEqual(self._eval(u"%len{}"), u"0")


def suite():
return unittest.TestLoader().loadTestsFromName(__name__)

if __name__ == '__main__':
unittest.main()
unittest.main(defaultTest='suite')
11 changes: 7 additions & 4 deletions tmep/tmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def __init__(self, string, in_argument=False):
# Common parsing resources.
special_chars = (SYMBOL_DELIM, FUNC_DELIM, GROUP_OPEN, GROUP_CLOSE,
ESCAPE_CHAR)
special_char_re = re.compile(r'[%s]|$' %
special_char_re = re.compile(r'[%s]|\Z' %
u''.join(re.escape(c) for c in special_chars))
escapable_chars = (SYMBOL_DELIM, FUNC_DELIM, GROUP_CLOSE, ARG_SEP)
terminator_chars = (GROUP_CLOSE,)
Expand All @@ -345,8 +345,11 @@ def parse_expression(self):
if self.in_argument:
extra_special_chars = (ARG_SEP,)
special_char_re = re.compile(
r'[%s]|$' % u''.join(re.escape(c) for c in
self.special_chars + extra_special_chars))
r'[%s]|\Z' % u''.join(
re.escape(c) for c in
self.special_chars + extra_special_chars
)
)

text_parts = []

Expand Down Expand Up @@ -572,7 +575,7 @@ def substitute(self, values={}, functions={}):
"""
try:
res = self.compiled(values, functions)
except: # Handle any exceptions thrown by compiled version.
except Exception: # Handle any exceptions thrown by compiled version.
res = self.interpret(values, functions)

return res
Expand Down

0 comments on commit 9219b22

Please sign in to comment.