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::
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):
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