Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def normalize(got, want, runstate=None):
r"""
Adapated from doctest_nose_plugin.py from the nltk project:
https://github.com/nltk/nltk
Further extended to also support byte literals.
Example:
>>> want = "...\n(0, 2, {'weight': 1})\n(0, 3, {'weight': 2})"
>>> got = "(0, 2, {'weight': 1})\n(0, 3, {'weight': 2})"
"""
if runstate is None:
runstate = directive.RuntimeState()
def remove_prefixes(regex, text):
return re.sub(regex, r'\1\2', text)
def visible_text(lines):
# TODO: backspaces
# Any lines that end with only a carrage return are erased
return [line for line in lines if not line.endswith('\r')]
# Remove terminal colors
if True:
got = utils.strip_ansi(got)
want = utils.strip_ansi(want)
if True:
# normalize python 2/3 byte/unicode prefixes
def check_output(got, want, runstate=None):
"""
Does the actual comparison between `got` and `want`
"""
if not want: # nocover
return True
if want:
# Try default
if got == want:
return True
if runstate is None:
runstate = directive.RuntimeState()
got, want = normalize(got, want, runstate)
return _check_match(got, want, runstate)
return False
def test_blankline_not_accept():
# Check that blankline is not normalized away when
# DONT_ACCEPT_BLANKLINE is on
runstate = directive.RuntimeState({'DONT_ACCEPT_BLANKLINE': True})
got = 'foo\n\nbar'
want = 'foo\n\nbar'
assert not checker.check_output(got, want, runstate)
def output_repr_difference(self, runstate=None):
"""
Constructs a repr difference with minimal normalization.
"""
minimal_got = self.got.rstrip()
minimal_want = self.want.rstrip()
if runstate is None:
runstate = directive.RuntimeState()
# Don't normalize because it usually removes the newlines
runstate_ = runstate.to_dict()
if not runstate_['DONT_ACCEPT_BLANKLINE']:
minimal_want = remove_blankline_marker(minimal_want)
lines = [
('Repr Difference:'),
# TODO: get a semi-normalized output before showing repr?
(' got = {!r}'.format(minimal_got)),
(' want = {!r}'.format(minimal_want)),
]
return '\n'.join(lines)
def test_blankline_accept():
"""
pytest testing/test_checker.py
"""
# Check that blankline is normalized away
runstate = directive.RuntimeState({'DONT_ACCEPT_BLANKLINE': False})
got = 'foo\n\nbar'
want = 'foo\n\nbar'
assert checker.check_output(got, want, runstate)
def output_difference(self, runstate=None, colored=True):
"""
Return a string describing the differences between the expected output
for a given example (`example`) and the actual output (`got`).
The `runstate` contains option flags used to compare `want` and `got`.
Notes:
This does not check if got matches want, it only outputs the raw
differences. Got/Want normalization may make the differences appear
more exagerated than they are.
"""
got = self.got
want = self.want
if runstate is None:
runstate = directive.RuntimeState()
# Don't normalize because it usually removes the newlines
runstate_ = runstate.to_dict()
# Don't normalize whitespaces in report for better visibility
runstate_['NORMALIZE_WHITESPACE'] = False
runstate_['IGNORE_WHITESPACE'] = False
got, want = normalize(got, want, runstate_)
# If s are being used, then replace blank lines
# with in the actual output string.
# if not runstate['DONT_ACCEPT_BLANKLINE']:
# got = re.sub('(?m)^[ ]*(?=\n)', BLANKLINE_MARKER, got)
got = utils.ensure_unicode(got)