def test_tabs(): path = sample_path('doctest_tabs.txt') parser = DocTestParser(optionflags=REPORT_NDIFF|ELLIPSIS) with pytest.raises(ValueError) as excinfo: Document.parse(path, parser) assert str(excinfo.value) == ( 'tabs are not supported, first one found at line 2, column 4' )
def evaluate_region(region, namespace): return region.evaluator( Example(document=Document('', '/the/path'), line=0, column=0, region=region, namespace=namespace))
def test_example_line_and_column(self): text = 'R1XYZ\nR2XYZ\nR3XYZ\nR4XYZ\nR4XYZ\n' i = text.index document = Document(text, '') document.add(Region(0, i('R2') + 2, None, None)) document.add(Region(i('R3') - 1, i('R3') + 2, None, None)) document.add(Region(i('R4') + 3, len(text), None, None)) assert ([(e.line, e.column) for e in document] == [(1, 1), (2, 6), (4, 4)])
def test_windows_line_endings(tmp_path): p = tmp_path / "example.txt" p.write_bytes(b'This is my example:\r\n\r\n' b'.. code-block:: python\r\n\r\n' b' from math import cos\r\n' b' x = 123\r\n\r\n' b'That was my example.\r\n') document = Document.parse(str(p), CodeBlockParser()) example, = document example.evaluate() assert document.namespace['x'] == 123
def test_conditional_full(): document = Document.parse(sample_path('skip-conditional.txt'), DocTestParser(), skip) document.namespace['result'] = result = [] for example in document: try: example.evaluate() except SkipTest as e: result.append('skip:' + str(e)) assert result == [ 'start', 'skip:(2 > 1)', 'good 1', 'skip:foo', 'skip:foo', 'good 2', ]
def test_conditional_edge_cases(): document = Document.parse(sample_path('skip-conditional-edges.txt'), CodeBlockParser(), DocTestParser(), skip) document.namespace['sys'] = sys document.namespace['run'] = [] skipped = [] for example in document: try: example.evaluate() except SkipTest as e: skipped.append(str(e)) assert document.namespace['run'] == [1, 2] # we should always have one and only one skip from this document. if PY3: assert skipped == ['only true on python 2'] else: assert skipped == ['only true on python 3'] * 3
def document(): return Document('ABCDEFGH', '/the/path')
def document_from_sample(name): path = sample_path(name) with open(path, encoding='ascii') as source: return Document(source.read(), path)
def check_document(self, text, expected): d = Document(dedent(text), path='/dev/null') compare(list(r.parsed for r in FileParser('td')(d)), expected=expected)
def test_unicode(): document = Document(u'>>> print("├─")\n├─', path='dummy.rst') example, = DocTestParser()(document) namespace = document.namespace assert evaluate_region(example, namespace) == ''
def test_tabs(): path = sample_path('doctest_tabs.txt') parser = DocTestParser() with pytest.raises(ValueError): Document.parse(path, parser)
def test_basic(): document = Document.parse(sample_path('skip.txt'), CodeBlockParser(), skip) for example in document: example.evaluate() assert document.namespace['run'] == [2, 5]