Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Test cases for pyclbr.py |
| 3 | Nick Mathewson |
| 4 | ''' |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test.support import run_unittest |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 6 | import sys |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 7 | from types import FunctionType, MethodType, BuiltinFunctionType |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 8 | import pyclbr |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 9 | from unittest import TestCase |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 10 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 11 | StaticMethodType = type(staticmethod(lambda: None)) |
| 12 | ClassMethodType = type(classmethod(lambda c: None)) |
| 13 | |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 14 | # Here we test the python class browser code. |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 15 | # |
| 16 | # The main function in this suite, 'testModule', compares the output |
| 17 | # of pyclbr with the introspected members of a module. Because pyclbr |
| 18 | # is imperfect (as designed), testModule is called with a set of |
| 19 | # members to ignore. |
| 20 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 21 | class PyclbrTest(TestCase): |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 22 | |
| 23 | def assertListEq(self, l1, l2, ignore): |
| 24 | ''' succeed iff {l1} - {ignore} == {l2} - {ignore} ''' |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 25 | missing = (set(l1) ^ set(l2)) - set(ignore) |
Raymond Hettinger | 91bbd9a | 2003-05-02 09:06:28 +0000 | [diff] [blame] | 26 | if missing: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 27 | print("l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore), file=sys.stderr) |
Raymond Hettinger | 91bbd9a | 2003-05-02 09:06:28 +0000 | [diff] [blame] | 28 | self.fail("%r missing" % missing.pop()) |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 29 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 30 | def assertHasattr(self, obj, attr, ignore): |
| 31 | ''' succeed iff hasattr(obj,attr) or attr in ignore. ''' |
| 32 | if attr in ignore: return |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 33 | if not hasattr(obj, attr): print("???", attr) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 34 | self.assertTrue(hasattr(obj, attr), |
Tim Peters | 5e5ca56 | 2002-07-10 02:37:21 +0000 | [diff] [blame] | 35 | 'expected hasattr(%r, %r)' % (obj, attr)) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def assertHaskey(self, obj, key, ignore): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 39 | ''' succeed iff key in obj or key in ignore. ''' |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 40 | if key in ignore: return |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 41 | if key not in obj: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 42 | print("***",key, file=sys.stderr) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 43 | self.assertIn(key, obj) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 44 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 45 | def assertEqualsOrIgnored(self, a, b, ignore): |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 46 | ''' succeed iff a == b or a in ignore or b in ignore ''' |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 47 | if a not in ignore and b not in ignore: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 48 | self.assertEqual(a, b) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 49 | |
| 50 | def checkModule(self, moduleName, module=None, ignore=()): |
| 51 | ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds |
| 52 | to the actual module object, module. Any identifiers in |
| 53 | ignore are ignored. If no module is provided, the appropriate |
| 54 | module is loaded with __import__.''' |
| 55 | |
Guido van Rossum | d858f70 | 2006-04-21 09:17:15 +0000 | [diff] [blame] | 56 | ignore = set(ignore) | set(['object']) |
| 57 | |
Benjamin Peterson | 2a691a8 | 2008-03-31 01:51:45 +0000 | [diff] [blame] | 58 | if module is None: |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 59 | # Import it. |
| 60 | # ('<silly>' is to work around an API silliness in __import__) |
| 61 | module = __import__(moduleName, globals(), {}, ['<silly>']) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 62 | |
| 63 | dict = pyclbr.readmodule_ex(moduleName) |
| 64 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 65 | def ismethod(oclass, obj, name): |
| 66 | classdict = oclass.__dict__ |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 67 | if isinstance(obj, MethodType): |
| 68 | # could be a classmethod |
| 69 | if (not isinstance(classdict[name], ClassMethodType) or |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 70 | obj.__self__ is not oclass): |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 71 | return False |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 72 | elif not isinstance(obj, FunctionType): |
| 73 | return False |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 75 | objname = obj.__name__ |
| 76 | if objname.startswith("__") and not objname.endswith("__"): |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 77 | objname = "_%s%s" % (oclass.__name__, objname) |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 78 | return objname == name |
| 79 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 80 | # Make sure the toplevel functions and classes are the same. |
| 81 | for name, value in dict.items(): |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 82 | if name in ignore: |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 83 | continue |
| 84 | self.assertHasattr(module, name, ignore) |
| 85 | py_item = getattr(module, name) |
| 86 | if isinstance(value, pyclbr.Function): |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 87 | self.assertIsInstance(py_item, (FunctionType, BuiltinFunctionType)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 88 | if py_item.__module__ != moduleName: |
| 89 | continue # skip functions that came from somewhere else |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 90 | self.assertEqual(py_item.__module__, value.module) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 91 | else: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 92 | self.assertIsInstance(py_item, type) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 93 | if py_item.__module__ != moduleName: |
| 94 | continue # skip classes that came from somewhere else |
| 95 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 96 | real_bases = [base.__name__ for base in py_item.__bases__] |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 97 | pyclbr_bases = [ getattr(base, 'name', base) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 98 | for base in value.super ] |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 100 | try: |
| 101 | self.assertListEq(real_bases, pyclbr_bases, ignore) |
| 102 | except: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 103 | print("class=%s" % py_item, file=sys.stderr) |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 104 | raise |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 105 | |
| 106 | actualMethods = [] |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 107 | for m in py_item.__dict__.keys(): |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 108 | if ismethod(py_item, getattr(py_item, m), m): |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 109 | actualMethods.append(m) |
| 110 | foundMethods = [] |
| 111 | for m in value.methods.keys(): |
| 112 | if m[:2] == '__' and m[-2:] != '__': |
| 113 | foundMethods.append('_'+name+m) |
| 114 | else: |
| 115 | foundMethods.append(m) |
| 116 | |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 117 | try: |
| 118 | self.assertListEq(foundMethods, actualMethods, ignore) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 119 | self.assertEqual(py_item.__module__, value.module) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 120 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 121 | self.assertEqualsOrIgnored(py_item.__name__, value.name, |
| 122 | ignore) |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 123 | # can't check file or lineno |
| 124 | except: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 125 | print("class=%s" % py_item, file=sys.stderr) |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 126 | raise |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 127 | |
| 128 | # Now check for missing stuff. |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 129 | def defined_in(item, module): |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 130 | if isinstance(item, type): |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 131 | return item.__module__ == module.__name__ |
| 132 | if isinstance(item, FunctionType): |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 133 | return item.__globals__ is module.__dict__ |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 134 | return False |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 135 | for name in dir(module): |
| 136 | item = getattr(module, name) |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 137 | if isinstance(item, (type, FunctionType)): |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 138 | if defined_in(item, module): |
| 139 | self.assertHaskey(dict, name, ignore) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 140 | |
| 141 | def test_easy(self): |
| 142 | self.checkModule('pyclbr') |
Georg Brandl | 9f0f960 | 2008-06-12 22:23:59 +0000 | [diff] [blame] | 143 | self.checkModule('ast') |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 144 | self.checkModule('doctest', ignore=("TestResults", "_SpoofOut", |
| 145 | "DocTestCase")) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 146 | self.checkModule('difflib', ignore=("Match",)) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 147 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 148 | def test_decorators(self): |
| 149 | # XXX: See comment in pyclbr_input.py for a test that would fail |
| 150 | # if it were not commented out. |
| 151 | # |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 152 | self.checkModule('test.pyclbr_input', ignore=['om']) |
Tim Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 153 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 154 | def test_others(self): |
| 155 | cm = self.checkModule |
| 156 | |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 157 | # These were once about the 10 longest modules |
Raymond Hettinger | e401b6f | 2002-12-30 07:21:32 +0000 | [diff] [blame] | 158 | cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 159 | cm('cgi', ignore=('log',)) # set with = in module |
Tim Peters | 264c659 | 2004-07-18 00:08:11 +0000 | [diff] [blame] | 160 | cm('pickle') |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 161 | cm('aifc', ignore=('openfp',)) # set with = in module |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 162 | cm('sre_parse', ignore=('dump',)) # from sre_constants import * |
| 163 | cm('pdb') |
| 164 | cm('pydoc') |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 166 | # Tests for modules inside packages |
Neal Norwitz | 315d845 | 2007-08-30 03:06:59 +0000 | [diff] [blame] | 167 | cm('email.parser') |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 168 | cm('test.test_pyclbr') |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 169 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 170 | |
| 171 | def test_main(): |
| 172 | run_unittest(PyclbrTest) |
| 173 | |
| 174 | |
| 175 | if __name__ == "__main__": |
| 176 | test_main() |