|
| 1 | +"""Unit test for pep257 module decorator handling. |
| 2 | +
|
| 3 | +Use tox or py.test to run the test suite. |
| 4 | +""" |
| 5 | + |
| 6 | +try: |
| 7 | + from StringIO import StringIO |
| 8 | +except ImportError: |
| 9 | + from io import StringIO |
| 10 | + |
| 11 | +import textwrap |
| 12 | + |
| 13 | +import pep257 |
| 14 | + |
| 15 | + |
| 16 | +class TestParser: |
| 17 | + """Check parsing of Python source code.""" |
| 18 | + |
| 19 | + def test_parse_class_single_decorator(self): |
| 20 | + """Class decorator is recorded in class instance.""" |
| 21 | + code = textwrap.dedent("""\ |
| 22 | + @first_decorator |
| 23 | + class Foo: |
| 24 | + pass |
| 25 | + """) |
| 26 | + module = pep257.parse(StringIO(code), 'dummy.py') |
| 27 | + decorators = module.children[0].decorators |
| 28 | + |
| 29 | + assert 1 == len(decorators) |
| 30 | + assert 'first_decorator' == decorators[0].name |
| 31 | + assert '' == decorators[0].arguments |
| 32 | + |
| 33 | + def test_parse_class_decorators(self): |
| 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 | + """) |
| 45 | + |
| 46 | + module = pep257.parse(StringIO(code), 'dummy.py') |
| 47 | + defined_class = module.children[0] |
| 48 | + decorators = defined_class.decorators |
| 49 | + |
| 50 | + assert 3 == len(decorators) |
| 51 | + assert 'first_decorator' == decorators[0].name |
| 52 | + assert '' == decorators[0].arguments |
| 53 | + assert 'second.decorator' == decorators[1].name |
| 54 | + assert 'argument' == decorators[1].arguments |
| 55 | + assert 'third.multi.line' == decorators[2].name |
| 56 | + assert 'decorator,key=value,' == decorators[2].arguments |
| 57 | + |
| 58 | + def test_parse_class_nested_decorator(self): |
| 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 | + """) |
| 68 | + module = pep257.parse(StringIO(code), 'dummy.py') |
| 69 | + nested_class = module.children[0].children[0] |
| 70 | + decorators = nested_class.decorators |
| 71 | + |
| 72 | + assert 1 == len(decorators) |
| 73 | + assert 'first_decorator' == decorators[0].name |
| 74 | + assert '' == decorators[0].arguments |
| 75 | + |
| 76 | + def test_parse_method_single_decorator(self): |
| 77 | + """Method decorators are accumulated.""" |
| 78 | + code = textwrap.dedent("""\ |
| 79 | + class Foo: |
| 80 | + @first_decorator |
| 81 | + def method(self): |
| 82 | + pass |
| 83 | + """) |
| 84 | + |
| 85 | + module = pep257.parse(StringIO(code), 'dummy.py') |
| 86 | + defined_class = module.children[0] |
| 87 | + decorators = defined_class.children[0].decorators |
| 88 | + |
| 89 | + assert 1 == len(decorators) |
| 90 | + assert 'first_decorator' == decorators[0].name |
| 91 | + assert '' == decorators[0].arguments |
| 92 | + |
| 93 | + def test_parse_method_decorators(self): |
| 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 | + """) |
| 106 | + |
| 107 | + module = pep257.parse(StringIO(code), 'dummy.py') |
| 108 | + defined_class = module.children[0] |
| 109 | + decorators = defined_class.children[0].decorators |
| 110 | + |
| 111 | + assert 3 == len(decorators) |
| 112 | + assert 'first_decorator' == decorators[0].name |
| 113 | + assert '' == decorators[0].arguments |
| 114 | + assert 'second.decorator' == decorators[1].name |
| 115 | + assert 'argument' == decorators[1].arguments |
| 116 | + assert 'third.multi.line' == decorators[2].name |
| 117 | + assert 'decorator,key=value,' == decorators[2].arguments |
| 118 | + |
| 119 | + def test_parse_function_decorator(self): |
| 120 | + """A function decorator is also accumulated.""" |
| 121 | + code = textwrap.dedent("""\ |
| 122 | + @first_decorator |
| 123 | + def some_method(self): |
| 124 | + pass |
| 125 | + """) |
| 126 | + |
| 127 | + module = pep257.parse(StringIO(code), 'dummy.py') |
| 128 | + decorators = module.children[0].decorators |
| 129 | + |
| 130 | + assert 1 == len(decorators) |
| 131 | + assert 'first_decorator' == decorators[0].name |
| 132 | + assert '' == decorators[0].arguments |
| 133 | + |
| 134 | + def test_parse_method_nested_decorator(self): |
| 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 | + """) |
| 144 | + |
| 145 | + module = pep257.parse(StringIO(code), 'dummy.py') |
| 146 | + defined_class = module.children[0] |
| 147 | + decorators = defined_class.children[0].children[0].decorators |
| 148 | + |
| 149 | + assert 1 == len(decorators) |
| 150 | + assert 'first_decorator' == decorators[0].name |
| 151 | + assert '' == decorators[0].arguments |
| 152 | + |
| 153 | + |
| 154 | +class TestMethod: |
| 155 | + """Unit test for Method class.""" |
| 156 | + |
| 157 | + def makeMethod(self, name='someMethodName'): |
| 158 | + """Return a simple method instance.""" |
| 159 | + children = [] |
| 160 | + all = ['ClassName'] |
| 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) |
| 174 | + |
| 175 | + def test_is_public_normal(self): |
| 176 | + """Methods are normally public, even if decorated.""" |
| 177 | + method = self.makeMethod('methodName') |
| 178 | + method.decorators = [pep257.Decorator('some_decorator', [])] |
| 179 | + |
| 180 | + assert method.is_public |
| 181 | + |
| 182 | + def test_is_public_setter(self): |
| 183 | + """Setter methods are considered private.""" |
| 184 | + method = self.makeMethod('methodName') |
| 185 | + method.decorators = [ |
| 186 | + pep257.Decorator('some_decorator', []), |
| 187 | + pep257.Decorator('methodName.setter', []), |
| 188 | + ] |
| 189 | + |
| 190 | + assert not method.is_public |
| 191 | + |
| 192 | + def test_is_public_deleter(self): |
| 193 | + """Deleter methods are also considered private.""" |
| 194 | + method = self.makeMethod('methodName') |
| 195 | + method.decorators = [ |
| 196 | + pep257.Decorator('methodName.deleter', []), |
| 197 | + pep257.Decorator('another_decorator', []), |
| 198 | + ] |
| 199 | + |
| 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