diff options
author | Paul McGuire <ptmcg@users.noreply.github.com> | 2020-06-27 17:54:24 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@users.noreply.github.com> | 2020-06-27 17:54:24 -0500 |
commit | 0448be431e30369f6397c96c27cb7745a73a6871 (patch) | |
tree | d48477ac7aaf71f71d4c2f7169f73f4f5f40a01b | |
parent | 8f588705fb0795bfe80fe04dfcbc15e56a893e5c (diff) | |
download | pyparsing-git-0448be431e30369f6397c96c27cb7745a73a6871.tar.gz |
Additional unit testspyparsing_3.0.0a2
-rw-r--r-- | tests/test_unit.py | 96 |
1 files changed, 91 insertions, 5 deletions
diff --git a/tests/test_unit.py b/tests/test_unit.py index 5b5d391..58bd1e6 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -105,6 +105,45 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): print("Packrat enabled:", ParserElement._packratEnabled) self.assertFalse(ParserElement._packratEnabled, "packrat enabled") + def testScanStringWithOverlap(self): + parser = pp.Word(pp.alphas, exact=3) + without_overlaps = sum(t for t, s, e in parser.scanString("ABCDEFGHI")).asList() + self.assertEqual( + ["ABC", "DEF", "GHI"], + without_overlaps, + msg="scanString without overlaps failed", + ) + with_overlaps = sum( + t for t, s, e in parser.scanString("ABCDEFGHI", overlap=True) + ).asList() + self.assertEqual( + ["ABC", "BCD", "CDE", "DEF", "EFG", "FGH", "GHI"], + with_overlaps, + msg="scanString with overlaps failed", + ) + + def testTransformString(self): + make_int_with_commas = ppc.integer().addParseAction( + lambda t: "{:,}".format(t[0]) + ) + lower_case_words = pp.Word(pp.alphas.lower(), asKeyword=True) + pp.Optional( + pp.White() + ) + nested_list = pp.nestedExpr().addParseAction(pp.ParseResults.asList) + transformer = make_int_with_commas | nested_list | lower_case_words.suppress() + + in_string = ( + "I wish to buy 12345 shares of Acme Industries (as a gift to my (ex)wife)" + ) + print(in_string) + out_string = transformer.transformString(in_string) + print(out_string) + self.assertEqual( + "I 12,345 Acme Industries asagifttomyexwife", + out_string, + msg="failure in transformString", + ) + def testUpdateDefaultWhitespace(self): prev_default_whitespace_chars = pp.ParserElement.DEFAULT_WHITE_CHARS @@ -2989,11 +3028,6 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): + td_end.suppress() ) - # ~ manuf_body.setDebug() - - # ~ for tokens in manuf_body.scanString(html): - # ~ print(tokens) - def testParseUsingRegex(self): import re @@ -6902,6 +6936,58 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): "using enable_debug_on_named_expressions", ) + def testEnableDebugOnExpressionWithParseAction(self): + import textwrap + + test_stdout = StringIO() + with resetting(sys, "stdout", "stderr"): + sys.stdout = test_stdout + sys.stderr = test_stdout + + parser = (ppc.integer().setDebug() | pp.Word(pp.alphanums).setDebug())[...] + parser.setDebug() + parser.parseString("123 A100") + + # now turn off debug - should only get output for components, not overall parser + print() + parser.setDebug(False) + parser.parseString("123 A100") + + expected_debug_output = textwrap.dedent( + """\ + Match [{integer | W:(0-9A-Za-z)}]... at loc 0(1,1) + Match integer at loc 0(1,1) + Matched integer -> [123] + Match integer at loc 3(1,4) + Exception raised:Expected integer, found 'A' (at char 4), (line:1, col:5) + Match W:(0-9A-Za-z) at loc 3(1,4) + Matched W:(0-9A-Za-z) -> ['A100'] + Match integer at loc 8(1,9) + Exception raised:Expected integer, found end of text (at char 8), (line:1, col:9) + Match W:(0-9A-Za-z) at loc 8(1,9) + Exception raised:Expected W:(0-9A-Za-z), found end of text (at char 8), (line:1, col:9) + Matched [{integer | W:(0-9A-Za-z)}]... -> [123, 'A100'] + + Match integer at loc 0(1,1) + Matched integer -> [123] + Match integer at loc 3(1,4) + Exception raised:Expected integer, found 'A' (at char 4), (line:1, col:5) + Match W:(0-9A-Za-z) at loc 3(1,4) + Matched W:(0-9A-Za-z) -> ['A100'] + Match integer at loc 8(1,9) + Exception raised:Expected integer, found end of text (at char 8), (line:1, col:9) + Match W:(0-9A-Za-z) at loc 8(1,9) + Exception raised:Expected W:(0-9A-Za-z), found end of text (at char 8), (line:1, col:9) + """ + ) + output = test_stdout.getvalue() + print(output) + self.assertEqual( + expected_debug_output, + output, + "invalid debug output when using parse action", + ) + def testUndesirableButCommonPractices(self): # While these are valid constructs, and they are not encouraged |