-
-
Notifications
You must be signed in to change notification settings - Fork 826
symbols: support group references in replacements #11116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+168
−2
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
95c5c3f
symbols: support group references in replacements
sthibaul 1010ab6
Add a test for a reference to a missing group
sthibaul c7daeed
Fix example according to translator's update
sthibaul 56cec12
Revert to current fr status, translator has fixed this rule, pending …
sthibaul aa70107
Drop unused imports
sthibaul c16f0f2
Update dates naming
sthibaul cbe6832
Fix text
invalid-email-address 6f0a34a
Add some comments in the user guide as well
sthibaul cb3c057
Merge branch 'master' into groups
sthibaul fae834f
drop bogus paragraph
sthibaul 6de43b9
rephrase paragraph for user guide
sthibaul 4a79307
Merge remote-tracking branch 'origin/master' into groups
feerrenrut e3b3fcc
update changes file for PR #11116
feerrenrut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # A part of NonVisual Desktop Access (NVDA) | ||
| # This file is covered by the GNU General Public License. | ||
| # See the file COPYING for more details. | ||
| # Copyright (C) 2020 NV Access Limited | ||
|
|
||
| """Unit tests for the characterProcessing module. | ||
| """ | ||
|
|
||
| import unittest | ||
| import re | ||
| from characterProcessing import SpeechSymbolProcessor | ||
|
|
||
|
|
||
| class TestComplex(unittest.TestCase): | ||
| """Test the complex symbols rules. | ||
| """ | ||
|
|
||
| def _replace_cb(self, replacement, name=None): | ||
| """Return a regexp callback which replaces matches of the given | ||
| group name (or all groups if no name is given) with the | ||
| replacement string, with support for replacement of group | ||
| references. | ||
| """ | ||
| def replace(m): | ||
| if name is None or m.lastgroup == name: | ||
| return SpeechSymbolProcessor._replaceGroups(self, m, replacement) | ||
| return m.group() | ||
| return replace | ||
|
|
||
| def _replace(self, string, pattern, replacement, name=None): | ||
| """Perform a pattern-based replacement on a string, for the | ||
| given named group (or all groups if no name is given), with | ||
| support for replacement of group references. | ||
| """ | ||
| regexp = re.compile(pattern, re.UNICODE) | ||
| return regexp.sub(self._replace_cb(replacement, name), string) | ||
|
|
||
| def test_group_replacement(self): | ||
| """Test that plain text gets properly replaced | ||
| """ | ||
| replaced = self._replace( | ||
| string="1", | ||
| pattern=r"(\d)", | ||
| replacement="a" | ||
| ) | ||
| self.assertEqual(replaced, "a") | ||
|
|
||
| def test_backslash_replacement(self): | ||
| """Test that backslashes get properly replaced | ||
| """ | ||
| replaced = self._replace( | ||
| string="1", | ||
| pattern=r"(\d)", | ||
| replacement=r"\\" | ||
| ) | ||
| self.assertEqual(replaced, "\\") | ||
|
|
||
| def test_double_backslash_replacement(self): | ||
| """Test that double backslashes get properly replaced | ||
| """ | ||
| replaced = self._replace( | ||
| string="1", | ||
| pattern=r"(\d)", | ||
| replacement=r"\\\\" | ||
| ) | ||
| self.assertEqual(replaced, r"\\") | ||
|
|
||
| def test_unknown_escape(self): | ||
| """Test that a non-supported escaped character (i.e. not \\1, | ||
| \\2, ... \\9 and \\\\) in the replacement raises an error | ||
| """ | ||
| with self.assertRaises(LookupError): | ||
| self._replace( | ||
| string="1", | ||
| pattern=r"(\d)", | ||
| replacement=r"\a" | ||
| ) | ||
|
|
||
| def test_missing_group(self): | ||
| """Test that a reference in the replacement to an non-existing | ||
| group raises an error | ||
| """ | ||
| with self.assertRaises(IndexError): | ||
| self._replace( | ||
| string="1", | ||
| pattern=r"(\d)", | ||
| replacement=r"\2" | ||
| ) | ||
|
|
||
| def test_unterminated_escape(self): | ||
| """Test that an escape at the end of replacement raises an | ||
| error, since there is nothing to be escaped there | ||
| """ | ||
| with self.assertRaises(LookupError): | ||
| self._replace( | ||
| string="1", | ||
| pattern=r"(\d)", | ||
| replacement="\\" | ||
| ) | ||
|
|
||
| def test_group_replacements(self): | ||
| """Test that group references get properly replaced | ||
| """ | ||
| replaced = self._replace( | ||
| string="bar.BAT", | ||
| pattern=r"(([a-z]*)\.([A-Z]*))", | ||
| replacement=r"\2>\1" | ||
| ) | ||
| self.assertEqual(replaced, "BAT>bar") | ||
|
|
||
| def test_multiple_group_replacement(self): | ||
| """Test that group indexing is correct with multiple groups | ||
| """ | ||
| replaced = self._replace( | ||
| string="bar.BAT", | ||
| pattern=r"(baz)|(?P<foo>([a-z]*)\.([A-Z]*))", | ||
| replacement=r"\2>\1", | ||
| name="foo" | ||
| ) | ||
| self.assertEqual(replaced, "BAT>bar") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.