Skip to content
Browse files

Add Keyword spec parsing tests and fixes

  • Loading branch information...
1 parent 31b79f0 commit 90f13cc04ad30dbe1031d1a53cf46dd0b5f1f1f6 @wichert committed Feb 11, 2014
Showing with 48 additions and 13 deletions.
  1. +13 −13 src/lingua/extractors/__init__.py
  2. +35 −0 tests/extractors/test_init.py
View
26 src/lingua/extractors/__init__.py
@@ -84,24 +84,24 @@ def from_spec(cls, spec):
try:
(function, args) = spec.split(':', 1)
kw = cls(function)
+ seen_msgid_param = False
while args:
- if args.match(kw._comment_arg) is not None:
+ if cls._comment_arg.match(args) is not None:
kw.comment = args[1:-1]
break
- (param, args) = args.split(',', 1)
+ (param, args) = args.split(',', 1) if ',' in args else (args, '')
if param.endswith('c'):
- key = 'msgctxt_param'
- param = param[:-1]
- elif param.endswidth('d'):
- key = 'domain'
- param = param[:-1]
- elif param.endswidth('t'):
- key = 'required_arguments'
- param = param[:-1]
+ kw.msgctxt_param = int(param[:-1])
+ elif param.endswith('d'):
+ kw.domain_param = int(param[:-1])
+ elif param.endswith('t'):
+ kw.required_arguments = int(param[:-1])
+ elif not seen_msgid_param:
+ kw.msgid_param = int(param)
+ seen_msgid_param = True
else:
- key = 'msgid_plural_param' if kw.msgid_param else kw.msgid_param
- setattr(kw, key, int(param))
- except ValueError:
+ kw.msgid_plural_param = int(param)
+ except SyntaxError:
raise ValueError('Invalid keyword spec: %s' % spec)
return kw
View
35 tests/extractors/test_init.py
@@ -1,4 +1,5 @@
from lingua.extractors import check_c_format
+from lingua.extractors import Keyword
def test_basic_c_format():
@@ -23,3 +24,37 @@ def test_strftime_is_not_c_format():
flags = []
check_c_format('%Y-%m-%d', flags)
assert 'c-format' not in flags
+
+
+class TestKeywordFromSpec(object):
+ def test_minimal(self):
+ kw = Keyword.from_spec('gettext')
+ assert kw.function == 'gettext'
+ assert kw.msgid_param == 1
+ assert kw.msgid_plural_param is None
+
+ def test_custom_msgid_param(self):
+ kw = Keyword.from_spec('i18n_log:2')
+ assert kw.function == 'i18n_log'
+ assert kw.msgid_param == 2
+ assert kw.msgid_plural_param is None
+
+ def test_plural(self):
+ kw = Keyword.from_spec('ngettext:1,2')
+ assert kw.function == 'ngettext'
+ assert kw.msgid_param == 1
+ assert kw.msgid_plural_param == 2
+
+ def test_domain_param(self):
+ kw = Keyword.from_spec('dgettext:1d,2')
+ assert kw.function == 'dgettext'
+ assert kw.msgid_param == 2
+ assert kw.msgid_plural_param is None
+ assert kw.domain_param == 1
+
+ def test_parameter_count(self):
+ kw = Keyword.from_spec('myfunc:1,5t')
+ assert kw.function == 'myfunc'
+ assert kw.msgid_param == 1
+ assert kw.msgid_plural_param is None
+ assert kw.required_arguments == 5

0 comments on commit 90f13cc

Please sign in to comment.
Something went wrong with that request. Please try again.