2010-01-03 20:27:06 +08:00
|
|
|
import py
|
2010-01-14 00:15:54 +08:00
|
|
|
from py._plugin.pytest_restdoc import deindent
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_deindent():
|
|
|
|
assert deindent('foo') == 'foo'
|
|
|
|
assert deindent('foo\n bar') == 'foo\n bar'
|
|
|
|
assert deindent(' foo\n bar\n') == 'foo\nbar\n'
|
|
|
|
assert deindent(' foo\n\n bar\n') == 'foo\n\nbar\n'
|
|
|
|
assert deindent(' foo\n bar\n') == 'foo\n bar\n'
|
|
|
|
assert deindent(' foo\n bar\n') == ' foo\nbar\n'
|
|
|
|
|
|
|
|
class TestDoctest:
|
2010-01-03 20:27:06 +08:00
|
|
|
def setup_class(cls):
|
|
|
|
py.test.importorskip("docutils")
|
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def pytest_funcarg__testdir(self, request):
|
|
|
|
testdir = request.getfuncargvalue("testdir")
|
2009-12-25 07:24:58 +08:00
|
|
|
testdir.plugins.append("restdoc")
|
2009-09-06 22:59:39 +08:00
|
|
|
assert request.module.__name__ == __name__
|
2009-10-29 19:25:47 +08:00
|
|
|
testdir.makepyfile(confrest=
|
2010-01-14 00:15:54 +08:00
|
|
|
"from py._plugin.pytest_restdoc import Project")
|
2009-11-24 22:16:58 +08:00
|
|
|
# we scope our confrest file so that it doesn't
|
2010-07-27 03:15:15 +08:00
|
|
|
# conflict with another global confrest.py
|
2009-11-24 22:16:58 +08:00
|
|
|
testdir.makepyfile(__init__="")
|
2009-09-06 22:59:39 +08:00
|
|
|
return testdir
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_doctest_extra_exec(self, testdir):
|
|
|
|
xtxt = testdir.maketxtfile(x="""
|
|
|
|
hello::
|
2010-07-27 03:15:15 +08:00
|
|
|
.. >>> raise ValueError
|
2009-09-06 22:59:39 +08:00
|
|
|
>>> None
|
|
|
|
""")
|
2009-12-25 07:24:58 +08:00
|
|
|
result = testdir.runpytest(xtxt)
|
|
|
|
result.stdout.fnmatch_lines(['*1 fail*'])
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_doctest_basic(self, testdir):
|
2009-09-06 22:59:39 +08:00
|
|
|
xtxt = testdir.maketxtfile(x="""
|
2010-07-27 03:15:15 +08:00
|
|
|
..
|
|
|
|
>>> from os.path import abspath
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
hello world
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
>>> assert abspath
|
2009-09-06 22:59:39 +08:00
|
|
|
>>> i=3
|
|
|
|
>>> print (i)
|
|
|
|
3
|
|
|
|
|
|
|
|
yes yes
|
|
|
|
|
|
|
|
>>> i
|
|
|
|
3
|
|
|
|
|
|
|
|
end
|
|
|
|
""")
|
2009-12-25 07:24:58 +08:00
|
|
|
result = testdir.runpytest(xtxt)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*2 passed*"
|
|
|
|
])
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_doctest_eol(self, testdir):
|
2009-09-06 22:59:39 +08:00
|
|
|
ytxt = testdir.maketxtfile(y=".. >>> 1 + 1\r\n 2\r\n\r\n")
|
2009-12-25 07:24:58 +08:00
|
|
|
result = testdir.runpytest(ytxt)
|
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_doctest_indentation(self, testdir):
|
|
|
|
footxt = testdir.maketxtfile(foo=
|
|
|
|
'..\n >>> print ("foo\\n bar")\n foo\n bar\n')
|
2009-12-25 07:24:58 +08:00
|
|
|
result = testdir.runpytest(footxt)
|
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_js_ignore(self, testdir):
|
|
|
|
xtxt = testdir.maketxtfile(xtxt="""
|
|
|
|
`blah`_
|
|
|
|
|
|
|
|
.. _`blah`: javascript:some_function()
|
|
|
|
""")
|
2009-12-25 07:24:58 +08:00
|
|
|
result = testdir.runpytest(xtxt)
|
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_pytest_doctest_prepare_content(self, testdir):
|
2009-12-25 07:24:58 +08:00
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_doctest_prepare_content(content):
|
2009-09-06 22:59:39 +08:00
|
|
|
return content.replace("False", "True")
|
2009-12-25 07:24:58 +08:00
|
|
|
""")
|
2009-09-06 22:59:39 +08:00
|
|
|
xtxt = testdir.maketxtfile(x="""
|
|
|
|
hello:
|
|
|
|
|
|
|
|
>>> 2 == 2
|
|
|
|
False
|
|
|
|
|
|
|
|
""")
|
2009-12-25 07:24:58 +08:00
|
|
|
result = testdir.runpytest(xtxt)
|
|
|
|
outcomes = result.parseoutcomes()
|
|
|
|
assert outcomes['passed'] >= 1
|
|
|
|
assert 'failed' not in outcomes
|
2010-07-27 03:15:15 +08:00
|
|
|
assert 'skipped' not in outcomes
|