OLD | NEW |
(Empty) | |
| 1 from Visitor import ScopeTrackingTransform |
| 2 from Nodes import StatListNode, SingleAssignmentNode, CFuncDefNode, DefNode |
| 3 from ExprNodes import DictNode, DictItemNode, NameNode, UnicodeNode |
| 4 from PyrexTypes import py_object_type |
| 5 from StringEncoding import EncodedString |
| 6 import Symtab |
| 7 |
| 8 class AutoTestDictTransform(ScopeTrackingTransform): |
| 9 # Handles autotestdict directive |
| 10 |
| 11 blacklist = ['__cinit__', '__dealloc__', '__richcmp__', |
| 12 '__nonzero__', '__bool__', |
| 13 '__len__', '__contains__'] |
| 14 |
| 15 def visit_ModuleNode(self, node): |
| 16 if node.is_pxd: |
| 17 return node |
| 18 self.scope_type = 'module' |
| 19 self.scope_node = node |
| 20 |
| 21 if not self.current_directives['autotestdict']: |
| 22 return node |
| 23 self.all_docstrings = self.current_directives['autotestdict.all'] |
| 24 self.cdef_docstrings = self.all_docstrings or self.current_directives['a
utotestdict.cdef'] |
| 25 |
| 26 assert isinstance(node.body, StatListNode) |
| 27 |
| 28 # First see if __test__ is already created |
| 29 if u'__test__' in node.scope.entries: |
| 30 # Do nothing |
| 31 return node |
| 32 |
| 33 pos = node.pos |
| 34 |
| 35 self.tests = [] |
| 36 self.testspos = node.pos |
| 37 |
| 38 test_dict_entry = node.scope.declare_var(EncodedString(u'__test__'), |
| 39 py_object_type, |
| 40 pos, |
| 41 visibility='public') |
| 42 create_test_dict_assignment = SingleAssignmentNode(pos, |
| 43 lhs=NameNode(pos, name=EncodedString(u'__test__'), |
| 44 entry=test_dict_entry), |
| 45 rhs=DictNode(pos, key_value_pairs=self.tests)) |
| 46 self.visitchildren(node) |
| 47 node.body.stats.append(create_test_dict_assignment) |
| 48 return node |
| 49 |
| 50 def add_test(self, testpos, path, doctest): |
| 51 pos = self.testspos |
| 52 keystr = u'%s (line %d)' % (path, testpos[1]) |
| 53 key = UnicodeNode(pos, value=EncodedString(keystr)) |
| 54 value = UnicodeNode(pos, value=doctest) |
| 55 self.tests.append(DictItemNode(pos, key=key, value=value)) |
| 56 |
| 57 def visit_ExprNode(self, node): |
| 58 # expressions cannot contain functions and lambda expressions |
| 59 # do not have a docstring |
| 60 return node |
| 61 |
| 62 def visit_FuncDefNode(self, node): |
| 63 if not node.doc or (isinstance(node, DefNode) and node.fused_py_func): |
| 64 return node |
| 65 if not self.cdef_docstrings: |
| 66 if isinstance(node, CFuncDefNode) and not node.py_func: |
| 67 return node |
| 68 if not self.all_docstrings and '>>>' not in node.doc: |
| 69 return node |
| 70 |
| 71 pos = self.testspos |
| 72 if self.scope_type == 'module': |
| 73 path = node.entry.name |
| 74 elif self.scope_type in ('pyclass', 'cclass'): |
| 75 if isinstance(node, CFuncDefNode): |
| 76 if node.py_func is not None: |
| 77 name = node.py_func.name |
| 78 else: |
| 79 name = node.entry.name |
| 80 else: |
| 81 name = node.name |
| 82 if self.scope_type == 'cclass' and name in self.blacklist: |
| 83 return node |
| 84 if self.scope_type == 'pyclass': |
| 85 class_name = self.scope_node.name |
| 86 else: |
| 87 class_name = self.scope_node.class_name |
| 88 if isinstance(node.entry.scope, Symtab.PropertyScope): |
| 89 property_method_name = node.entry.scope.name |
| 90 path = "%s.%s.%s" % (class_name, node.entry.scope.name, |
| 91 node.entry.name) |
| 92 else: |
| 93 path = "%s.%s" % (class_name, node.entry.name) |
| 94 else: |
| 95 assert False |
| 96 self.add_test(node.pos, path, node.doc) |
| 97 return node |
OLD | NEW |