1
- """
2
- Unit test for pep257 module decorator handling.
1
+ """Unit test for pep257 module decorator handling.
3
2
4
3
Use tox or py.test to run the test suite.
5
4
"""
9
8
except ImportError :
10
9
from io import StringIO
11
10
11
+ import textwrap
12
+
12
13
import pep257
13
14
14
15
15
16
class TestParser :
16
- """
17
- Check parsing of Python source code.
18
- """
17
+ """Check parsing of Python source code."""
19
18
20
19
def test_parse_class_single_decorator (self ):
21
- """
22
- Class decorator is recorded in class instance.
23
- """
24
- code = """
25
- @first_decorator
26
- class Foo:
27
- pass
28
- """
20
+ """Class decorator is recorded in class instance."""
21
+ code = textwrap .dedent ("""\
22
+ @first_decorator
23
+ class Foo:
24
+ pass
25
+ """ )
29
26
module = pep257 .parse (StringIO (code ), 'dummy.py' )
30
27
decorators = module .children [0 ].decorators
31
28
@@ -34,19 +31,17 @@ class Foo:
34
31
assert '' == decorators [0 ].arguments
35
32
36
33
def test_parse_class_decorators (self ):
37
- """
38
- Class decorators are accumulated, together with they arguments.
39
- """
40
- code = """
41
- @first_decorator
42
- @second.decorator(argument)
43
- @third.multi.line(
44
- decorator,
45
- key=value,
46
- )
47
- class Foo:
48
- pass
49
- """
34
+ """Class decorators are accumulated together with their arguments."""
35
+ code = textwrap .dedent ("""\
36
+ @first_decorator
37
+ @second.decorator(argument)
38
+ @third.multi.line(
39
+ decorator,
40
+ key=value,
41
+ )
42
+ class Foo:
43
+ pass
44
+ """ )
50
45
51
46
module = pep257 .parse (StringIO (code ), 'dummy.py' )
52
47
defined_class = module .children [0 ]
@@ -61,17 +56,15 @@ class Foo:
61
56
assert 'decorator,key=value,' == decorators [2 ].arguments
62
57
63
58
def test_parse_class_nested_decorator (self ):
64
- """
65
- Class decorator is recorded even for nested classes.
66
- """
67
- code = """
68
- @parent_decorator
69
- class Foo:
70
- pass
71
- @first_decorator
72
- class NestedClass:
73
- pass
74
- """
59
+ """Class decorator is recorded even for nested classes."""
60
+ code = textwrap .dedent ("""\
61
+ @parent_decorator
62
+ class Foo:
63
+ pass
64
+ @first_decorator
65
+ class NestedClass:
66
+ pass
67
+ """ )
75
68
module = pep257 .parse (StringIO (code ), 'dummy.py' )
76
69
nested_class = module .children [0 ].children [0 ]
77
70
decorators = nested_class .decorators
@@ -81,15 +74,13 @@ class NestedClass:
81
74
assert '' == decorators [0 ].arguments
82
75
83
76
def test_parse_method_single_decorator (self ):
84
- """
85
- Method decorators are accumulated.
86
- """
87
- code = """
88
- class Foo:
89
- @first_decorator
90
- def method(self):
91
- pass
92
- """
77
+ """Method decorators are accumulated."""
78
+ code = textwrap .dedent ("""\
79
+ class Foo:
80
+ @first_decorator
81
+ def method(self):
82
+ pass
83
+ """ )
93
84
94
85
module = pep257 .parse (StringIO (code ), 'dummy.py' )
95
86
defined_class = module .children [0 ]
@@ -100,20 +91,18 @@ def method(self):
100
91
assert '' == decorators [0 ].arguments
101
92
102
93
def test_parse_method_decorators (self ):
103
- """
104
- Method decorators are accumulated.
105
- """
106
- code = """
107
- class Foo:
108
- @first_decorator
109
- @second.decorator(argument)
110
- @third.multi.line(
111
- decorator,
112
- key=value,
113
- )
114
- def method(self):
115
- pass
116
- """
94
+ """Multiple method decorators are accumulated along with their args."""
95
+ code = textwrap .dedent ("""\
96
+ class Foo:
97
+ @first_decorator
98
+ @second.decorator(argument)
99
+ @third.multi.line(
100
+ decorator,
101
+ key=value,
102
+ )
103
+ def method(self):
104
+ pass
105
+ """ )
117
106
118
107
module = pep257 .parse (StringIO (code ), 'dummy.py' )
119
108
defined_class = module .children [0 ]
@@ -128,13 +117,12 @@ def method(self):
128
117
assert 'decorator,key=value,' == decorators [2 ].arguments
129
118
130
119
def test_parse_function_decorator (self ):
131
- """
132
- It accumulates decorators for functions.
133
- """
134
- code = """@first_decorator
135
- def some_method(self):
136
- pass
137
- """
120
+ """A function decorator is also accumulated."""
121
+ code = textwrap .dedent ("""\
122
+ @first_decorator
123
+ def some_method(self):
124
+ pass
125
+ """ )
138
126
139
127
module = pep257 .parse (StringIO (code ), 'dummy.py' )
140
128
decorators = module .children [0 ].decorators
@@ -144,17 +132,15 @@ def some_method(self):
144
132
assert '' == decorators [0 ].arguments
145
133
146
134
def test_parse_method_nested_decorator (self ):
147
- """
148
- Method decorators are accumulated for nested methods.
149
- """
150
- code = """
151
- class Foo:
152
- @parent_decorator
153
- def method(self):
154
- @first_decorator
155
- def nested_method(arg):
156
- pass
157
- """
135
+ """Method decorators are accumulated for nested methods."""
136
+ code = textwrap .dedent ("""\
137
+ class Foo:
138
+ @parent_decorator
139
+ def method(self):
140
+ @first_decorator
141
+ def nested_method(arg):
142
+ pass
143
+ """ )
158
144
159
145
module = pep257 .parse (StringIO (code ), 'dummy.py' )
160
146
defined_class = module .children [0 ]
@@ -166,53 +152,25 @@ def nested_method(arg):
166
152
167
153
168
154
class TestMethod :
169
- """
170
- Unit test for Method class.
171
- """
155
+ """Unit test for Method class."""
172
156
173
157
def makeMethod (self , name = 'someMethodName' ):
174
- """
175
- Return a simple method instance.
176
- """
158
+ """Return a simple method instance."""
177
159
children = []
178
160
all = ['ClassName' ]
179
- source = 'class ClassName:\n def %s(self):\n ' % (name )
180
-
181
- module = pep257 .Module (
182
- 'module_name' ,
183
- source ,
184
- 0 ,
185
- 1 ,
186
- [],
187
- 'Docstring for module' ,
188
- [],
189
- None ,
190
- all ,
191
- )
192
-
193
- parent = pep257 .Class (
194
- 'ClassName' ,
195
- source ,
196
- 0 ,
197
- 1 ,
198
- [],
199
- 'Docstring for class' ,
200
- children ,
201
- module ,
202
- all ,
203
- )
204
-
205
- return pep257 .Method (
206
- name ,
207
- source ,
208
- 0 ,
209
- 1 ,
210
- [],
211
- 'Docstring for method' ,
212
- children ,
213
- parent ,
214
- all ,
215
- )
161
+ source = textwrap .dedent ("""\
162
+ class ClassName:
163
+ def %s(self):
164
+ """ % (name ))
165
+
166
+ module = pep257 .Module ('module_name' , source , 0 , 1 , [],
167
+ 'Docstring for module' , [], None , all )
168
+
169
+ cls = pep257 .Class ('ClassName' , source , 0 , 1 , [],
170
+ 'Docstring for class' , children , module , all )
171
+
172
+ return pep257 .Method (name , source , 0 , 1 , [],
173
+ 'Docstring for method' , children , cls , all )
216
174
217
175
def test_is_public_normal (self ):
218
176
"""Methods are normally public, even if decorated."""
@@ -240,3 +198,13 @@ def test_is_public_deleter(self):
240
198
]
241
199
242
200
assert not method .is_public
201
+
202
+ def test_is_public_trick (self ):
203
+ """Common prefix does not necessarily indicate private."""
204
+ method = self .makeMethod ("foo" )
205
+ method .decorators = [
206
+ pep257 .Decorator ('foobar' , []),
207
+ pep257 .Decorator ('foobar.baz' , []),
208
+ ]
209
+
210
+ assert method .is_public
0 commit comments