1# cython: autotestdict=True, autotestdict.cdef=True 2 3""" 4Tests autotestdict compiler directive. 5 6Both module test and individual tests are run; finally, 7all_tests_run() is executed which does final validation. 8 9>>> items = list(__test__.items()) 10>>> items.sort() 11>>> for key, value in items: 12... print('%s ; %s' % (key, value)) 13MyCdefClass.cdef_method (line 78) ; >>> add_log("cdef class cmethod") 14MyCdefClass.cpdef_method (line 75) ; >>> add_log("cpdef class method") 15MyCdefClass.method (line 72) ; >>> add_log("cdef class method") 16MyClass.method (line 61) ; >>> add_log("class method") 17cdeffunc (line 25) ; >>> add_log("cdef") 18mycpdeffunc (line 48) ; >>> add_log("cpdef") 19myfunc (line 39) ; >>> add_log("def") 20""" 21 22import sys 23log = [] 24 25cdef cdeffunc(): 26 """>>> add_log("cdef")""" 27cdeffunc() # make sure it's being used 28 29def all_tests_run(): 30 assert sorted(log) == sorted([u'cdef', u'cdef class', u'cdef class cmethod', u'class'] + ( 31 ((1 if sys.version_info < (3, 4) else 2) * [u'cdef class method', u'class method', u'cpdef', u'cpdef class method', u'def']))), sorted(log) 32 33def add_log(s): 34 log.append(unicode(s)) 35 if len(log) == len(__test__) + (2 if sys.version_info < (3, 4) else 7): 36 # Final per-function doctest executed 37 all_tests_run() 38 39def myfunc(): 40 """>>> add_log("def")""" 41 42def doc_without_test(): 43 """Some docs""" 44 45def nodocstring(): 46 pass 47 48cpdef mycpdeffunc(): 49 """>>> add_log("cpdef")""" 50 51 52class MyClass: 53 """ 54 Needs no hack 55 56 >>> add_log("class") 57 >>> True 58 True 59 """ 60 61 def method(self): 62 """>>> add_log("class method")""" 63 64cdef class MyCdefClass: 65 """ 66 Needs no hack 67 68 >>> add_log("cdef class") 69 >>> True 70 True 71 """ 72 def method(self): 73 """>>> add_log("cdef class method")""" 74 75 cpdef cpdef_method(self): 76 """>>> add_log("cpdef class method")""" 77 78 cdef cdef_method(self): 79 """>>> add_log("cdef class cmethod")""" 80 81 def __cinit__(self): 82 """ 83 Should not be included, as it can't be looked up with getattr 84 85 >>> True 86 False 87 """ 88 89 def __dealloc__(self): 90 """ 91 Should not be included, as it can't be looked up with getattr 92 93 >>> True 94 False 95 """ 96 97 def __richcmp__(self, other, int op): 98 """ 99 Should not be included, as it can't be looked up with getattr in Py 2 100 101 >>> True 102 False 103 """ 104 105 def __nonzero__(self): 106 """ 107 Should not be included, as it can't be looked up with getattr in Py 3.1 108 109 >>> True 110 False 111 """ 112 113 def __len__(self): 114 """ 115 Should not be included, as it can't be looked up with getattr in Py 3.1 116 117 >>> sys.version_info < (3, 4) 118 False 119 """ 120 121 def __contains__(self, value): 122 """ 123 Should not be included, as it can't be looked up with getattr in Py 3.1 124 125 >>> sys.version_info < (3, 4) 126 False 127 """ 128 129cdef class MyOtherCdefClass: 130 """ 131 Needs no hack 132 133 >>> True 134 True 135 """ 136 137 def __bool__(self): 138 """ 139 Should not be included, as it can't be looked up with getattr in Py 2 140 141 >>> True 142 False 143 """ 144