Skip to content

Commit

Permalink
Choice tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Jul 22, 2018
1 parent 2ca8678 commit 5f471f8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
63 changes: 59 additions & 4 deletions tests/test_textparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import textparser
from textparser import Grammar
from textparser import Sequence
from textparser import Choice
from textparser import ChoiceDict
from textparser import OneOrMore
from textparser import ZeroOrMore
from textparser import DelimitedList
from textparser import Token
from textparser import TokenizerError
from textparser import create_token_re


def tokenize(items):
Expand All @@ -28,10 +31,53 @@ def test_sequence(self):

def test_sequence_mismatch(self):
grammar = Grammar(Sequence('NUMBER', 'WORD'))
tokens = tokenize([
('NUMBER', '1.45'),
('__EOF__', '')
])
tokens = tokenize([('NUMBER', '1.45'), ('__EOF__', '')])

with self.assertRaises(textparser.Error) as cm:
grammar.parse(tokens)

self.assertEqual(str(cm.exception), '')

def test_choice(self):
grammar = Grammar(Choice('NUMBER', 'WORD'))

datas = [
([('WORD', 'm')], 'm'),
([('NUMBER', '5')], '5')
]

for tokens, expected_tree in datas:
tokens = tokenize(tokens + [('__EOF__', '')])
tree = grammar.parse(tokens)
self.assertEqual(tree, expected_tree)

def test_choice_mismatch(self):
grammar = Grammar(Choice('NUMBER', 'WORD'))
tokens = tokenize([(',', ','), ('__EOF__', '')])

with self.assertRaises(textparser.Error) as cm:
grammar.parse(tokens)

self.assertEqual(str(cm.exception), '')

def test_choice_dict(self):
grammar = Grammar(ChoiceDict(Sequence('NUMBER'),
Sequence('WORD')))

datas = [
([('WORD', 'm')], ['m']),
([('NUMBER', '5')], ['5'])
]

for tokens, expected_tree in datas:
tokens = tokenize(tokens + [('__EOF__', '')])
tree = grammar.parse(tokens)
self.assertEqual(tree, expected_tree)

def test_choice_dict_mismatch(self):
grammar = Grammar(ChoiceDict(Sequence('NUMBER'),
Sequence('WORD')))
tokens = tokenize([(',', ','), ('__EOF__', '')])

with self.assertRaises(textparser.Error) as cm:
grammar.parse(tokens)
Expand Down Expand Up @@ -126,6 +172,15 @@ def test_tokenizer_error(self):
str(cm.exception),
'Invalid syntax at line 0, column 1: "{}"'.format(message))

def test_create_token_re(self):
datas = [
([('A', r'a')], '(?P<A>a)'),
([('A', r'b'), ('C', r'd')], '(?P<A>b)|(?P<C>d)')
]

for spec, re_token in datas:
self.assertEqual(create_token_re(spec), re_token)


if __name__ == '__main__':
unittest.main()
6 changes: 5 additions & 1 deletion textparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,11 @@ def __init__(self, grammar):

def parse(self, tokens):
tokens = _Tokens(tokens)
parsed = self._root.match(tokens)

try:
parsed = self._root.match(tokens)
except KeyError:
parsed = None

if parsed is not None and tokens.get().kind == '__EOF__':
return parsed
Expand Down

0 comments on commit 5f471f8

Please sign in to comment.