]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Extract PluralRule.test and add tests
authorbenselme <benselme@gmail.com>
Thu, 8 Jan 2015 22:53:16 +0000 (17:53 -0500)
committerbenselme <benselme@gmail.com>
Thu, 8 Jan 2015 22:53:16 +0000 (17:53 -0500)
babel/plural.py
tests/test_plural.py

index 03f92fc35c6cad8eb6804fff28c7e10f6d067260..1936c5cc41b7770db4c5c7afd3e584ed37814023 100644 (file)
@@ -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):
index 95e233029bd6bd9e862b4d7e94ab228818943e99..49bdfa737c7f48e3aded710b44677e08fea23c0c 100644 (file)
@@ -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