|
| 1 | +# A part of NonVisual Desktop Access (NVDA) |
| 2 | +# This file is covered by the GNU General Public License. |
| 3 | +# See the file COPYING for more details. |
| 4 | +# Copyright (C) 2020 NV Access Limited |
| 5 | + |
| 6 | +"""Unit tests for the characterProcessing module. |
| 7 | +""" |
| 8 | + |
| 9 | +import unittest |
| 10 | +import re |
| 11 | +from characterProcessing import SYMLVL_ALL |
| 12 | +from characterProcessing import SpeechSymbolProcessor |
| 13 | +from characterProcessing import processSpeechSymbols as process |
| 14 | + |
| 15 | + |
| 16 | +class TestComplex(unittest.TestCase): |
| 17 | + """Test the complex symbols rules. |
| 18 | + """ |
| 19 | + |
| 20 | + def _replace_cb(self, replacement, name=None): |
| 21 | + """Return a regexp callback which replaces matches of the given |
| 22 | + group name (or all groups if no name is given) with the |
| 23 | + replacement string, with support for replacement of group |
| 24 | + references. |
| 25 | + """ |
| 26 | + def replace(m): |
| 27 | + if name is None or m.lastgroup == name: |
| 28 | + return SpeechSymbolProcessor._replaceGroups(self, m, replacement) |
| 29 | + return m.group() |
| 30 | + return replace |
| 31 | + |
| 32 | + def _replace(self, string, pattern, replacement, name=None): |
| 33 | + """Perform a pattern-based replacement on a string, for the |
| 34 | + given named group (or all groups if no name is given), with |
| 35 | + support for replacement of group references. |
| 36 | + """ |
| 37 | + regexp = re.compile(pattern, re.UNICODE) |
| 38 | + return regexp.sub(self._replace_cb(replacement, name), string) |
| 39 | + |
| 40 | + def test_group_replacement(self): |
| 41 | + """Test that plain text gets properly replaced |
| 42 | + """ |
| 43 | + replaced = self._replace( |
| 44 | + string="1", |
| 45 | + pattern=r"(\d)", |
| 46 | + replacement="a" |
| 47 | + ) |
| 48 | + self.assertEqual(replaced, "a") |
| 49 | + |
| 50 | + def test_backslash_replacement(self): |
| 51 | + """Test that backslashes get properly replaced |
| 52 | + """ |
| 53 | + replaced = self._replace( |
| 54 | + string="1", |
| 55 | + pattern=r"(\d)", |
| 56 | + replacement=r"\\" |
| 57 | + ) |
| 58 | + self.assertEqual(replaced, "\\") |
| 59 | + |
| 60 | + def test_double_backslash_replacement(self): |
| 61 | + """Test that double backslashes get properly replaced |
| 62 | + """ |
| 63 | + replaced = self._replace( |
| 64 | + string="1", |
| 65 | + pattern=r"(\d)", |
| 66 | + replacement=r"\\\\" |
| 67 | + ) |
| 68 | + self.assertEqual(replaced, r"\\") |
| 69 | + |
| 70 | + def test_unknown_escape(self): |
| 71 | + """Test that a non-supported escaped character (i.e. not \\1, |
| 72 | + \\2, ... \\9 and \\\\) in the replacement raises an error |
| 73 | + """ |
| 74 | + with self.assertRaises(LookupError): |
| 75 | + self._replace( |
| 76 | + string="1", |
| 77 | + pattern=r"(\d)", |
| 78 | + replacement=r"\a" |
| 79 | + ) |
| 80 | + |
| 81 | + def test_missing_group(self): |
| 82 | + """Test that a reference in the replacement to an non-existing |
| 83 | + group raises an error |
| 84 | + """ |
| 85 | + with self.assertRaises(IndexError): |
| 86 | + self._replace( |
| 87 | + string="1", |
| 88 | + pattern=r"(\d)", |
| 89 | + replacement=r"\2" |
| 90 | + ) |
| 91 | + |
| 92 | + def test_unterminated_escape(self): |
| 93 | + """Test that an escape at the end of replacement raises an |
| 94 | + error, since there is nothing to be escaped there |
| 95 | + """ |
| 96 | + with self.assertRaises(LookupError): |
| 97 | + self._replace( |
| 98 | + string="1", |
| 99 | + pattern=r"(\d)", |
| 100 | + replacement="\\" |
| 101 | + ) |
| 102 | + |
| 103 | + def test_group_replacements(self): |
| 104 | + """Test that group references get properly replaced |
| 105 | + """ |
| 106 | + replaced = self._replace( |
| 107 | + string="bar.BAT", |
| 108 | + pattern=r"(([a-z]*)\.([A-Z]*))", |
| 109 | + replacement=r"\2>\1" |
| 110 | + ) |
| 111 | + self.assertEqual(replaced, "BAT>bar") |
| 112 | + |
| 113 | + def test_multiple_group_replacement(self): |
| 114 | + """Test that group indexing is correct with multiple groups |
| 115 | + """ |
| 116 | + replaced = self._replace( |
| 117 | + string="bar.BAT", |
| 118 | + pattern=r"(baz)|(?P<foo>([a-z]*)\.([A-Z]*))", |
| 119 | + replacement=r"\2>\1", |
| 120 | + name="foo" |
| 121 | + ) |
| 122 | + self.assertEqual(replaced, "BAT>bar") |
0 commit comments