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