def test_format_src(): """ python testing/test_doctest_example.py test_format_src pytest testing/test_doctest_example.py::test_format_src -s -v """ string = utils.codeblock( ''' >>> i = 0 >>> 0 / i 2 ''') string_with_lineno = utils.codeblock( ''' 1 >>> i = 0 2 >>> 0 / i 2 ''').replace('!', ' ') self = doctest_example.DocTest(docsrc=string) self._parse() assert self.format_src(colored=0, linenos=1) == string_with_lineno assert self.format_src(colored=0, linenos=0) == string assert utils.strip_ansi(self.format_src(colored=1, linenos=1)) == string_with_lineno assert utils.strip_ansi(self.format_src(colored=1, linenos=0)) == string
def test_clearline(): """ Make sure a question mark is printed if the total is unknown pytest ubelt/tests/test_progiter.py::test_clearline """ file = cStringIO() # Clearline=False version should simply have a newline at the end. prog = ProgIter(file=file, show_times=False, clearline=False) message = prog.format_message() assert strip_ansi(message).strip(' ') == '0/?... \n' # Clearline=True version should carrage return at the begining and have no # newline at the end. prog = ProgIter(file=file, show_times=False, clearline=True) message = prog.format_message() assert strip_ansi(message).strip(' ') == '\r 0/?...'
def test_progiter_offset_10(): """ pytest -s ~/code/ubelt/ubelt/tests/test_progiter.py::test_progiter_offset_10 xdoctest ~/code/ubelt/tests/test_progiter.py test_progiter_offset_10 """ # Define a function that takes some time file = cStringIO() list( ProgIter(range(10), total=20, verbose=3, start=10, file=file, freq=5, show_times=False)) file.seek(0) want = ['10/20...', '15/20...', '20/20...'] got = [line.strip() for line in file.readlines()] if sys.platform.startswith('win32'): # nocover # on windows \r seems to be mixed up with ansi sequences from xdoctest.utils import strip_ansi got = [strip_ansi(line).strip() for line in got] print('want = {!r}'.format(want)) print('got = {!r}'.format(got)) assert got == want
def test_initial(): """ Make sure a question mark is printed if the total is unknown """ file = cStringIO() prog = ProgIter(initial=9001, file=file, show_times=False, clearline=False) message = prog.format_message() assert strip_ansi(message) == ' 9001/?... \n'
def test_progiter_offset_0(): """ pytest -s ~/code/progiter/tests/test_progiter.py::test_progiter_offset_0 """ # Define a function that takes some time file = cStringIO() for _ in ProgIter(range(10), total=20, verbose=3, start=0, file=file, freq=5, show_times=False): pass file.seek(0) want = ['0/20...', '5/20...', '10/20...'] got = [line.strip() for line in file.readlines()] if sys.platform.startswith('win32'): # nocover # on windows \r seems to be mixed up with ansi sequences from xdoctest.utils import strip_ansi got = [strip_ansi(line).strip() for line in got] assert got == want
def warn(harn, msg): if harn.flog: msg = strip_ansi(msg) harn.flog.warn(msg) print(msg)
def error(harn, msg): print(msg) if harn.flog: msg = strip_ansi(msg) harn.flog.error(msg)
def debug(harn, msg): if harn.flog: msg = strip_ansi(msg) harn.flog.debug(msg)
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 got = remove_prefixes(unicode_literal_re, got) want = remove_prefixes(unicode_literal_re, want) # Note: normalizing away prefixes can cause weird "got" # results to print when there is a got-want mismatch. # For instance, if you get {'b': 22} but you want {'b': 2} # this will cause xdoctest to report that you wanted {'': 2} # because it reports the normalized version of the want message got = remove_prefixes(bytes_literal_re, got) want = remove_prefixes(bytes_literal_re, want) # Replace <BLANKLINE>s if it is being used. if not runstate['DONT_ACCEPT_BLANKLINE']: want = remove_blankline_marker(want) # always remove trailing whitepsace got = re.sub(TRAILING_WS, '', got) want = re.sub(TRAILING_WS, '', want) # normalize endling newlines want = want.rstrip() got = got.rstrip() # Always remove invisible text got_lines = got.splitlines(True) want_lines = want.splitlines(True) got_lines = visible_text(got_lines) want_lines = visible_text(want_lines) want = ''.join(want_lines) got = ''.join(got_lines) if runstate['NORMALIZE_WHITESPACE'] or runstate['IGNORE_WHITESPACE']: # all whitespace normalization # treat newlines and all whitespace as a single space got = ' '.join(got.split()) want = ' '.join(want.split()) if runstate['IGNORE_WHITESPACE']: # Completely remove whitespace got = re.sub(r'\s', '', got, flags=re.MULTILINE) want = re.sub(r'\s', '', want, flags=re.MULTILINE) if runstate['NORMALIZE_REPR']: def norm_repr(a, b): # If removing quotes would allow for a match, remove them. if not _check_match(a, b, runstate): for q in ['"', "'"]: if a.startswith(q) and a.endswith(q): if _check_match(a[1:-1], b, runstate): return a[1:-1] return a got = norm_repr(got, want) want = norm_repr(want, got) return got, want
def debug(harn, msg): if harn.flog: from xdoctest.utils import strip_ansi msg = strip_ansi(msg) harn.flog.debug(msg)