File: //usr/local/lib/python3.7/test/__pycache__/test_doctest.cpython-37.pyc
B
��g� � @ s d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl Zddl
Z
ddlZddlZddl
Z
dd� ZG dd� d�ZG dd � d e�ZG d
d� d�Zdd
� Zdd� ZG dd� d�ZG dd� de
j�Zdd� ZG dd� d�Zdd� Zdd� Zeed��re�� �sdd� Zdd � Zd!d"� Z d#d$� Z!d%d&� Z"G d'd(� d(�Z#e#d)d*� �Z$d+d,� Z%d-d.� Z&G d/d0� d0ej'j(ej'j)�Z*G d1d2� d2�Z+e
j,d3d4� �Z-d5d6� Z.d7d8� Z/ye�0d9� W n e1k
�r� Y n
X d:d;� Z2d<d=� Z3d>d?� Z4d@dA� Z5dBdC� Z6e7dDk�rdEej8k�re6dF� ne5� dS )Gz
Test script for doctest.
� )�supportNc C s | | S )zG
Blah blah
>>> print(sample_func(22))
44
Yee ha!
� )�vr r �-/usr/local/lib/python3.7/test/test_doctest.py�sample_func s r c @ sb e Zd ZdZdd� Zdd� Zdd� Zdd � Zee�Zd
d� Z e
e �Z eedd
�ZG dd� d�Z
dS )�SampleClassa7
>>> print(1)
1
>>> # comments get ignored. so are empty PS1 and PS2 prompts:
>>>
...
Multiline example:
>>> sc = SampleClass(3)
>>> for i in range(10):
... sc = sc.double()
... print(' ', sc.get(), sep='', end='')
6 12 24 48 96 192 384 768 1536 3072
c C s
|| _ dS )z=
>>> print(SampleClass(12).get())
12
N)�val)�selfr r r r �__init__4 s zSampleClass.__init__c C s t | j| j �S )zF
>>> print(SampleClass(12).double().get())
24
)r r )r r r r �double; s zSampleClass.doublec C s | j S )z=
>>> print(SampleClass(-5).get())
-5
)r )r r r r �getB s zSampleClass.getc C s | d S )zF
>>> print(SampleClass.a_staticmethod(10))
11
� r )r r r r �a_staticmethodI s zSampleClass.a_staticmethodc C s |d S )z�
>>> print(SampleClass.a_classmethod(10))
12
>>> print(SampleClass(0).a_classmethod(10))
12
� r )�clsr r r r �
a_classmethodQ s zSampleClass.a_classmethodzB
>>> print(SampleClass(22).a_property)
22
)�docc @ s* e Zd ZdZd
dd�Zdd� Zdd� Zd S )zSampleClass.NestedClasszu
>>> x = SampleClass.NestedClass(5)
>>> y = x.square()
>>> print(y.get())
25
r c C s
|| _ dS )zR
>>> print(SampleClass.NestedClass().get())
0
N)r )r r r r r r
g s z SampleClass.NestedClass.__init__c C s t �| j| j �S )N)r �NestedClassr )r r r r �squarem s zSampleClass.NestedClass.squarec C s | j S )N)r )r r r r r o s zSampleClass.NestedClass.getN)r )�__name__�
__module__�__qualname__�__doc__r
r r r r r r r ` s
r N)r r r r r
r r r �staticmethodr �classmethod�propertyZ
a_propertyr r r r r r $ s r c @ s( e Zd ZdZdd� Zdd� Zdd� ZdS ) �SampleNewStyleClassz0
>>> print('1\n2\n3')
1
2
3
c C s
|| _ dS )zE
>>> print(SampleNewStyleClass(12).get())
12
N)r )r r r r r r
y s zSampleNewStyleClass.__init__c C s t | j| j �S )zN
>>> print(SampleNewStyleClass(12).double().get())
24
)r r )r r r r r � s zSampleNewStyleClass.doublec C s | j S )zE
>>> print(SampleNewStyleClass(-5).get())
-5
)r )r r r r r � s zSampleNewStyleClass.getN)r r r r r
r r r r r r r r s r c @ s e Zd ZdZdd� Zdd� ZdS )�
_FakeInputa
A fake input stream for pdb's interactive debugger. Whenever a
line is read, print it (to simulate the user typing it), and then
return it. The set of lines to return is specified in the
constructor; they should not have trailing newlines.
c C s
|| _ d S )N)�lines)r r r r r r
� s z_FakeInput.__init__c C s | j �d�}t|� |d S )Nr �
)r �pop�print)r �liner r r �readline� s z_FakeInput.readlineN)r r r r r
r# r r r r r � s r c C s dS )ao
Unit tests for the `Example` class.
Example is a simple container class that holds:
- `source`: A source string.
- `want`: An expected output string.
- `exc_msg`: An expected exception message string (or None if no
exception is expected).
- `lineno`: A line number (within the docstring).
- `indent`: The example's indentation in the input string.
- `options`: An option dictionary, mapping option flags to True or
False.
These attributes are set by the constructor. `source` and `want` are
required; the other attributes all have default values:
>>> example = doctest.Example('print(1)', '1\n')
>>> (example.source, example.want, example.exc_msg,
... example.lineno, example.indent, example.options)
('print(1)\n', '1\n', None, 0, 0, {})
The first three attributes (`source`, `want`, and `exc_msg`) may be
specified positionally; the remaining arguments should be specified as
keyword arguments:
>>> exc_msg = 'IndexError: pop from an empty list'
>>> example = doctest.Example('[].pop()', '', exc_msg,
... lineno=5, indent=4,
... options={doctest.ELLIPSIS: True})
>>> (example.source, example.want, example.exc_msg,
... example.lineno, example.indent, example.options)
('[].pop()\n', '', 'IndexError: pop from an empty list\n', 5, 4, {8: True})
The constructor normalizes the `source` string to end in a newline:
Source spans a single line: no terminating newline.
>>> e = doctest.Example('print(1)', '1\n')
>>> e.source, e.want
('print(1)\n', '1\n')
>>> e = doctest.Example('print(1)\n', '1\n')
>>> e.source, e.want
('print(1)\n', '1\n')
Source spans multiple lines: require terminating newline.
>>> e = doctest.Example('print(1);\nprint(2)\n', '1\n2\n')
>>> e.source, e.want
('print(1);\nprint(2)\n', '1\n2\n')
>>> e = doctest.Example('print(1);\nprint(2)', '1\n2\n')
>>> e.source, e.want
('print(1);\nprint(2)\n', '1\n2\n')
Empty source string (which should never appear in real examples)
>>> e = doctest.Example('', '')
>>> e.source, e.want
('\n', '')
The constructor normalizes the `want` string to end in a newline,
unless it's the empty string:
>>> e = doctest.Example('print(1)', '1\n')
>>> e.source, e.want
('print(1)\n', '1\n')
>>> e = doctest.Example('print(1)', '1')
>>> e.source, e.want
('print(1)\n', '1\n')
>>> e = doctest.Example('print', '')
>>> e.source, e.want
('print\n', '')
The constructor normalizes the `exc_msg` string to end in a newline,
unless it's `None`:
Message spans one line
>>> exc_msg = 'IndexError: pop from an empty list'
>>> e = doctest.Example('[].pop()', '', exc_msg)
>>> e.exc_msg
'IndexError: pop from an empty list\n'
>>> exc_msg = 'IndexError: pop from an empty list\n'
>>> e = doctest.Example('[].pop()', '', exc_msg)
>>> e.exc_msg
'IndexError: pop from an empty list\n'
Message spans multiple lines
>>> exc_msg = 'ValueError: 1\n 2'
>>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg)
>>> e.exc_msg
'ValueError: 1\n 2\n'
>>> exc_msg = 'ValueError: 1\n 2\n'
>>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg)
>>> e.exc_msg
'ValueError: 1\n 2\n'
Empty (but non-None) exception message (which should never appear
in real examples)
>>> exc_msg = ''
>>> e = doctest.Example('raise X()', '', exc_msg)
>>> e.exc_msg
'\n'
Compare `Example`:
>>> example = doctest.Example('print 1', '1\n')
>>> same_example = doctest.Example('print 1', '1\n')
>>> other_example = doctest.Example('print 42', '42\n')
>>> example == same_example
True
>>> example != same_example
False
>>> hash(example) == hash(same_example)
True
>>> example == other_example
False
>>> example != other_example
True
Nr r r r r �test_Example� s wr$ c C s dS )a�
Unit tests for the `DocTest` class.
DocTest is a collection of examples, extracted from a docstring, along
with information about where the docstring comes from (a name,
filename, and line number). The docstring is parsed by the `DocTest`
constructor:
>>> docstring = '''
... >>> print(12)
... 12
...
... Non-example text.
...
... >>> print('another\\example')
... another
... example
... '''
>>> globs = {} # globals to run the test in.
>>> parser = doctest.DocTestParser()
>>> test = parser.get_doctest(docstring, globs, 'some_test',
... 'some_file', 20)
>>> print(test)
<DocTest some_test from some_file:20 (2 examples)>
>>> len(test.examples)
2
>>> e1, e2 = test.examples
>>> (e1.source, e1.want, e1.lineno)
('print(12)\n', '12\n', 1)
>>> (e2.source, e2.want, e2.lineno)
("print('another\\example')\n", 'another\nexample\n', 6)
Source information (name, filename, and line number) is available as
attributes on the doctest object:
>>> (test.name, test.filename, test.lineno)
('some_test', 'some_file', 20)
The line number of an example within its containing file is found by
adding the line number of the example and the line number of its
containing test:
>>> test.lineno + e1.lineno
21
>>> test.lineno + e2.lineno
26
If the docstring contains inconsistent leading whitespace in the
expected output of an example, then `DocTest` will raise a ValueError:
>>> docstring = r'''
... >>> print('bad\nindentation')
... bad
... indentation
... '''
>>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
Traceback (most recent call last):
ValueError: line 4 of the docstring for some_test has inconsistent leading whitespace: 'indentation'
If the docstring contains inconsistent leading whitespace on
continuation lines, then `DocTest` will raise a ValueError:
>>> docstring = r'''
... >>> print(('bad indentation',
... ... 2))
... ('bad', 'indentation')
... '''
>>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
Traceback (most recent call last):
ValueError: line 2 of the docstring for some_test has inconsistent leading whitespace: '... 2))'
If there's no blank space after a PS1 prompt ('>>>'), then `DocTest`
will raise a ValueError:
>>> docstring = '>>>print(1)\n1'
>>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
Traceback (most recent call last):
ValueError: line 1 of the docstring for some_test lacks blank after >>>: '>>>print(1)'
If there's no blank space after a PS2 prompt ('...'), then `DocTest`
will raise a ValueError:
>>> docstring = '>>> if 1:\n...print(1)\n1'
>>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
Traceback (most recent call last):
ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print(1)'
Compare `DocTest`:
>>> docstring = '''
... >>> print 12
... 12
... '''
>>> test = parser.get_doctest(docstring, globs, 'some_test',
... 'some_test', 20)
>>> same_test = parser.get_doctest(docstring, globs, 'some_test',
... 'some_test', 20)
>>> test == same_test
True
>>> test != same_test
False
>>> hash(test) == hash(same_test)
True
>>> docstring = '''
... >>> print 42
... 42
... '''
>>> other_test = parser.get_doctest(docstring, globs, 'other_test',
... 'other_file', 10)
>>> test == other_test
False
>>> test != other_test
True
Compare `DocTestCase`:
>>> DocTestCase = doctest.DocTestCase
>>> test_case = DocTestCase(test)
>>> same_test_case = DocTestCase(same_test)
>>> other_test_case = DocTestCase(other_test)
>>> test_case == same_test_case
True
>>> test_case != same_test_case
False
>>> hash(test_case) == hash(same_test_case)
True
>>> test == other_test_case
False
>>> test != other_test_case
True
Nr r r r r �test_DocTest s r% c @ s"