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