From 78577debb9127ace54101f77e669d7c674476803 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Fri, 8 Apr 2022 18:35:00 -0600 Subject: [PATCH] Fixes for recent regex library handling of unrecognized escapes --- tests/test_bre.py | 4 +-- tests/test_bregex.py | 60 +++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/tests/test_bre.py b/tests/test_bre.py index d88fe86..64b9a57 100644 --- a/tests/test_bre.py +++ b/tests/test_bre.py @@ -1464,7 +1464,7 @@ def test_escaped_slash_before_backref(self): self.assertEqual(r'\\test: \This is a test of escaped slash backrefs!', results) - def test_normal_escaping(self): + def test_normal_escaping_replace(self): """Test normal escaped slash.""" text = "This is a test of normal escaping!" @@ -1478,7 +1478,7 @@ def test_normal_escaping(self): self.assertEqual(results2, results) self.assertEqual('\t \\t \\\t \\\\t \\\\\t', results) - def test_bytes_normal_escaping(self): + def test_bytes_normal_escaping_replace(self): """Test bytes normal escaped slash.""" text = b"This is a test of normal escaping!" diff --git a/tests/test_bregex.py b/tests/test_bregex.py index 929227a..5a3d0af 100644 --- a/tests/test_bregex.py +++ b/tests/test_bregex.py @@ -801,12 +801,10 @@ def test_bytes_line_break(self): ) def test_line_break_in_group(self): - """Test that line break in group matches a normal R.""" + """Test that line break in group fails.""" - self.assertEqual( - bregex.sub(r"[\R]", 'l', 'Rine\r\nRine\nRine\r'), - 'line\r\nline\nline\r' - ) + with self.assertRaises(_regex_core.error): + bregex.sub(r"[\R]", 'l', 'Rine\r\nRine\nRine\r') def test_replace_unicode_name_ascii_range(self): """Test replacing Unicode names in the ASCII range.""" @@ -1176,33 +1174,33 @@ def test_escaped_slash_before_backref(self): self.assertEqual(r'\\test: \This is a test of escaped slash backrefs!', results) - def test_normal_escaping(self): + def test_normal_escaping_replace(self): """Test normal escaped slash.""" text = "This is a test of normal escaping!" pattern = regex.compile(r"(.+)") - repl_pattern = r'\e \\e \\\e \\\\e \\\\\e' + repl_pattern = r'\t \\t \\\t \\\\t \\\\\t' expand = bregex.compile_replace(pattern, repl_pattern) m = pattern.match(text) results = expand(m) results2 = pattern.sub(repl_pattern, text) self.assertEqual(results2, results) - self.assertEqual('\\e \\e \\\\e \\\\e \\\\\\e', results) + self.assertEqual('\t \\t \\\t \\\\t \\\\\t', results) - def test_bytes_normal_escaping(self): + def test_bytes_normal_escaping_replace(self): """Test bytes normal escaped slash.""" text = b"This is a test of normal escaping!" pattern = regex.compile(br"(.+)") - repl_pattern = br'\e \\e \\\e \\\\e \\\\\e' + repl_pattern = br'\t \\t \\\t \\\\t \\\\\t' expand = bregex.compile_replace(pattern, repl_pattern) m = pattern.match(text) results = expand(m) results2 = pattern.sub(repl_pattern, text) self.assertEqual(results2, results) - self.assertEqual(b'\\e \\e \\\\e \\\\e \\\\\\e', results) + self.assertEqual(b'\t \\t \\\t \\\\t \\\\\t', results) def test_escaped_slash_at_eol(self): """Test escaped slash at end of line.""" @@ -1214,15 +1212,12 @@ def test_escaped_slash_at_eol(self): self.assertEqual('\\\\', results) - def test_unrecognized_backrefs(self): + def test_unrecognized_backrefs2(self): """Test unrecognized backrefs, or literal backslash before a char.""" - text = "This is a test of unrecognized backrefs!" pattern = regex.compile(r"(.+)") - expand = bregex.compile_replace(pattern, r'\k\1') - results = expand(pattern.match(text)) - - self.assertEqual(r'\kThis is a test of unrecognized backrefs!', results) + with self.assertRaises(_regex_core.error): + bregex.compile_replace(pattern, r'\k\1') def test_ignore_group(self): """Test that backrefs inserted by matching groups are passed over.""" @@ -1628,23 +1623,23 @@ def test_dont_case_special_refs(self): self.assertEqual('\u0108\nWw\u0108', results) # Bytes doesn't care about Unicode, but should evaluate bytes - pattern = regex.compile(b'Test') - expand = bregex.compile_replace(pattern, br'\C\u0109\n\x77\E\l\x57\c\u0109') - results = expand(pattern.match(b'Test')) - self.assertEqual(b'\\U0109\nWw\\u0109', results) + # pattern = regex.compile(b'Test') + # expand = bregex.compile_replace(pattern, br'\C\u0109\n\x77\E\l\x57\c\u0109') + # results = expand(pattern.match(b'Test')) + # self.assertEqual(b'\\U0109\nWw\\u0109', results) - expandf = bregex.compile_replace(pattern, br'\C\u0109\n\x77\E\l\x57\c\u0109', bregex.FORMAT) - results = expandf(pattern.match(b'Test')) - self.assertEqual(b'\\U0109\nWw\\u0109', results) + # expandf = bregex.compile_replace(pattern, br'\C\u0109\n\x77\E\l\x57\c\u0109', bregex.FORMAT) + # results = expandf(pattern.match(b'Test')) + # self.assertEqual(b'\\U0109\nWw\\u0109', results) - pattern = regex.compile(b'Test') - expand = bregex.compile_replace(pattern, br'\C\U00000109\n\x77\E\l\x57\c\U00000109') - results = expand(pattern.match(b'Test')) - self.assertEqual(b'\U00000109\nWw\U00000109', results) + # pattern = regex.compile(b'Test') + # expand = bregex.compile_replace(pattern, br'\C\U00000109\n\x77\E\l\x57\c\U00000109') + # results = expand(pattern.match(b'Test')) + # self.assertEqual(b'\U00000109\nWw\U00000109', results) - expandf = bregex.compile_replace(pattern, br'\C\U00000109\n\x77\E\l\x57\c\U00000109', bregex.FORMAT) - results = expandf(pattern.match(b'Test')) - self.assertEqual(b'\U00000109\nWw\U00000109', results) + # expandf = bregex.compile_replace(pattern, br'\C\U00000109\n\x77\E\l\x57\c\U00000109', bregex.FORMAT) + # results = expandf(pattern.match(b'Test')) + # self.assertEqual(b'\U00000109\nWw\U00000109', results) # Format doesn't care about groups pattern = regex.compile('Test') @@ -2184,4 +2179,5 @@ def test_auto_compile_off(self): replace = p.compile(r'{1}', bregex.FORMAT) self.assertEqual(p.subf(replace, 'tests'), 'test') - self.assertEqual(p.sub(r'\ltest', 'tests'), r'\ltest') + with self.assertRaises(_regex_core.error): + self.assertEqual(p.sub(r'\ltest', 'tests'), r'\ltest')