File: test_cases.py

package info (click to toggle)
xdoctest 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,512 kB
  • sloc: python: 10,963; sh: 815; cpp: 33; makefile: 19
file content (225 lines) | stat: -rw-r--r-- 5,969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from xdoctest.utils.util_misc import _run_case
from xdoctest import utils


def test_properties():
    """
    Test that all doctests are extracted from properties correctly.
    https://github.com/Erotemic/xdoctest/issues/73

    Credit: @trappitsch
    """
    text = _run_case(utils.codeblock(
        '''
        class Test:
            @property
            def test(self):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test
                    3.14
                """
                return 3.14

            @test.setter
            def test(self, s):
                pass
        '''))
    assert 'running 1 test' in text

    text = _run_case(utils.codeblock(
        '''
        class Test:
            @property
            def test(self):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test
                    3.14
                """
                return 3.14
        '''))
    assert 'running 1 test' in text

    text = _run_case(utils.codeblock(
        '''
        class Test:
            @property
            def test(self):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test
                    3.14
                """
                return 3.14

            @test.setter
            def test(self, s):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test = 3
                """
                pass
        '''))
    assert 'running 1 test' in text

    text = _run_case(utils.codeblock(
        '''
        class Test:
            @property
            def test(self):
                return 3.14

            @test.setter
            def test(self, s):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test = 3
                """
                pass
        '''))
    assert 'running 0 test' in text

    text = _run_case(utils.codeblock(
        '''
        class Test:
            @property
            def test(self):
                return 3.14

            @test.setter
            def test(self, s):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test = 3
                """
                pass

            @test.deleter
            def test(self, s):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test = 3
                """
                pass
        '''))
    assert 'running 0 test' in text


def test_correct_skipping_on_decorators1():
    """
    This is a weird case similar to the torch dispatch doctest

    ~/code/pytorch/torch/fx/experimental/unification/multipledispatch/core.py

    Something about it causes the skip directive not to be applied to the
    entire thing. Not quite sure what's going on yet.

    The issue was that decorator line numbers were returning as the line of the
    function itself. This mean that the PS1 grouping put the directive in a
    group with logic, which made the parser think it was inline, which meant
    the skip state was cleared after it was executed, so it executed the bad
    code. This fixes that.
    """
    import xdoctest
    from xdoctest import runner
    from os.path import join

    source = utils.codeblock(
        '''
        def dispatch(*types, **kwargs):
            """ blah blah blah blah blah blah blah blah blah blah blah blah
            blah blah blah blah blah blah blah blah

            Example:
                >>> # xdoctest: +SKIP
                >>> @dispatch(int)
                ... def f(x):
                ...     return x + 1
                >>> @dispatch(float)
                ... def f(x):
                ...     return x + 1
                >>> f(3)
                4
                >>> f(3.0)
                4.0
            """
            return lambda x: x
        ''')

    config = {
        # 'global_exec': 'a=1',
        'style': 'google',
    }

    # xdoctest.global_state.DEBUG = 1
    # xdoctest.global_state.DEBUG_PARSER = 10
    # xdoctest.global_state.DEBUG_CORE = 1
    # xdoctest.global_state.DEBUG_RUNNER = 1
    # xdoctest.global_state.DEBUG_DOCTEST = 1

    temp = utils.TempDir()
    dpath = temp.ensure()
    with temp as temp:
        modpath = join(dpath, 'test_example_run.py')

        with open(modpath, 'w') as file:
            file.write(source)

        examples = list(xdoctest.core.parse_doctestables(modpath, style='google', analysis='static'))
        print(f'examples={examples}')

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'all', argv=[''], config=config)
        print(cap.text)
        assert '1 skipped' in cap.text


def test_correct_skipping_on_decorators_simple():
    """
    minimal test for decorator skips
    """

    import xdoctest
    from xdoctest import runner
    from os.path import join

    source = utils.codeblock(
        '''
        def _my_decorator():
            """
            Example:
                >>> # xdoctest: +SKIP
                >>> @_my_decorator()
                ... def my_func(x):
                ...     ...
                >>> f(3)
            """
            return
        ''')

    config = {
        'style': 'google',
    }
    temp = utils.TempDir()
    dpath = temp.ensure()
    with temp as temp:
        modpath = join(dpath, 'test_example_run.py')

        with open(modpath, 'w') as file:
            file.write(source)

        examples = list(xdoctest.core.parse_doctestables(modpath, style='google', analysis='static'))
        print(f'examples={examples}')

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'all', argv=[''], config=config)
        print(cap.text)
        assert '1 skipped' in cap.text