diff options
author | ptmcg <ptmcg@austin.rr.com> | 2020-06-26 01:59:40 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2020-06-26 01:59:40 -0500 |
commit | c4435b9072dad0436bae008eac748e0f1e003345 (patch) | |
tree | 888a5cab2c7e679c7199ad306e998b8c2da65ad1 | |
parent | 56dee11e32a41a5032fa77655a303339b59ac2a1 (diff) | |
download | pyparsing-git-c4435b9072dad0436bae008eac748e0f1e003345.tar.gz |
Restructure unit tests to do proper testing with and without packrat enabled
-rw-r--r-- | pyparsing/testing.py | 27 | ||||
-rw-r--r-- | pyparsing/util.py | 2 | ||||
-rw-r--r-- | tests/test_unit.py | 37 |
3 files changed, 53 insertions, 13 deletions
diff --git a/pyparsing/testing.py b/pyparsing/testing.py index 0cbefa9..a6add20 100644 --- a/pyparsing/testing.py +++ b/pyparsing/testing.py @@ -46,17 +46,26 @@ class pyparsing_test: def save(self): self._save_context["default_whitespace"] = ParserElement.DEFAULT_WHITE_CHARS self._save_context["default_keyword_chars"] = Keyword.DEFAULT_KEYWORD_CHARS + self._save_context[ "literal_string_class" ] = ParserElement._literalStringClass + self._save_context["packrat_enabled"] = ParserElement._packratEnabled + if ParserElement._packratEnabled: + self._save_context["packrat_cache_size"] = ParserElement.packrat_cache.size + else: + self._save_context["packrat_cache_size"] = None self._save_context["packrat_parse"] = ParserElement._parse + self._save_context["__diag__"] = { name: getattr(__diag__, name) for name in __diag__._all_names } + self._save_context["__compat__"] = { "collect_all_And_tokens": __compat__.collect_all_And_tokens } + return self def restore(self): @@ -68,16 +77,30 @@ class pyparsing_test: ParserElement.setDefaultWhitespaceChars( self._save_context["default_whitespace"] ) + Keyword.DEFAULT_KEYWORD_CHARS = self._save_context["default_keyword_chars"] ParserElement.inlineLiteralsUsing( self._save_context["literal_string_class"] ) + for name, value in self._save_context["__diag__"].items(): (__diag__.enable if value else __diag__.disable)(name) - ParserElement._packratEnabled = self._save_context["packrat_enabled"] - ParserElement._parse = self._save_context["packrat_parse"] + + ParserElement._packratEnabled = False + if self._save_context["packrat_enabled"]: + ParserElement.enablePackrat(self._save_context["packrat_cache_size"]) + else: + ParserElement._parse = self._save_context["packrat_parse"] + __compat__.collect_all_And_tokens = self._save_context["__compat__"] + return self + + def copy(self): + ret = type(self)() + ret._save_context.update(self._save_context) + return ret + def __enter__(self): return self.save() diff --git a/pyparsing/util.py b/pyparsing/util.py index ce68d38..e7843d6 100644 --- a/pyparsing/util.py +++ b/pyparsing/util.py @@ -90,6 +90,7 @@ class _UnboundedCache: def cache_len(self): return len(cache) + self.size = None self.get = types.MethodType(get, self) self.set = types.MethodType(set, self) self.clear = types.MethodType(clear, self) @@ -119,6 +120,7 @@ class _FifoCache: def cache_len(self): return len(cache) + self.size = size self.get = types.MethodType(get, self) self.set = types.MethodType(set, self) self.clear = types.MethodType(clear, self) diff --git a/tests/test_unit.py b/tests/test_unit.py index 719bf04..09fea37 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -73,6 +73,7 @@ class Test1_PyparsingTestInit(TestCase): class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): suite_context = None + save_suite_context = None def setUp(self): self.suite_context.restore() @@ -7753,18 +7754,22 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): class Test3_EnablePackratParsing(TestCase): def runTest(self): + Test2_WithoutPackrat.suite_context.restore() + ParserElement.enablePackrat() # SAVE A NEW SUITE CONTEXT - Test2_WithoutPackrat.save_suite_context = Test2_WithoutPackrat.suite_context - Test2_WithoutPackrat.suite_context = ppt.reset_pyparsing_context() - Test2_WithoutPackrat.suite_context.save() + Test2_WithoutPackrat.suite_context = ppt.reset_pyparsing_context().save() class Test4_WithPackrat(Test2_WithoutPackrat): """ rerun Test2 tests, now that packrat is enabled """ + def test000_assert_packrat_status(self): + self.assertTrue(ParserElement._packratEnabled, "packrat not enabled") + self.assertEqual("_FifoCache", type(ParserElement.packrat_cache).__name__, + msg="incorrect cache type") class Test5_EnableBoundedPackratParsing(TestCase): @@ -7772,17 +7777,20 @@ class Test5_EnableBoundedPackratParsing(TestCase): Test2_WithoutPackrat.suite_context = Test2_WithoutPackrat.save_suite_context Test2_WithoutPackrat.suite_context.restore() - ParserElement.enablePackrat(16) + ParserElement.enablePackrat(cache_size_limit=16) # SAVE A NEW SUITE CONTEXT - Test2_WithoutPackrat.suite_context = ppt.reset_pyparsing_context() - Test2_WithoutPackrat.suite_context.save() + Test2_WithoutPackrat.suite_context = ppt.reset_pyparsing_context().save() class Test6_WithBoundedPackrat(Test2_WithoutPackrat): """ rerun Test2 tests, now with bounded packrat cache """ + def test000_assert_packrat_status(self): + self.assertTrue(ParserElement._packratEnabled, "packrat not enabled") + self.assertEqual("_FifoCache", type(ParserElement.packrat_cache).__name__, + msg="incorrect cache type") class Test7_EnableUnboundedPackratParsing(TestCase): @@ -7790,18 +7798,25 @@ class Test7_EnableUnboundedPackratParsing(TestCase): Test2_WithoutPackrat.suite_context = Test2_WithoutPackrat.save_suite_context Test2_WithoutPackrat.suite_context.restore() - ParserElement.enablePackrat(None) + ParserElement.enablePackrat(cache_size_limit=None) # SAVE A NEW SUITE CONTEXT - Test2_WithoutPackrat.suite_context = ppt.reset_pyparsing_context() - Test2_WithoutPackrat.suite_context.save() + Test2_WithoutPackrat.suite_context = ppt.reset_pyparsing_context().save() class Test8_WithUnboundedPackrat(Test2_WithoutPackrat): """ rerun Test2 tests, now with unbounded packrat cache """ + def test000_assert_packrat_status(self): + self.assertTrue(ParserElement._packratEnabled, "packrat not enabled") + self.assertEqual("_UnboundedCache", type(ParserElement.packrat_cache).__name__, + msg="incorrect cache type") + +# force clear of packrat parsing flags before saving contexts +pp.ParserElement._packratEnabled = False +pp.ParserElement._parse = pp.ParserElement._parseNoCache -Test2_WithoutPackrat.suite_context = ppt.reset_pyparsing_context() -Test2_WithoutPackrat.suite_context.save() +Test2_WithoutPackrat.suite_context = ppt.reset_pyparsing_context().save() +Test2_WithoutPackrat.save_suite_context = ppt.reset_pyparsing_context().save() |