Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_doctest/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os
from test.support import load_package_tests

def load_tests(*args):
return load_package_tests(os.path.dirname(__file__), *args)
10 changes: 10 additions & 0 deletions Lib/test/test_doctest/decorator_mod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This module is used in `doctest_lineno.py`.
import functools


def decorator(f):
@functools.wraps(f)
def inner():
return f()

return inner
13 changes: 13 additions & 0 deletions Lib/test/test_doctest/doctest_aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Used by test_doctest.py.

class TwoNames:
'''f() and g() are two names for the same method'''

def f(self):
'''
>>> print(TwoNames().f())
f
'''
return 'f'

g = f # define an alias for f
107 changes: 107 additions & 0 deletions Lib/test/test_doctest/doctest_lineno.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# This module is used in `test_doctest`.
# It must not have a docstring.

def func_with_docstring():
"""Some unrelated info."""


def func_without_docstring():
pass


def func_with_doctest():
"""
This function really contains a test case.

>>> func_with_doctest.__name__
'func_with_doctest'
"""
return 3


class ClassWithDocstring:
"""Some unrelated class information."""


class ClassWithoutDocstring:
pass


class ClassWithDoctest:
"""This class really has a test case in it.

>>> ClassWithDoctest.__name__
'ClassWithDoctest'
"""


class MethodWrapper:
def method_with_docstring(self):
"""Method with a docstring."""

def method_without_docstring(self):
pass

def method_with_doctest(self):
"""
This has a doctest!
>>> MethodWrapper.method_with_doctest.__name__
'method_with_doctest'
"""

@classmethod
def classmethod_with_doctest(cls):
"""
This has a doctest!
>>> MethodWrapper.classmethod_with_doctest.__name__
'classmethod_with_doctest'
"""

@property
def property_with_doctest(self):
"""
This has a doctest!
>>> MethodWrapper.property_with_doctest.__name__
'property_with_doctest'
"""

# https://github.com/python/cpython/issues/99433
str_wrapper = object().__str__


# https://github.com/python/cpython/issues/115392
from test.test_doctest.decorator_mod import decorator

@decorator
@decorator
def func_with_docstring_wrapped():
"""Some unrelated info."""


# https://github.com/python/cpython/issues/136914
import functools


@functools.cache
def cached_func_with_doctest(value):
"""
>>> cached_func_with_doctest(1)
-1
"""
return -value


@functools.cache
def cached_func_without_docstring(value):
return value + 1


class ClassWithACachedProperty:

@functools.cached_property
def cached(self):
"""
>>> X().cached
-1
"""
return 0
76 changes: 76 additions & 0 deletions Lib/test/test_doctest/sample_doctest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""This is a sample module that doesn't really test anything all that
interesting.

It simply has a few tests, some of which succeed and some of which fail.

It's important that the numbers remain constant as another test is
testing the running of these tests.


>>> 2+2
4
"""


def foo():
"""

>>> 2+2
5

>>> 2+2
4
"""

def bar():
"""

>>> 2+2
4
"""

def test_silly_setup():
"""

>>> import test.test_doctest.test_doctest
>>> test.test_doctest.test_doctest.sillySetup
True
"""

def w_blank():
"""
>>> if 1:
... print('a')
... print()
... print('b')
a
<BLANKLINE>
b
"""

x = 1
def x_is_one():
"""
>>> x
1
"""

def y_is_one():
"""
>>> y
1
"""

__test__ = {'good': """
>>> 42
42
""",
'bad': """
>>> 42
666
""",
}

def test_suite():
import doctest
return doctest.DocTestSuite()
46 changes: 46 additions & 0 deletions Lib/test/test_doctest/sample_doctest_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""This is a sample module used for testing doctest.

This module includes various scenarios involving errors.

>>> 2 + 2
5
>>> 1/0
1
"""

def g():
[][0] # line 12

def errors():
"""
>>> 2 + 2
5
>>> 1/0
1
>>> def f():
... 2 + '2'
...
>>> f()
1
>>> g()
1
"""

def syntax_error():
"""
>>> 2+*3
5
"""

__test__ = {
'bad': """
>>> 2 + 2
5
>>> 1/0
1
""",
}

def test_suite():
import doctest
return doctest.DocTestSuite()
12 changes: 12 additions & 0 deletions Lib/test/test_doctest/sample_doctest_no_docstrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This is a sample module used for testing doctest.
#
# This module is for testing how doctest handles a module with no
# docstrings.


class Foo(object):

# A class with no docstring.

def __init__(self):
pass
15 changes: 15 additions & 0 deletions Lib/test/test_doctest/sample_doctest_no_doctests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""This is a sample module used for testing doctest.

This module is for testing how doctest handles a module with docstrings
but no doctest examples.

"""


class Foo(object):
"""A docstring with no doctest examples.

"""

def __init__(self):
pass
49 changes: 49 additions & 0 deletions Lib/test/test_doctest/sample_doctest_skip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""This is a sample module used for testing doctest.

This module includes various scenarios involving skips.
"""

def no_skip_pass():
"""
>>> 2 + 2
4
"""

def no_skip_fail():
"""
>>> 2 + 2
5
"""

def single_skip():
"""
>>> 2 + 2 # doctest: +SKIP
4
"""

def double_skip():
"""
>>> 2 + 2 # doctest: +SKIP
4
>>> 3 + 3 # doctest: +SKIP
6
"""

def partial_skip_pass():
"""
>>> 2 + 2 # doctest: +SKIP
4
>>> 3 + 3
6
"""

def partial_skip_fail():
"""
>>> 2 + 2 # doctest: +SKIP
4
>>> 2 + 2
5
"""

def no_examples():
"""A docstring with no examples should not be counted as run or skipped."""
Loading
Loading