diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-08-20 21:33:59 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-08-20 21:33:59 -0500 |
commit | 99ea242512c3d625f6f94775afbb9d4642600615 (patch) | |
tree | 52ef6638406bb10d526e631ed5e865f8002cbc37 | |
parent | 072f0ddd3c7907bf061c12a21d352a0f31001508 (diff) | |
download | pyparsing-git-99ea242512c3d625f6f94775afbb9d4642600615.tar.gz |
Fix minor bug in creating regex range for single character; add unit tests
-rw-r--r-- | pyparsing.py | 5 | ||||
-rw-r--r-- | unitTests.py | 70 |
2 files changed, 73 insertions, 2 deletions
diff --git a/pyparsing.py b/pyparsing.py index d6c0c34..49b15a8 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -141,6 +141,7 @@ class __config_flags: dname, cls._type_desc, str(getattr(cls, dname)).upper())) + return if dname in cls._all_names: setattr(cls, dname, value) else: @@ -1053,7 +1054,7 @@ class ParseResults(object): except Exception: return False else: - return not isinstance(obj, (str, bytes)) + return not isinstance(obj, str_type) ret = cls([]) for k, v in other.items(): @@ -5312,7 +5313,7 @@ def _collapseAndEscapeRegexRangeChars(s): for c in chars: last = c if first == last: - ret.append(first) + ret.append(escape_re_range_char(first)) else: ret.append("{}-{}".format(escape_re_range_char(first), escape_re_range_char(last))) diff --git a/unitTests.py b/unitTests.py index 34e925b..201e9e5 100644 --- a/unitTests.py +++ b/unitTests.py @@ -4654,6 +4654,76 @@ class EnableWarnDiagsTest(ParseTestCase): self.assertFalse(getattr(pp.__diag__, diag_name), "__diag__.{} not set to True".format(diag_name)) +class WordInternalReRangesTest(ParseTestCase): + def runTest(self): + import pyparsing as pp + import random + import re + + self.assertEqual(pp.Word(pp.printables).reString, "[!-~]+", "failed to generate correct internal re") + self.assertEqual(pp.Word(pp.alphanums).reString, "[0-9A-Za-z]+", "failed to generate correct internal re") + self.assertEqual(pp.Word(pp.pyparsing_unicode.Latin1.printables).reString, "[!-~¡-ÿ]+", + "failed to generate correct internal re") + self.assertEqual(pp.Word(pp.alphas8bit).reString, "[À-ÖØ-öø-ÿ]+", "failed to generate correct internal re") + + esc_chars = r"\^-]" + for esc_char in esc_chars: + # test escape char as first character in range + next_char = chr(ord(esc_char) + 1) + prev_char = chr(ord(esc_char) - 1) + esc_word = pp.Word(esc_char + next_char) + expected = r"[\{}-{}{}]+".format(esc_char, '\\' if next_char in esc_chars else '', next_char) + print("Testing escape char: {} -> {} re: '{}')".format(esc_char, esc_word, esc_word.reString)) + self.assertEqual(esc_word.reString, expected, "failed to generate correct internal re") + test_string = ''.join(random.choice([esc_char, next_char]) for __ in range(16)) + print("Match '{}' -> {}".format(test_string, test_string == esc_word.parseString(test_string)[0])) + self.assertEqual(esc_word.parseString(test_string)[0], test_string, + "Word using escaped range char failed to parse") + + # test escape char as last character in range + esc_word = pp.Word(prev_char + esc_char) + expected = r"[{}{}-\{}]+".format('\\' if prev_char in esc_chars else '', prev_char, esc_char) + print("Testing escape char: {} -> {} re: '{}')".format(esc_char, esc_word, esc_word.reString)) + self.assertEqual(esc_word.reString, expected, "failed to generate correct internal re") + test_string = ''.join(random.choice([esc_char, prev_char]) for __ in range(16)) + print("Match '{}' -> {}".format(test_string, test_string == esc_word.parseString(test_string)[0])) + self.assertEqual(esc_word.parseString(test_string)[0], test_string, + "Word using escaped range char failed to parse") + + # test escape char as first character in range + next_char = chr(ord(esc_char) + 1) + prev_char = chr(ord(esc_char) - 1) + esc_word = pp.Word(esc_char + next_char) + expected = r"[\{}-{}{}]+".format(esc_char, '\\' if next_char in esc_chars else '', next_char) + print("Testing escape char: {} -> {} re: '{}')".format(esc_char, esc_word, esc_word.reString)) + self.assertEqual(esc_word.reString, expected, "failed to generate correct internal re") + test_string = ''.join(random.choice([esc_char, next_char]) for __ in range(16)) + print("Match '{}' -> {}".format(test_string, test_string == esc_word.parseString(test_string)[0])) + self.assertEqual(esc_word.parseString(test_string)[0], test_string, + "Word using escaped range char failed to parse") + + # test escape char as only character in range + esc_word = pp.Word(esc_char + esc_char, pp.alphas.upper()) + expected = r"[\{}][A-Z]*".format(esc_char) + print("Testing escape char: {} -> {} re: '{}')".format(esc_char, esc_word, esc_word.reString)) + self.assertEqual(esc_word.reString, expected, "failed to generate correct internal re") + test_string = esc_char + ''.join(random.choice(pp.alphas.upper()) for __ in range(16)) + print("Match '{}' -> {}".format(test_string, test_string == esc_word.parseString(test_string)[0])) + self.assertEqual(esc_word.parseString(test_string)[0], test_string, + "Word using escaped range char failed to parse") + + # test escape char as only character + esc_word = pp.Word(esc_char, pp.alphas.upper()) + expected = r"{}[A-Z]*".format(re.escape(esc_char)) + print("Testing escape char: {} -> {} re: '{}')".format(esc_char, esc_word, esc_word.reString)) + self.assertEqual(esc_word.reString, expected, "failed to generate correct internal re") + test_string = esc_char + ''.join(random.choice(pp.alphas.upper()) for __ in range(16)) + print("Match '{}' -> {}".format(test_string, test_string == esc_word.parseString(test_string)[0])) + self.assertEqual(esc_word.parseString(test_string)[0], test_string, + "Word using escaped range char failed to parse") + print() + + class MiscellaneousParserTests(ParseTestCase): def runTest(self): |