1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from __future__ import unicode_literals
import pytest
import sys
from arpeggio import ParserPython
def test_memoization_positive(capsys):
'''
Test that already matched rule is found in the cache on
subsequent matches.
Args:
capsys - pytest fixture for output capture
'''
def grammar(): return [(rule1, ruleb), (rule1, rulec)]
def rule1(): return rulea, ruleb
def rulea(): return "a"
def ruleb(): return "b"
def rulec(): return "c"
parser = ParserPython(grammar, debug=True)
# Parse input where a rule1 will match but ruleb will fail
# Second sequence will try rule1 again on the same location
# and result should be found in the cache.
parse_tree = parser.parse("a b c")
# Assert that cached result is used
assert "Cache hit" in capsys.readouterr()[0]
def test_memoization_nomatch(capsys):
'''
Test that already failed match is found in the cache on
subsequent matches.
'''
def grammar(): return [(rule1, ruleb), [rule1, rulec]]
def rule1(): return rulea, ruleb
def rulea(): return "a"
def ruleb(): return "b"
def rulec(): return "c"
parser = ParserPython(grammar, debug=True)
parse_tree = parser.parse("c")
assert "Cache hit for [rule1=Sequence, 0] = 'Expected 'rulea' at position (1, 1)" in capsys.readouterr()[0]