This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
/
test_definitions.py
111 lines (89 loc) · 3.27 KB
/
test_definitions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from pep257 import (StringIO, TokenStream, Parser,
Module, Class, Method, Function, NestedFunction)
_ = type('', (), dict(__repr__=lambda *a: '_', __eq__=lambda *a: True))()
parse = Parser()
source = '''
"""Module."""
__all__ = ('a', 'b'
'c',)
def function():
"Function."
def nested_1():
"""Nested."""
if True:
def nested_2():
pass
class class_(object):
"""Class."""
def method_1(self):
"""Method."""
def method_2(self):
def nested_3(self):
"""Nested."""
'''
source_alt = '''
__all__ = ['a', 'b'
'c',]
'''
source_alt_nl_at_bracket = '''
__all__ = [
# Inconvenient comment.
'a', 'b' 'c',]
'''
def test_parser():
all = ('a', 'bc')
module = parse(StringIO(source), 'file.py')
assert len(list(module)) == 8
assert Module('file.py', _, 1, len(source.split('\n')),
'"""Module."""', _, _, all) == module
function, class_ = module.children
assert Function('function', _, _, _, '"Function."', _, module) == function
assert Class('class_', _, _, _, '"""Class."""', _, module) == class_
nested_1, nested_2 = function.children
assert NestedFunction('nested_1', _, _, _,
'"""Nested."""', _, function) == nested_1
assert NestedFunction('nested_2', _, _, _, None, _, function) == nested_2
assert nested_1.is_public is False
method_1, method_2 = class_.children
assert method_1.parent == method_2.parent == class_
assert Method('method_1', _, _, _, '"""Method."""', _, class_) == method_1
assert Method('method_2', _, _, _, None, _, class_) == method_2
nested_3, = method_2.children
assert NestedFunction('nested_3', _, _, _,
'"""Nested."""', _, method_2) == nested_3
assert nested_3.module == module
assert nested_3.all == all
module = parse(StringIO(source_alt), 'file_alt.py')
assert Module('file_alt.py', _, 1, len(source_alt.split('\n')),
None, _, _, all) == module
module = parse(StringIO(source_alt_nl_at_bracket), 'file_alt_nl.py')
assert Module('file_alt_nl.py', _, 1,
len(source_alt_nl_at_bracket.split('\n')), None, _, _,
all) == module
def _test_module():
module = Module(source, 'module.py')
assert module.source == source
assert module.parent is None
assert module.name == 'module'
assert module.docstring == '"""Module docstring."""'
assert module.is_public
function, = module.children
assert function.source.startswith('def function')
assert function.source.endswith('pass\n')
assert function.parent is module
assert function.name == 'function'
assert function.docstring == '"""Function docstring."""'
#nested_function, = function.children
#assert nested_function.source.startswith('def nested_function')
#assert nested_function.source.endswith('pass\n')
def test_token_stream():
stream = TokenStream(StringIO('hello#world'))
assert stream.current.value == 'hello'
assert stream.line == 1
assert stream.move().value == 'hello'
assert stream.current.value == '#world'
assert stream.line == 1
def test_pep257():
"""Run domain-specific tests from test.py file."""
import test
test.run()