Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 1 | import sys |
| 2 | import unittest |
| 3 | import inspect |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 4 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 5 | from test.test_support import TESTFN, run_unittest |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 6 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 7 | from test import inspect_fodder as mod |
| 8 | from test import inspect_fodder2 as mod2 |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 9 | |
| 10 | # Functions tested in this suite: |
| 11 | # ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode, |
| 12 | # isbuiltin, isroutine, getmembers, getdoc, getfile, getmodule, |
| 13 | # getsourcefile, getcomments, getsource, getclasstree, getargspec, |
| 14 | # getargvalues, formatargspec, formatargvalues, currentframe, stack, trace |
Martin v. Löwis | e59e2ba | 2003-05-03 09:09:02 +0000 | [diff] [blame] | 15 | # isdatadescriptor |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 16 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 17 | modfile = mod.__file__ |
| 18 | if modfile.endswith('c') or modfile.endswith('o'): |
| 19 | modfile = modfile[:-1] |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 20 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 21 | import __builtin__ |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 22 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 23 | try: |
| 24 | 1/0 |
| 25 | except: |
| 26 | tb = sys.exc_traceback |
| 27 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 28 | git = mod.StupidGit() |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 29 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 30 | class IsTestBase(unittest.TestCase): |
| 31 | predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode, |
| 32 | inspect.isframe, inspect.isfunction, inspect.ismethod, |
| 33 | inspect.ismodule, inspect.istraceback]) |
| 34 | |
| 35 | def istest(self, predicate, exp): |
| 36 | obj = eval(exp) |
| 37 | self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp)) |
| 38 | |
| 39 | for other in self.predicates - set([predicate]): |
| 40 | self.failIf(other(obj), 'not %s(%s)' % (other.__name__, exp)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 41 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 42 | class TestPredicates(IsTestBase): |
| 43 | def test_eleven(self): |
| 44 | # Doc/lib/libinspect.tex claims there are 11 such functions |
| 45 | count = len(filter(lambda x:x.startswith('is'), dir(inspect))) |
| 46 | self.assertEqual(count, 11, "There are %d (not 11) is* functions" % count) |
| 47 | |
| 48 | def test_excluding_predicates(self): |
| 49 | self.istest(inspect.isbuiltin, 'sys.exit') |
| 50 | self.istest(inspect.isbuiltin, '[].append') |
| 51 | self.istest(inspect.isclass, 'mod.StupidGit') |
| 52 | self.istest(inspect.iscode, 'mod.spam.func_code') |
| 53 | self.istest(inspect.isframe, 'tb.tb_frame') |
| 54 | self.istest(inspect.isfunction, 'mod.spam') |
| 55 | self.istest(inspect.ismethod, 'mod.StupidGit.abuse') |
| 56 | self.istest(inspect.ismethod, 'git.argue') |
| 57 | self.istest(inspect.ismodule, 'mod') |
| 58 | self.istest(inspect.istraceback, 'tb') |
| 59 | self.istest(inspect.isdatadescriptor, '__builtin__.file.closed') |
| 60 | self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 61 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 62 | def test_isroutine(self): |
| 63 | self.assert_(inspect.isroutine(mod.spam)) |
| 64 | self.assert_(inspect.isroutine([].count)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 65 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 66 | class TestInterpreterStack(IsTestBase): |
| 67 | def __init__(self, *args, **kwargs): |
| 68 | unittest.TestCase.__init__(self, *args, **kwargs) |
| 69 | |
| 70 | git.abuse(7, 8, 9) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 71 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 72 | def test_abuse_done(self): |
| 73 | self.istest(inspect.istraceback, 'git.ex[2]') |
| 74 | self.istest(inspect.isframe, 'mod.fr') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 75 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 76 | def test_stack(self): |
| 77 | self.assert_(len(mod.st) >= 5) |
| 78 | self.assertEqual(mod.st[0][1:], |
| 79 | (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0)) |
| 80 | self.assertEqual(mod.st[1][1:], |
| 81 | (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0)) |
| 82 | self.assertEqual(mod.st[2][1:], |
| 83 | (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0)) |
| 84 | self.assertEqual(mod.st[3][1:], |
| 85 | (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 86 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 87 | def test_trace(self): |
| 88 | self.assertEqual(len(git.tr), 3) |
| 89 | self.assertEqual(git.tr[0][1:], (modfile, 43, 'argue', |
| 90 | [' spam(a, b, c)\n'], 0)) |
| 91 | self.assertEqual(git.tr[1][1:], (modfile, 9, 'spam', |
| 92 | [' eggs(b + d, c + f)\n'], 0)) |
| 93 | self.assertEqual(git.tr[2][1:], (modfile, 18, 'eggs', |
| 94 | [' q = y / 0\n'], 0)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 95 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 96 | def test_frame(self): |
| 97 | args, varargs, varkw, locals = inspect.getargvalues(mod.fr) |
| 98 | self.assertEqual(args, ['x', 'y']) |
| 99 | self.assertEqual(varargs, None) |
| 100 | self.assertEqual(varkw, None) |
| 101 | self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14}) |
| 102 | self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), |
| 103 | '(x=11, y=14)') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 104 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 105 | def test_previous_frame(self): |
| 106 | args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back) |
| 107 | self.assertEqual(args, ['a', 'b', 'c', 'd', ['e', ['f']]]) |
| 108 | self.assertEqual(varargs, 'g') |
| 109 | self.assertEqual(varkw, 'h') |
| 110 | self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), |
| 111 | '(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 112 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 113 | class GetSourceBase(unittest.TestCase): |
| 114 | # Subclasses must override. |
| 115 | fodderFile = None |
| 116 | |
| 117 | def __init__(self, *args, **kwargs): |
| 118 | unittest.TestCase.__init__(self, *args, **kwargs) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 119 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 120 | self.source = file(inspect.getsourcefile(self.fodderFile)).read() |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 121 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 122 | def sourcerange(self, top, bottom): |
| 123 | lines = self.source.split("\n") |
| 124 | return "\n".join(lines[top-1:bottom]) + "\n" |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 125 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 126 | def assertSourceEqual(self, obj, top, bottom): |
| 127 | self.assertEqual(inspect.getsource(obj), |
| 128 | self.sourcerange(top, bottom)) |
| 129 | |
| 130 | class TestRetrievingSourceCode(GetSourceBase): |
| 131 | fodderFile = mod |
| 132 | |
| 133 | def test_getclasses(self): |
| 134 | classes = inspect.getmembers(mod, inspect.isclass) |
| 135 | self.assertEqual(classes, |
| 136 | [('FesteringGob', mod.FesteringGob), |
| 137 | ('MalodorousPervert', mod.MalodorousPervert), |
| 138 | ('ParrotDroppings', mod.ParrotDroppings), |
| 139 | ('StupidGit', mod.StupidGit)]) |
| 140 | tree = inspect.getclasstree([cls[1] for cls in classes], 1) |
| 141 | self.assertEqual(tree, |
| 142 | [(mod.ParrotDroppings, ()), |
| 143 | (mod.StupidGit, ()), |
| 144 | [(mod.MalodorousPervert, (mod.StupidGit,)), |
| 145 | [(mod.FesteringGob, (mod.MalodorousPervert, |
| 146 | mod.ParrotDroppings)) |
| 147 | ] |
| 148 | ] |
| 149 | ]) |
| 150 | |
| 151 | def test_getfunctions(self): |
| 152 | functions = inspect.getmembers(mod, inspect.isfunction) |
| 153 | self.assertEqual(functions, [('eggs', mod.eggs), |
| 154 | ('spam', mod.spam)]) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 155 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 156 | def test_getdoc(self): |
| 157 | self.assertEqual(inspect.getdoc(mod), 'A module docstring.') |
| 158 | self.assertEqual(inspect.getdoc(mod.StupidGit), |
| 159 | 'A longer,\n\nindented\n\ndocstring.') |
| 160 | self.assertEqual(inspect.getdoc(git.abuse), |
| 161 | 'Another\n\ndocstring\n\ncontaining\n\ntabs') |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 162 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 163 | def test_getcomments(self): |
| 164 | self.assertEqual(inspect.getcomments(mod), '# line 1\n') |
| 165 | self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n') |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 166 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 167 | def test_getmodule(self): |
| 168 | self.assertEqual(inspect.getmodule(mod.StupidGit), mod) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 169 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 170 | def test_getsource(self): |
| 171 | self.assertSourceEqual(git.abuse, 29, 39) |
| 172 | self.assertSourceEqual(mod.StupidGit, 21, 46) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 173 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 174 | def test_getsourcefile(self): |
| 175 | self.assertEqual(inspect.getsourcefile(mod.spam), modfile) |
| 176 | self.assertEqual(inspect.getsourcefile(git.abuse), modfile) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 177 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 178 | def test_getfile(self): |
| 179 | self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 180 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 181 | class TestDecorators(GetSourceBase): |
| 182 | fodderFile = mod2 |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 183 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 184 | def test_wrapped_decorator(self): |
| 185 | self.assertSourceEqual(mod2.wrapped, 14, 17) |
Johannes Gijsbers | c473c99 | 2004-08-18 12:40:31 +0000 | [diff] [blame] | 186 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 187 | def test_replacing_decorator(self): |
| 188 | self.assertSourceEqual(mod2.gone, 9, 10) |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 189 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 190 | # Helper for testing classify_class_attrs. |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 191 | def attrs_wo_objs(cls): |
| 192 | return [t[:3] for t in inspect.classify_class_attrs(cls)] |
| 193 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 194 | class TestClassesAndFunctions(unittest.TestCase): |
| 195 | def test_classic_mro(self): |
| 196 | # Test classic-class method resolution order. |
| 197 | class A: pass |
| 198 | class B(A): pass |
| 199 | class C(A): pass |
| 200 | class D(B, C): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 201 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 202 | expected = (D, B, A, C) |
| 203 | got = inspect.getmro(D) |
| 204 | self.assertEqual(expected, got) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 205 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 206 | def test_newstyle_mro(self): |
| 207 | # The same w/ new-class MRO. |
| 208 | class A(object): pass |
| 209 | class B(A): pass |
| 210 | class C(A): pass |
| 211 | class D(B, C): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 212 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 213 | expected = (D, B, C, A, object) |
| 214 | got = inspect.getmro(D) |
| 215 | self.assertEqual(expected, got) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 216 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 217 | def assertArgSpecEquals(self, routine, args_e, varargs_e = None, |
| 218 | varkw_e = None, defaults_e = None, |
| 219 | formatted = None): |
| 220 | args, varargs, varkw, defaults = inspect.getargspec(routine) |
| 221 | self.assertEqual(args, args_e) |
| 222 | self.assertEqual(varargs, varargs_e) |
| 223 | self.assertEqual(varkw, varkw_e) |
| 224 | self.assertEqual(defaults, defaults_e) |
| 225 | if formatted is not None: |
| 226 | self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults), |
| 227 | formatted) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 228 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 229 | def test_getargspec(self): |
| 230 | self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted = '(x, y)') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 231 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 232 | self.assertArgSpecEquals(mod.spam, |
| 233 | ['a', 'b', 'c', 'd', ['e', ['f']]], |
| 234 | 'g', 'h', (3, (4, (5,))), |
| 235 | '(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 236 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 237 | def test_getargspec_method(self): |
| 238 | class A(object): |
| 239 | def m(self): |
| 240 | pass |
| 241 | self.assertArgSpecEquals(A.m, ['self']) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 242 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 243 | def test_getargspec_sublistofone(self): |
| 244 | def sublistOfOne((foo)): return 1 |
| 245 | |
| 246 | self.assertArgSpecEquals(sublistOfOne, [['foo']]) |
| 247 | |
| 248 | def test_classify_oldstyle(self): |
| 249 | class A: |
| 250 | def s(): pass |
| 251 | s = staticmethod(s) |
| 252 | |
| 253 | def c(cls): pass |
| 254 | c = classmethod(c) |
| 255 | |
| 256 | def getp(self): pass |
| 257 | p = property(getp) |
| 258 | |
| 259 | def m(self): pass |
| 260 | |
| 261 | def m1(self): pass |
| 262 | |
| 263 | datablob = '1' |
| 264 | |
| 265 | attrs = attrs_wo_objs(A) |
| 266 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 267 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 268 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 269 | self.assert_(('m', 'method', A) in attrs, 'missing plain method') |
| 270 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 271 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
| 272 | |
| 273 | class B(A): |
| 274 | def m(self): pass |
| 275 | |
| 276 | attrs = attrs_wo_objs(B) |
| 277 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 278 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 279 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 280 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 281 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 282 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 283 | |
| 284 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 285 | class C(A): |
| 286 | def m(self): pass |
| 287 | def c(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 288 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 289 | attrs = attrs_wo_objs(C) |
| 290 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 291 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 292 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 293 | self.assert_(('m', 'method', C) in attrs, 'missing plain method') |
| 294 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 295 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 296 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 297 | class D(B, C): |
| 298 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 299 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 300 | attrs = attrs_wo_objs(D) |
| 301 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 302 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 303 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 304 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 305 | self.assert_(('m1', 'method', D) in attrs, 'missing plain method') |
| 306 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 307 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 308 | # Repeat all that, but w/ new-style classes. |
| 309 | def test_classify_newstyle(self): |
| 310 | class A(object): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 311 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 312 | def s(): pass |
| 313 | s = staticmethod(s) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 314 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 315 | def c(cls): pass |
| 316 | c = classmethod(c) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 317 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 318 | def getp(self): pass |
| 319 | p = property(getp) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 320 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 321 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 322 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 323 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 324 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 325 | datablob = '1' |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 326 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 327 | attrs = attrs_wo_objs(A) |
| 328 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 329 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 330 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 331 | self.assert_(('m', 'method', A) in attrs, 'missing plain method') |
| 332 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 333 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 334 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 335 | class B(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 336 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 337 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 338 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 339 | attrs = attrs_wo_objs(B) |
| 340 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 341 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 342 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 343 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 344 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 345 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 346 | |
| 347 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 348 | class C(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 349 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 350 | def m(self): pass |
| 351 | def c(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 352 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 353 | attrs = attrs_wo_objs(C) |
| 354 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 355 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 356 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 357 | self.assert_(('m', 'method', C) in attrs, 'missing plain method') |
| 358 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 359 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 360 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 361 | class D(B, C): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 362 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 363 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 364 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 365 | attrs = attrs_wo_objs(D) |
| 366 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 367 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 368 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 369 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 370 | self.assert_(('m1', 'method', D) in attrs, 'missing plain method') |
| 371 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Jeremy Hylton | c4bf5ed | 2003-06-27 18:43:12 +0000 | [diff] [blame] | 372 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 373 | def test_main(): |
| 374 | run_unittest(TestDecorators, TestRetrievingSourceCode, |
| 375 | TestInterpreterStack, TestClassesAndFunctions, TestPredicates) |
Martin v. Löwis | 893ffa4 | 2003-10-31 15:35:53 +0000 | [diff] [blame] | 376 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame^] | 377 | if __name__ == "__main__": |
| 378 | test_main() |
| 379 | |