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__ |
Georg Brandl | b2afe85 | 2006-06-09 20:43:48 +0000 | [diff] [blame^] | 18 | if modfile.endswith(('c', 'o')): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 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]) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 34 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 35 | def istest(self, predicate, exp): |
| 36 | obj = eval(exp) |
| 37 | self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp)) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 38 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 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) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 47 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 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) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 69 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 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:], |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 79 | (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0)) |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 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 |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 116 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 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)) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 129 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 130 | class TestRetrievingSourceCode(GetSourceBase): |
| 131 | fodderFile = mod |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 132 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 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 | ]) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 150 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 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): |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 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 | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 190 | class TestOneliners(GetSourceBase): |
| 191 | fodderFile = mod2 |
| 192 | def test_oneline_lambda(self): |
| 193 | # Test inspect.getsource with a one-line lambda function. |
| 194 | self.assertSourceEqual(mod2.oll, 25, 25) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 195 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 196 | def test_threeline_lambda(self): |
| 197 | # Test inspect.getsource with a three-line lambda function, |
| 198 | # where the second and third lines are _not_ indented. |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 199 | self.assertSourceEqual(mod2.tll, 28, 30) |
| 200 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 201 | def test_twoline_indented_lambda(self): |
| 202 | # Test inspect.getsource with a two-line lambda function, |
| 203 | # where the second line _is_ indented. |
| 204 | self.assertSourceEqual(mod2.tlli, 33, 34) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 205 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 206 | def test_onelinefunc(self): |
| 207 | # Test inspect.getsource with a regular one-line function. |
| 208 | self.assertSourceEqual(mod2.onelinefunc, 37, 37) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 209 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 210 | def test_manyargs(self): |
| 211 | # Test inspect.getsource with a regular function where |
| 212 | # the arguments are on two lines and _not_ indented and |
| 213 | # the body on the second line with the last arguments. |
| 214 | self.assertSourceEqual(mod2.manyargs, 40, 41) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 215 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 216 | def test_twolinefunc(self): |
| 217 | # Test inspect.getsource with a regular function where |
| 218 | # the body is on two lines, following the argument list and |
| 219 | # continued on the next line by a \\. |
| 220 | self.assertSourceEqual(mod2.twolinefunc, 44, 45) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 221 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 222 | def test_lambda_in_list(self): |
| 223 | # Test inspect.getsource with a one-line lambda function |
| 224 | # defined in a list, indented. |
| 225 | self.assertSourceEqual(mod2.a[1], 49, 49) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 226 | |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 227 | def test_anonymous(self): |
| 228 | # Test inspect.getsource with a lambda function defined |
| 229 | # as argument to another function. |
| 230 | self.assertSourceEqual(mod2.anonymous, 55, 55) |
| 231 | |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 232 | class TestBuggyCases(GetSourceBase): |
| 233 | fodderFile = mod2 |
| 234 | |
| 235 | def test_with_comment(self): |
| 236 | self.assertSourceEqual(mod2.with_comment, 58, 59) |
| 237 | |
| 238 | def test_multiline_sig(self): |
| 239 | self.assertSourceEqual(mod2.multiline_sig[0], 63, 64) |
| 240 | |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 241 | def test_nested_class(self): |
| 242 | self.assertSourceEqual(mod2.func69().func71, 71, 72) |
| 243 | |
| 244 | def test_one_liner_followed_by_non_name(self): |
| 245 | self.assertSourceEqual(mod2.func77, 77, 77) |
| 246 | |
| 247 | def test_one_liner_dedent_non_name(self): |
| 248 | self.assertSourceEqual(mod2.cls82.func83, 83, 83) |
| 249 | |
| 250 | def test_with_comment_instead_of_docstring(self): |
| 251 | self.assertSourceEqual(mod2.func88, 88, 90) |
| 252 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 253 | # Helper for testing classify_class_attrs. |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 254 | def attrs_wo_objs(cls): |
| 255 | return [t[:3] for t in inspect.classify_class_attrs(cls)] |
| 256 | |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 257 | class TestClassesAndFunctions(unittest.TestCase): |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 258 | def test_classic_mro(self): |
| 259 | # Test classic-class method resolution order. |
| 260 | class A: pass |
| 261 | class B(A): pass |
| 262 | class C(A): pass |
| 263 | class D(B, C): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 264 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 265 | expected = (D, B, A, C) |
| 266 | got = inspect.getmro(D) |
| 267 | self.assertEqual(expected, got) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 268 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 269 | def test_newstyle_mro(self): |
| 270 | # The same w/ new-class MRO. |
| 271 | class A(object): pass |
| 272 | class B(A): pass |
| 273 | class C(A): pass |
| 274 | class D(B, C): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 275 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 276 | expected = (D, B, C, A, object) |
| 277 | got = inspect.getmro(D) |
| 278 | self.assertEqual(expected, got) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 279 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 280 | def assertArgSpecEquals(self, routine, args_e, varargs_e = None, |
| 281 | varkw_e = None, defaults_e = None, |
| 282 | formatted = None): |
| 283 | args, varargs, varkw, defaults = inspect.getargspec(routine) |
| 284 | self.assertEqual(args, args_e) |
| 285 | self.assertEqual(varargs, varargs_e) |
| 286 | self.assertEqual(varkw, varkw_e) |
| 287 | self.assertEqual(defaults, defaults_e) |
| 288 | if formatted is not None: |
| 289 | self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults), |
| 290 | formatted) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 291 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 292 | def test_getargspec(self): |
| 293 | self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted = '(x, y)') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 294 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 295 | self.assertArgSpecEquals(mod.spam, |
| 296 | ['a', 'b', 'c', 'd', ['e', ['f']]], |
| 297 | 'g', 'h', (3, (4, (5,))), |
| 298 | '(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)') |
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 | def test_getargspec_method(self): |
| 301 | class A(object): |
| 302 | def m(self): |
| 303 | pass |
| 304 | self.assertArgSpecEquals(A.m, ['self']) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 305 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 306 | def test_getargspec_sublistofone(self): |
Neal Norwitz | 33b730e | 2006-03-27 08:58:23 +0000 | [diff] [blame] | 307 | def sublistOfOne((foo,)): return 1 |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 308 | self.assertArgSpecEquals(sublistOfOne, [['foo']]) |
| 309 | |
Neal Norwitz | 33b730e | 2006-03-27 08:58:23 +0000 | [diff] [blame] | 310 | def fakeSublistOfOne((foo)): return 1 |
| 311 | self.assertArgSpecEquals(fakeSublistOfOne, ['foo']) |
| 312 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 313 | def test_classify_oldstyle(self): |
| 314 | class A: |
| 315 | def s(): pass |
| 316 | s = staticmethod(s) |
| 317 | |
| 318 | def c(cls): pass |
| 319 | c = classmethod(c) |
| 320 | |
| 321 | def getp(self): pass |
| 322 | p = property(getp) |
| 323 | |
| 324 | def m(self): pass |
| 325 | |
| 326 | def m1(self): pass |
| 327 | |
| 328 | datablob = '1' |
| 329 | |
| 330 | attrs = attrs_wo_objs(A) |
| 331 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 332 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 333 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 334 | self.assert_(('m', 'method', A) in attrs, 'missing plain method') |
| 335 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 336 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
| 337 | |
| 338 | class B(A): |
| 339 | def m(self): pass |
| 340 | |
| 341 | attrs = attrs_wo_objs(B) |
| 342 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 343 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 344 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 345 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 346 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 347 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 348 | |
| 349 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 350 | class C(A): |
| 351 | def m(self): pass |
| 352 | def c(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 353 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 354 | attrs = attrs_wo_objs(C) |
| 355 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 356 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 357 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 358 | self.assert_(('m', 'method', C) in attrs, 'missing plain method') |
| 359 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 360 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 361 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 362 | class D(B, C): |
| 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', 'class method', A) in attrs, 'missing class 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') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 372 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 373 | # Repeat all that, but w/ new-style classes. |
| 374 | def test_classify_newstyle(self): |
| 375 | class A(object): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 376 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 377 | def s(): pass |
| 378 | s = staticmethod(s) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 379 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 380 | def c(cls): pass |
| 381 | c = classmethod(c) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 382 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 383 | def getp(self): pass |
| 384 | p = property(getp) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 385 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 386 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 387 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 388 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 389 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 390 | datablob = '1' |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 391 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 392 | attrs = attrs_wo_objs(A) |
| 393 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 394 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 395 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 396 | self.assert_(('m', 'method', A) in attrs, 'missing plain method') |
| 397 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 398 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 399 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 400 | class B(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 401 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 402 | def m(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 403 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 404 | attrs = attrs_wo_objs(B) |
| 405 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 406 | self.assert_(('c', 'class method', A) in attrs, 'missing class method') |
| 407 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 408 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 409 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 410 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 411 | |
| 412 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 413 | class C(A): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 414 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 415 | def m(self): pass |
| 416 | def c(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 417 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 418 | attrs = attrs_wo_objs(C) |
| 419 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 420 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 421 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 422 | self.assert_(('m', 'method', C) in attrs, 'missing plain method') |
| 423 | self.assert_(('m1', 'method', A) in attrs, 'missing plain method') |
| 424 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 425 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 426 | class D(B, C): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 427 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 428 | def m1(self): pass |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 429 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 430 | attrs = attrs_wo_objs(D) |
| 431 | self.assert_(('s', 'static method', A) in attrs, 'missing static method') |
| 432 | self.assert_(('c', 'method', C) in attrs, 'missing plain method') |
| 433 | self.assert_(('p', 'property', A) in attrs, 'missing property') |
| 434 | self.assert_(('m', 'method', B) in attrs, 'missing plain method') |
| 435 | self.assert_(('m1', 'method', D) in attrs, 'missing plain method') |
| 436 | self.assert_(('datablob', 'data', A) in attrs, 'missing data') |
Jeremy Hylton | c4bf5ed | 2003-06-27 18:43:12 +0000 | [diff] [blame] | 437 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 438 | def test_main(): |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 439 | run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners, |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 440 | TestBuggyCases, |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 441 | TestInterpreterStack, TestClassesAndFunctions, TestPredicates) |
Martin v. Löwis | 893ffa4 | 2003-10-31 15:35:53 +0000 | [diff] [blame] | 442 | |
Johannes Gijsbers | cb9015d | 2004-12-12 16:20:22 +0000 | [diff] [blame] | 443 | if __name__ == "__main__": |
| 444 | test_main() |