-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add output checker with support to remove u/b for string literals in …
…doctests.
- Loading branch information
Showing
6 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,50 @@ | ||
import doctest | ||
import re | ||
|
||
ALLOW_UNICODE = doctest.register_optionflag('ALLOW_UNICODE') | ||
ALLOW_BYTES = doctest.register_optionflag('ALLOW_BYTES') | ||
|
||
|
||
class LiteralsOutputChecker(doctest.OutputChecker): | ||
"""A checker with support for u/b literals. | ||
Use the option flags from this module to get the desired effects: | ||
ALLOW_UNICODE removes u/U prefixes | ||
ALLOW_BYTES removes b/B prefixes | ||
This version was copied from pytest | ||
https://github.com/pytest-dev/pytest/blob/master/_pytest/doctest.py | ||
which commented: | ||
Copied from doctest_nose_plugin.py from the nltk project: | ||
https://github.com/nltk/nltk | ||
Further extended to also support byte literals. | ||
""" | ||
|
||
_unicode_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE) | ||
_bytes_literal_re = re.compile(r"(\W|^)[bB]([rR]?[\'\"])", re.UNICODE) | ||
|
||
def check_output(self, want, got, optionflags): | ||
res = doctest.OutputChecker.check_output(self, want, got, | ||
optionflags) | ||
if res: | ||
return True | ||
|
||
allow_unicode = optionflags & ALLOW_UNICODE | ||
allow_bytes = optionflags & ALLOW_BYTES | ||
if not allow_unicode and not allow_bytes: | ||
return False | ||
|
||
else: | ||
def remove_prefixes(regex, txt): | ||
return re.sub(regex, r'\1\2', txt) | ||
|
||
if allow_unicode: | ||
want = remove_prefixes(self._unicode_literal_re, want) | ||
got = remove_prefixes(self._unicode_literal_re, got) | ||
if allow_bytes: | ||
want = remove_prefixes(self._bytes_literal_re, want) | ||
got = remove_prefixes(self._bytes_literal_re, got) | ||
res = doctest.OutputChecker.check_output(self, want, got, | ||
optionflags) | ||
return res |
This file contains 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,101 @@ | ||
from zope.testing.stringliterals import ALLOW_BYTES | ||
from zope.testing.stringliterals import ALLOW_UNICODE | ||
from zope.testing.stringliterals import LiteralsOutputChecker | ||
import textwrap | ||
import unittest | ||
|
||
|
||
class TestLiteralsOutputChecker(unittest.TestCase): | ||
"""Testing .renormalizing.LiteralsOutputChecker.""" | ||
|
||
def setUp(self): | ||
super(TestLiteralsOutputChecker, self).setUp() | ||
self.checker = LiteralsOutputChecker() | ||
|
||
def test__LiteralsOutputChecker__check_output__1(self): | ||
"""It works with simple strings.""" | ||
want = textwrap.dedent("""\ | ||
'Test string' | ||
""") | ||
got = textwrap.dedent("""\ | ||
'Test string' | ||
""") | ||
self.assertTrue(self.checker.check_output(want, got, 0)) | ||
|
||
def test__LiteralsOutputChecker__check_output__2(self): | ||
"""It fails with simple strings.""" | ||
want = textwrap.dedent("""\ | ||
'Test string' | ||
""") | ||
got = textwrap.dedent("""\ | ||
'False test string' | ||
""") | ||
self.assertFalse(self.checker.check_output(want, got, 0)) | ||
|
||
def test__LiteralsOutputChecker__check_output__3(self): | ||
"""It allows byte literals with option flag.""" | ||
want = textwrap.dedent("""\ | ||
'Test string' | ||
b'Test bytes' | ||
""") | ||
got = textwrap.dedent("""\ | ||
b'Test string' | ||
'Test bytes' | ||
""") | ||
self.assertFalse(self.checker.check_output(want, got, 0)) | ||
self.assertTrue(self.checker.check_output(want, got, ALLOW_BYTES)) | ||
|
||
def test__LiteralsOutputChecker__check_output__4(self): | ||
"""It fails on byte literals with the ALLOW_UNICODE option flag.""" | ||
want = textwrap.dedent("""\ | ||
'Test string' | ||
b'Test bytes' | ||
""") | ||
got = textwrap.dedent("""\ | ||
b'Test string' | ||
'Test bytes' | ||
""") | ||
self.assertFalse(self.checker.check_output(want, got, 0)) | ||
self.assertFalse(self.checker.check_output(want, got, ALLOW_UNICODE)) | ||
|
||
def test__LiteralsOutputChecker__check_output__5(self): | ||
"""It works allows unicode literals with option flag.""" | ||
want = textwrap.dedent("""\ | ||
'Test string' | ||
u'Test unicode' | ||
""") | ||
got = textwrap.dedent("""\ | ||
u'Test string' | ||
'Test unicode' | ||
""") | ||
self.assertFalse(self.checker.check_output(want, got, 0)) | ||
self.assertTrue(self.checker.check_output(want, got, ALLOW_UNICODE)) | ||
|
||
def test__LiteralsOutputChecker__check_output__6(self): | ||
"""It fails on unicode literals with the ALLOW_BYTES option flag.""" | ||
want = textwrap.dedent("""\ | ||
'Test string' | ||
u'Test unicode' | ||
""") | ||
got = textwrap.dedent("""\ | ||
u'Test string' | ||
'Test unicode' | ||
""") | ||
self.assertFalse(self.checker.check_output(want, got, 0)) | ||
self.assertFalse(self.checker.check_output(want, got, ALLOW_BYTES)) | ||
|
||
def test__LiteralsOutputChecker__check_output__7(self): | ||
"""It accepts both flags at the same time.""" | ||
want = textwrap.dedent("""\ | ||
'Test string' | ||
'Test unicode' | ||
'Test bytes' | ||
""") | ||
got = textwrap.dedent("""\ | ||
'Test string' | ||
u'Test unicode' | ||
b'Test bytes' | ||
""") | ||
self.assertFalse(self.checker.check_output(want, got, 0)) | ||
self.assertTrue(self.checker.check_output( | ||
want, got, ALLOW_UNICODE | ALLOW_BYTES)) |
This file contains 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