diff --git a/src/syntax.py b/src/syntax.py index e97badc..e728582 100644 --- a/src/syntax.py +++ b/src/syntax.py @@ -68,3 +68,9 @@ return self.nodes.pop(0) else: return None + + def peek(self): + if self.nodes: + return self.nodes[0] + else: + return None diff --git a/tests/test_syntax.py b/tests/test_syntax.py index 1935c9a..db09eff 100644 --- a/tests/test_syntax.py +++ b/tests/test_syntax.py @@ -213,12 +213,12 @@ assert (syntax1 == syntax2) == equals -# Tests that a syntax stream reads items correctly +# Tests that a syntax stream pops items correctly # We expect the following behaviour: # - All items are popped in order # - None is returned at the end of the stream @given(lists(draw_syntax_random())) -def test_syntax_syntax_stream(nodes): +def test_syntax_syntax_stream_pop(nodes): stream = SyntaxStream(nodes.copy()) read = [] node = stream.pop() @@ -227,3 +227,42 @@ node = stream.pop() assert read == nodes assert stream.pop() is None + + +# Tests that a syntax stream peeks items correctly +# We expect the following behaviour: +# - Peeking does not pop any values +# - None is returned at the end of the stream +@given(lists(draw_syntax_random()), integers(min_value=0, max_value=100)) +def test_syntax_syntax_stream_peek(nodes, times): + stream = SyntaxStream(nodes.copy()) + node_count = len(stream.nodes) + if node_count == 0: + real_times = times + expected = None + else: + real_times = times % len(stream.nodes) + expected = nodes[0] + for _ in range(0, real_times): + node = stream.peek() + assert node == expected + + +# Tests that peeking and popping don't influence each other +# We expect the following behaviour: +# - Peeking does not influence the next pop call +# - Popping does not influence the next peep call +@given(lists(draw_syntax_random())) +def test_syntax_syntax_stream_mixed(nodes): + stream = SyntaxStream(nodes.copy()) + read = [] + node1 = stream.peek() + node2 = stream.pop() + assert node1 == node2 + while node2 is not None: + read.append(node2) + node1 = stream.peek() + node2 = stream.pop() + assert node1 == node2 + assert read == nodes + assert stream.pop() is None