From: benselme Date: Thu, 8 Jan 2015 22:53:16 +0000 (-0500) Subject: Extract PluralRule.test and add tests X-Git-Tag: dev-2a51c9b95d06~51^2~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9f354e1ff36e0cebdfee88b8eca6bb269a43cae4;p=thirdparty%2Fbabel.git Extract PluralRule.test and add tests --- diff --git a/babel/plural.py b/babel/plural.py index 03f92fc3..1936c5cc 100644 --- a/babel/plural.py +++ b/babel/plural.py @@ -286,6 +286,11 @@ def tokenize_rule(s): return result[::-1] +def test_next_token(tokens, type_, value=None): + return tokens and tokens[-1][0] == type_ and \ + (value is None or tokens[-1][1] == value) + + class _Parser(object): """Internal parser. This class can translate a single rule into an abstract tree of tuples. It implements the following grammar:: @@ -338,12 +343,8 @@ class _Parser(object): raise RuleError('Expected end of rule, got %r' % self.tokens[-1][1]) - def test(self, type_, value=None): - return self.tokens and self.tokens[-1][0] == type_ and \ - (value is None or self.tokens[-1][1] == value) - def skip(self, type_, value=None): - if self.test(type_, value): + if test_next_token(self.tokens, type_, value): return self.tokens.pop() def expect(self, type_, value=None, term=None): diff --git a/tests/test_plural.py b/tests/test_plural.py index 95e23302..49bdfa73 100644 --- a/tests/test_plural.py +++ b/tests/test_plural.py @@ -126,3 +126,20 @@ MALFORMED_TOKEN_TESTS = ( def test_tokenize_malformed(rule_text): with pytest.raises(plural.RuleError): plural.tokenize_rule(rule_text) + + +class TestNextTokenTestCase(unittest.TestCase): + def test_empty(self): + assert not plural.test_next_token([], '') + + def test_type_ok_and_no_value(self): + assert plural.test_next_token([('word', 'and')], 'word') + + def test_type_ok_and_not_value(self): + assert not plural.test_next_token([('word', 'and')], 'word', 'or') + + def test_type_ok_and_value_ok(self): + assert plural.test_next_token([('word', 'and')], 'word', 'and') + + def test_type_not_ok_and_value_ok(self): + assert not plural.test_next_token([('abc', 'and')], 'word', 'and') \ No newline at end of file