[e988c2]: / tests / docs / test_find_docs_examples.py

Download this file

206 lines (200 with data), 5.6 kB

  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
import textwrap
from io import StringIO
from pathlib import Path
import pytest
from . import test_complete_examples
# The SuperFences extension that we use has its own test suite.
# The tests below cover only the most common cases
# and are to help document and catch changes in behaviour
# that we may be interested in.
@pytest.mark.parametrize(
"fence,expected_example",
[
pytest.param(
textwrap.dedent(
"""\
```ehrql
```
"""
),
[
test_complete_examples.EhrqlExample(
path=Path("test"),
fence_number=1,
source="",
),
],
id="fence with no lines",
),
pytest.param(
textwrap.dedent(
"""\
```ehrql
some code
```
"""
),
[
test_complete_examples.EhrqlExample(
path=Path("test"),
fence_number=1,
source="some code",
),
],
id="fence with one line",
),
pytest.param(
textwrap.dedent(
"""\
```ehrql
some code
more code
```
"""
),
[
test_complete_examples.EhrqlExample(
path=Path("test"),
fence_number=1,
source="some code\nmore code",
),
],
id="fence with multiple lines",
),
pytest.param(
textwrap.dedent(
"""\
```ehrql
some code
```ehrql
more code
```
"""
),
[
test_complete_examples.EhrqlExample(
path=Path("test"),
fence_number=1,
source="some code\n```ehrql\nmore code",
),
],
id="open fence",
),
pytest.param(
textwrap.dedent(
"""\
some text
```ehrql
some code
```
more text
"""
),
[
test_complete_examples.EhrqlExample(
path=Path("test"),
fence_number=1,
source="some code",
),
],
id="fence between text",
),
pytest.param(
textwrap.dedent(
"""\
```ehrql
some code
```
some text
```ehrql
more code
```
"""
),
[
test_complete_examples.EhrqlExample(
path=Path("test"),
fence_number=1,
source="some code",
),
test_complete_examples.EhrqlExample(
path=Path("test"),
fence_number=2,
source="more code",
),
],
id="multiple fences",
),
pytest.param(
textwrap.dedent(
"""\
> ```ehrql
some code
```
"""
),
[
test_complete_examples.EhrqlExample(
path=Path("test"),
fence_number=1,
source="some code",
),
],
id="fence in quote",
),
# List marker must be followed by a blank line with at least one space,
# and the fence starts on next line.
# Use an explicit newline character so that we can:
# * keep the desired formatting
# * avoid complaints from tooling about trailing whitespace
pytest.param(
textwrap.dedent(
"""\
* \n
```ehrql
some code
```
"""
),
[
test_complete_examples.EhrqlExample(
path=Path("test"),
fence_number=1,
source="some code",
),
],
id="fence in list",
),
pytest.param(
textwrap.dedent(
"""\
```ehrql
some code
"""
),
[],
id="fence at end of file",
),
pytest.param(
textwrap.dedent(
"""\
```python
some code
```
"""
),
[],
id="fence with non-matching syntax",
),
],
)
def test_find_docs_examples(fence, expected_example):
example = StringIO(fence)
# Unlike file objects, StringIO objects do not have a name.
# In the relevant code being tested,
# we access the file's name to save somewhat redundantly passing the name.
example.name = "test"
result = list(
test_complete_examples.find_complete_ehrql_examples_in_markdown(example)
)
assert result == expected_example