Skip to content
This repository has been archived by the owner on May 30, 2020. It is now read-only.

Commit

Permalink
Added more tests to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ercpe committed Jan 9, 2016
1 parent 0c5d1ab commit d3f23f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/phylter/parser.py
Expand Up @@ -107,8 +107,7 @@ def find_group_end(self, consumable):
elif item == '(':
i += 1

if openings > 1:
raise Exception("Unbalanced parenthesis")
raise Exception("Unbalanced parenthesis")

def build_query(self, consumable):
l = []
Expand Down Expand Up @@ -140,7 +139,6 @@ def build_query(self, consumable):
condition = self._get_condition_class(operator)(left, right)
l.append(condition)
else:
print(consumable.current)
raise Exception("Unexpected tokens found: %s" % consumable.iterable[consumable.pos:])

assert all((isinstance(x, (Condition, ConditionGroup)) or x in ('and', 'or') for x in l))
Expand Down
29 changes: 26 additions & 3 deletions tests/test_parser.py
Expand Up @@ -20,13 +20,21 @@ def test_constructor(self):
assert ci.length == 3
assert ci.remaining == 3

def test_current(self):
def test_current_and_next(self):
ci = ConsumableIter([1, 2, 3])
assert ci.current == 1
assert ci.next == 2

def test_current_empty(self):
def test_current_and_next_empty(self):
ci = ConsumableIter([])
assert ci.current is None
assert ci.next is None

def test_current_and_next_beyond_end(self):
ci = ConsumableIter([1])
ci.consume()
assert ci.current is None
assert ci.next is None

def test_has_more(self):
ci = ConsumableIter([1, 2, 3])
Expand Down Expand Up @@ -234,7 +242,22 @@ def test_parse_fail(self):
'foo == 1 or ',
'or',
'and',
# 'foo == bar and (foo < 1 or foo > 2)',
):
with pytest.raises(ParseException) as e:
Parser().parse(s)

def test_find_group_end(self):
p = Parser()

for items, group_end_pos in (
(['foo', '==', '1', ')'], 3),
(['foo', '==', '1', ')', 'or', 'a', '==', '1'], 3),
(['foo', '==', '1', ')', 'or', '(', 'foo', '==', '1', ')'], 3),
):
assert p.find_group_end(ConsumableIter(items)) == group_end_pos

with pytest.raises(Exception):
assert p.find_group_end(ConsumableIter(['foo', '==', 'bar'])) == group_end_pos

with pytest.raises(Exception) as e:
assert p.find_group_end(ConsumableIter(['(', 'foo', '==', 'bar'])) == group_end_pos

0 comments on commit d3f23f2

Please sign in to comment.