diff options
author | ptmcg <ptmcg@austin.rr.com> | 2019-01-30 20:06:13 -0600 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2019-01-30 20:06:13 -0600 |
commit | f10a02039a6039f8a42b0cc99bea51623676cf01 (patch) | |
tree | bbd802ab697bbea6d5fe4524073cb42fd62fc4bf | |
parent | 349637f0548b12bf8f53ed3b5ff8ddbc5afb247d (diff) | |
download | pyparsing-git-f10a02039a6039f8a42b0cc99bea51623676cf01.tar.gz |
Add tests to show use of Combine and simple Regex
-rw-r--r-- | simple_unit_tests.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/simple_unit_tests.py b/simple_unit_tests.py index f13ff07..fee6d16 100644 --- a/simple_unit_tests.py +++ b/simple_unit_tests.py @@ -155,6 +155,22 @@ class TestWord(PyparsingExpressionTestCase): ), ] +class TestCombine(PyparsingExpressionTestCase): + tests = [ + PpTestSpec( + desc="Parsing real numbers - fail, parsed numbers are in pieces", + expr=pp.OneOrMore(pp.Word(pp.nums) + '.' + pp.Word(pp.nums)), + text="1.2 2.3 3.1416 98.6", + expected_list=['1', '.', '2', '2', '.', '3', '3', '.', '1416', '98', '.', '6'], + ), + PpTestSpec( + desc="Parsing real numbers - better, use Combine to combine multiple tokens into one", + expr=pp.OneOrMore(pp.Combine(pp.Word(pp.nums) + '.' + pp.Word(pp.nums))), + text="1.2 2.3 3.1416 98.6", + expected_list=['1.2', '2.3', '3.1416', '98.6'], + ), + ] + class TestRepetition(PyparsingExpressionTestCase): tests = [ PpTestSpec( @@ -250,6 +266,12 @@ class TestGroups(PyparsingExpressionTestCase): class TestParseAction(PyparsingExpressionTestCase): tests = [ PpTestSpec( + desc="Parsing real numbers - use parse action to convert to float at parse time", + expr=pp.OneOrMore(pp.Combine(pp.Word(pp.nums) + '.' + pp.Word(pp.nums)).addParseAction(lambda t: float(t[0]))), + text="1.2 2.3 3.1416 98.6", + expected_list= [1.2, 2.3, 3.1416, 98.6], # note, these are now floats, not strs + ), + PpTestSpec( desc = "Match with numeric string converted to int", expr = pp.Word("0123456789").addParseAction(lambda t: int(t[0])), text = "12345", @@ -303,6 +325,16 @@ class TestResultsModifyingParseAction(PyparsingExpressionTestCase): ), ] +class TestRegex(PyparsingExpressionTestCase): + tests = [ + PpTestSpec( + desc="Parsing real numbers - using Regex instead of Combine", + expr=pp.OneOrMore(pp.Regex(r'\d+\.\d+').addParseAction(lambda t: float(t[0]))), + text="1.2 2.3 3.1416 98.6", + expected_list=[1.2, 2.3, 3.1416, 98.6], # note, these are now floats, not strs + ), + ] + class TestParseCondition(PyparsingExpressionTestCase): tests = [ PpTestSpec( |