From 300351adc70db62a38e4705bf5f0361bbee94b3a Mon Sep 17 00:00:00 2001 From: Igor Dejanovic <igor.dejanovic@gmail.com> Date: Mon, 4 Aug 2014 18:46:34 +0200 Subject: [PATCH] memoization tests --- tests/unit/regressions/test_memoization.py | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/unit/regressions/test_memoization.py diff --git a/tests/unit/regressions/test_memoization.py b/tests/unit/regressions/test_memoization.py new file mode 100644 index 0000000..627b595 --- /dev/null +++ b/tests/unit/regressions/test_memoization.py @@ -0,0 +1,47 @@ +from StringIO import StringIO +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 'grammar' at position (1, 1)" in capsys.readouterr()[0] + + -- 2.18.0