diff options
| author | Michael Milton <ttmigueltt@gmail.com> | 2020-06-09 15:09:21 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-09 00:09:21 -0500 |
| commit | 17aaf616a752990327ec51f41d2ac4c4a0871215 (patch) | |
| tree | 8a96779b8b238c8eb1c80783a1cd4574c1e14a06 /tests | |
| parent | 1181390886f39240a98977348bd5b9030986f98d (diff) | |
| download | pyparsing-git-17aaf616a752990327ec51f41d2ac4c4a0871215.tar.gz | |
Additional configuration for `skipWhitespace` and `leaveWhitespace` (#219)
* Add .ignoreWhitespace() method
* Add recursive arg to leave- and ignoreWhitespace(), with tests
* Add tests and implementation of the recursive flag
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_simple_unit.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/test_simple_unit.py b/tests/test_simple_unit.py index 15edc33..8682f0f 100644 --- a/tests/test_simple_unit.py +++ b/tests/test_simple_unit.py @@ -568,6 +568,97 @@ class TestCommonHelperExpressions(PyparsingExpressionTestCase): ] +class TestWhitespaceMethods(PyparsingExpressionTestCase): + tests = [ + # These test the single-element versions + PpTestSpec( + desc="The word foo", + expr=pp.Literal("foo").ignoreWhitespace(), + text=" foo ", + expected_list=["foo"], + ), + PpTestSpec( + desc="The word foo", + expr=pp.Literal("foo").leaveWhitespace(), + text=" foo ", + expected_fail_locn=0, + ), + PpTestSpec( + desc="The word foo", + expr=pp.Literal("foo").ignoreWhitespace(), + text="foo", + expected_list=["foo"], + ), + PpTestSpec( + desc="The word foo", + expr=pp.Literal("foo").leaveWhitespace(), + text="foo", + expected_list=["foo"], + ), + # These test the composite elements + PpTestSpec( + desc="If we recursively leave whitespace on the parent, this whitespace-dependent grammar will succeed, even if the children themselves skip whitespace", + expr=pp.And( + [ + pp.Literal(" foo").ignoreWhitespace(), + pp.Literal(" bar").ignoreWhitespace(), + ] + ).leaveWhitespace(recursive=True), + text=" foo bar", + expected_list=[" foo", " bar"], + ), + # + PpTestSpec( + desc="If we recursively ignore whitespace in our parsing, this whitespace-dependent grammar will fail, even if the children themselves keep whitespace", + expr=pp.And( + [ + pp.Literal(" foo").leaveWhitespace(), + pp.Literal(" bar").leaveWhitespace(), + ] + ).ignoreWhitespace(recursive=True), + text=" foo bar", + expected_fail_locn=1, + ), + PpTestSpec( + desc="If we leave whitespace on the parent, but it isn't recursive, this whitespace-dependent grammar will fail", + expr=pp.And( + [ + pp.Literal(" foo").ignoreWhitespace(), + pp.Literal(" bar").ignoreWhitespace(), + ] + ).leaveWhitespace(recursive=False), + text=" foo bar", + expected_fail_locn=5, + ), + # These test the Enhance classes + PpTestSpec( + desc="If we recursively leave whitespace on the parent, this whitespace-dependent grammar will succeed, even if the children themselves skip whitespace", + expr=pp.Optional(pp.Literal(" foo").ignoreWhitespace()).leaveWhitespace( + recursive=True + ), + text=" foo", + expected_list=[" foo"], + ), + # + PpTestSpec( + desc="If we ignore whitespace on the parent, but it isn't recursive, parsing will fail because we skip to the first character 'f' before the internal expr can see it", + expr=pp.Optional(pp.Literal(" foo").leaveWhitespace()).ignoreWhitespace( + recursive=True + ), + text=" foo", + expected_list=[], + ), + # PpTestSpec( + # desc="If we leave whitespace on the parent, this whitespace-dependent grammar will succeed, even if the children themselves skip whitespace", + # expr=pp.Optional(pp.Literal(" foo").ignoreWhitespace()).leaveWhitespace( + # recursive=False + # ), + # text=" foo", + # expected_list=[] + # ), + ] + + def _get_decl_line_no(cls): import inspect |
