Skip to content

Commit b148a2a

Browse files
committed
fixing tests
1 parent 44a2323 commit b148a2a

File tree

1 file changed

+81
-51
lines changed

1 file changed

+81
-51
lines changed

tests/test_execute.py

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
from jupyter_sphinx.ast import (
1515
JupyterCellNode,
16+
CellInputNode,
17+
CellOutputNode,
1618
JupyterWidgetViewNode,
1719
JupyterWidgetStateNode,
1820
cell_output_to_nodes,
@@ -42,7 +44,7 @@ def doctree(
4244
apps.append(app)
4345
app.build()
4446

45-
doctree = app.env.get_doctree("contents")
47+
doctree = app.env.get_and_resolve_doctree("contents", app.builder)
4648

4749
if return_warnings:
4850
return doctree, warnings.getvalue()
@@ -66,12 +68,13 @@ def test_basic(doctree):
6668
"""
6769
tree = doctree(source)
6870
(cell,) = tree.traverse(JupyterCellNode)
71+
(cellinput, celloutput) = cell.children
6972
assert cell.attributes["code_below"] is False
7073
assert cell.attributes["hide_code"] is False
7174
assert cell.attributes["hide_output"] is False
7275
assert cell.attributes["linenos"] is False
73-
assert cell.children[0].rawsource.strip() == "2 + 2"
74-
assert cell.children[1].rawsource.strip() == "4"
76+
assert cellinput.children[0].rawsource.strip() == "2 + 2"
77+
assert celloutput.children[0].rawsource.strip() == "4"
7578

7679

7780
def test_basic_old_entrypoint(doctree):
@@ -82,12 +85,13 @@ def test_basic_old_entrypoint(doctree):
8285
"""
8386
tree = doctree(source, entrypoint="jupyter_sphinx.execute")
8487
(cell,) = tree.traverse(JupyterCellNode)
88+
(cellinput, celloutput) = cell.children
8589
assert cell.attributes["code_below"] is False
8690
assert cell.attributes["hide_code"] is False
8791
assert cell.attributes["hide_output"] is False
8892
assert cell.attributes["linenos"] is False
89-
assert cell.children[0].rawsource.strip() == "2 + 2"
90-
assert cell.children[1].rawsource.strip() == "4"
93+
assert cellinput.children[0].rawsource.strip() == "2 + 2"
94+
assert celloutput.children[0].rawsource.strip() == "4"
9195

9296

9397
def test_hide_output(doctree):
@@ -99,9 +103,10 @@ def test_hide_output(doctree):
99103
"""
100104
tree = doctree(source)
101105
(cell,) = tree.traverse(JupyterCellNode)
106+
(cellinput, celloutput) = cell.children
102107
assert cell.attributes["hide_output"] is True
103-
assert len(cell.children) == 1
104-
assert cell.children[0].rawsource.strip() == "2 + 2"
108+
assert len(celloutput.children) == 0
109+
assert cellinput.children[0].rawsource.strip() == "2 + 2"
105110

106111

107112
def test_hide_code(doctree):
@@ -113,9 +118,10 @@ def test_hide_code(doctree):
113118
"""
114119
tree = doctree(source)
115120
(cell,) = tree.traverse(JupyterCellNode)
121+
(celloutput,) = cell.children
116122
assert cell.attributes["hide_code"] is True
117123
assert len(cell.children) == 1
118-
assert cell.children[0].rawsource.strip() == "4"
124+
assert celloutput.children[0].rawsource.strip() == "4"
119125

120126

121127
def test_code_below(doctree):
@@ -127,9 +133,10 @@ def test_code_below(doctree):
127133
"""
128134
tree = doctree(source)
129135
(cell,) = tree.traverse(JupyterCellNode)
136+
(celloutput, cellinput) = cell.children
130137
assert cell.attributes["code_below"] is True
131-
assert cell.children[0].rawsource.strip() == "4"
132-
assert cell.children[1].rawsource.strip() == "2 + 2"
138+
assert cellinput.children[0].rawsource.strip() == "2 + 2"
139+
assert celloutput.children[0].rawsource.strip() == "4"
133140

134141

135142
def test_linenos(doctree):
@@ -141,10 +148,11 @@ def test_linenos(doctree):
141148
"""
142149
tree = doctree(source)
143150
(cell,) = tree.traverse(JupyterCellNode)
151+
(cellinput, celloutput) = cell.children
144152
assert cell.attributes["linenos"] is True
145153
assert len(cell.children) == 2
146-
assert cell.children[0].rawsource.strip() == "2 + 2"
147-
assert cell.children[1].rawsource.strip() == "4"
154+
assert cellinput.children[0].rawsource.strip() == "2 + 2"
155+
assert celloutput.children[0].rawsource.strip() == "4"
148156
source = """
149157
.. jupyter-execute::
150158
:linenos:
@@ -154,6 +162,7 @@ def test_linenos(doctree):
154162
"""
155163
tree = doctree(source)
156164
(cell,) = tree.traverse(JupyterCellNode)
165+
(cellinput, celloutput) = cell.children
157166
assert len(cell.children) == 2
158167
assert cell.attributes["linenos"] is True
159168

@@ -166,10 +175,11 @@ def test_linenos_conf_option(doctree):
166175
"""
167176
tree = doctree(source, config="jupyter_sphinx_linenos = True")
168177
(cell,) = tree.traverse(JupyterCellNode)
169-
assert cell.children[0].attributes["linenos"]
170-
assert "highlight_args" not in cell.children[0].attributes
171-
assert cell.children[0].rawsource.strip() == "2 + 2"
172-
assert cell.children[1].rawsource.strip() == "4"
178+
(cellinput, celloutput) = cell.children
179+
assert cellinput.attributes["linenos"]
180+
assert "highlight_args" not in cellinput.attributes
181+
assert cellinput.children[0].rawsource.strip() == "2 + 2"
182+
assert celloutput.children[0].rawsource.strip() == "4"
173183

174184

175185
def test_continue_linenos_conf_option(doctree):
@@ -183,9 +193,10 @@ def test_continue_linenos_conf_option(doctree):
183193

184194
tree = doctree(source, config="jupyter_sphinx_continue_linenos = True")
185195
(cell,) = tree.traverse(JupyterCellNode)
186-
assert "linenos" not in cell.children[0].attributes
187-
assert cell.children[0].rawsource.strip() == "2 + 2"
188-
assert cell.children[1].rawsource.strip() == "4"
196+
(cellinput, celloutput) = cell.children
197+
assert "linenos" not in cellinput.attributes
198+
assert cellinput.children[0].rawsource.strip() == "2 + 2"
199+
assert celloutput.children[0].rawsource.strip() == "4"
189200

190201
# Test continuous line numbering
191202
source = """
@@ -206,14 +217,16 @@ def test_continue_linenos_conf_option(doctree):
206217
)
207218

208219
cell0, cell1 = tree.traverse(JupyterCellNode)
209-
assert cell0.children[0].attributes["linenos"]
210-
assert cell0.children[0].rawsource.strip() == "2 + 2"
211-
assert cell0.children[1].rawsource.strip() == "4"
220+
(cellinput0, celloutput0) = cell0.children
221+
(cellinput1, celloutput1) = cell1.children
222+
assert cellinput0.attributes["linenos"]
223+
assert cellinput0.children[0].rawsource.strip() == "2 + 2"
224+
assert celloutput0.children[0].rawsource.strip() == "4"
212225

213-
assert cell1.children[0].attributes["linenos"]
214-
assert cell1.children[0].attributes["highlight_args"]["linenostart"] == 2
215-
assert cell1.children[0].rawsource.strip() == "3 + 3"
216-
assert cell1.children[1].rawsource.strip() == "6"
226+
assert cellinput1.attributes["linenos"]
227+
assert cellinput1.attributes["highlight_args"]["linenostart"] == 2
228+
assert cellinput1.children[0].rawsource.strip() == "3 + 3"
229+
assert celloutput1.children[0].rawsource.strip() == "6"
217230

218231
# Line number should continue after lineno-start option
219232

@@ -234,14 +247,16 @@ def test_continue_linenos_conf_option(doctree):
234247
"jupyter_sphinx_continue_linenos = True",
235248
)
236249
cell0, cell1 = tree.traverse(JupyterCellNode)
237-
assert cell0.children[0].attributes["highlight_args"]["linenostart"] == 7
238-
assert cell0.children[0].rawsource.strip() == "2 + 2"
239-
assert cell0.children[1].rawsource.strip() == "4"
250+
(cellinput0, celloutput0) = cell0.children
251+
(cellinput1, celloutput1) = cell1.children
252+
assert cellinput0.attributes["highlight_args"]["linenostart"] == 7
253+
assert cellinput0.children[0].rawsource.strip() == "2 + 2"
254+
assert celloutput0.children[0].rawsource.strip() == "4"
240255

241-
assert cell1.children[0].attributes["linenos"]
242-
assert cell1.children[0].attributes["highlight_args"]["linenostart"] == 8
243-
assert cell1.children[0].rawsource.strip() == "3 + 3"
244-
assert cell1.children[1].rawsource.strip() == "6"
256+
assert cellinput1.attributes["linenos"]
257+
assert cellinput1.attributes["highlight_args"]["linenostart"] == 8
258+
assert cellinput1.children[0].rawsource.strip() == "3 + 3"
259+
assert celloutput1.children[0].rawsource.strip() == "6"
245260

246261

247262
def test_emphasize_lines(doctree):
@@ -266,6 +281,8 @@ def test_emphasize_lines(doctree):
266281
"""
267282
tree = doctree(source)
268283
cell0, cell1 = tree.traverse(JupyterCellNode)
284+
(cellinput0, celloutput0) = cell0.children
285+
(cellinput1, celloutput1) = cell1.children
269286

270287
assert cell0.attributes["emphasize_lines"] == [1, 3, 4, 5]
271288
assert cell1.attributes["emphasize_lines"] == [2, 4]
@@ -284,7 +301,9 @@ def test_execution_environment_carries_over(doctree):
284301
"""
285302
tree = doctree(source)
286303
cell0, cell1 = tree.traverse(JupyterCellNode)
287-
assert cell1.children[1].rawsource.strip() == "2"
304+
(cellinput0, celloutput0) = cell0.children
305+
(cellinput1, celloutput1) = cell1.children
306+
assert celloutput1.children[0].rawsource.strip() == "2"
288307

289308

290309
def test_kernel_restart(doctree):
@@ -304,7 +323,9 @@ def test_kernel_restart(doctree):
304323
"""
305324
tree = doctree(source)
306325
cell0, cell1 = tree.traverse(JupyterCellNode)
307-
assert "NameError" in cell1.children[1].rawsource
326+
(cellinput0, celloutput0) = cell0.children
327+
(cellinput1, celloutput1) = cell1.children
328+
assert "NameError" in celloutput1.children[0].rawsource
308329

309330

310331
def test_raises(doctree):
@@ -324,7 +345,8 @@ def test_raises(doctree):
324345
"""
325346
tree = doctree(source)
326347
(cell,) = tree.traverse(JupyterCellNode)
327-
assert "ValueError" in cell.children[1].rawsource
348+
(cellinput, celloutput) = cell.children
349+
assert "ValueError" in celloutput.children[0].rawsource
328350

329351
source = """
330352
.. jupyter-execute::
@@ -334,7 +356,8 @@ def test_raises(doctree):
334356
"""
335357
tree = doctree(source)
336358
(cell,) = tree.traverse(JupyterCellNode)
337-
assert "ValueError" in cell.children[1].rawsource
359+
(cellinput, celloutput) = cell.children
360+
assert "ValueError" in celloutput.children[0].rawsource
338361

339362

340363
def test_widgets(doctree):
@@ -370,8 +393,9 @@ def test_stdout(doctree):
370393
"""
371394
tree = doctree(source)
372395
(cell,) = tree.traverse(JupyterCellNode)
396+
(cellinput, celloutput) = cell.children
373397
assert len(cell.children) == 2
374-
assert cell.children[1].rawsource.strip() == "hello world"
398+
assert celloutput.children[0].rawsource.strip() == "hello world"
375399

376400

377401
def test_stderr(doctree):
@@ -385,7 +409,8 @@ def test_stderr(doctree):
385409
tree, warnings = doctree(source, return_warnings=True)
386410
assert "hello world" in warnings
387411
(cell,) = tree.traverse(JupyterCellNode)
388-
assert len(cell.children) == 1 # no output
412+
(_, celloutput) = cell.children
413+
assert len(celloutput) == 0 # no output
389414

390415
source = """
391416
.. jupyter-execute::
@@ -396,9 +421,10 @@ def test_stderr(doctree):
396421
"""
397422
tree = doctree(source)
398423
(cell,) = tree.traverse(JupyterCellNode)
424+
(_, celloutput) = cell.children
399425
assert len(cell.children) == 2
400-
assert "stderr" in cell.children[1].attributes["classes"]
401-
assert cell.children[1].astext().strip() == "hello world"
426+
assert "stderr" in celloutput.children[0].attributes["classes"]
427+
assert celloutput.children[0].astext().strip() == "hello world"
402428

403429

404430
thebe_config = 'jupyter_sphinx_thebelab_config = {"dummy": True}'
@@ -413,10 +439,11 @@ def test_thebe_hide_output(doctree):
413439
"""
414440
tree = doctree(source, thebe_config)
415441
(cell,) = tree.traverse(JupyterCellNode)
442+
(cellinput, celloutput) = cell.children
416443
assert cell.attributes["hide_output"] is True
417-
assert len(cell.children) == 1
444+
assert len(celloutput.children) == 0
418445

419-
source = cell.children[0]
446+
source = cellinput.children[0]
420447
assert type(source) == ThebeSourceNode
421448
assert len(source.children) == 1
422449
assert source.children[0].rawsource.strip() == "2 + 2"
@@ -431,16 +458,17 @@ def test_thebe_hide_code(doctree):
431458
"""
432459
tree = doctree(source, thebe_config)
433460
(cell,) = tree.traverse(JupyterCellNode)
461+
(cellinput, celloutput) = cell.children
434462
assert cell.attributes["hide_code"] is True
435463
assert len(cell.children) == 2
436464

437-
source = cell.children[0]
465+
source = cellinput.children[0]
438466
assert type(source) == ThebeSourceNode
439467
assert source.attributes["hide_code"] is True
440468
assert len(source.children) == 1
441469
assert source.children[0].rawsource.strip() == "2 + 2"
442470

443-
output = cell.children[1]
471+
output = celloutput.children[0]
444472
assert type(output) == ThebeOutputNode
445473
assert len(output.children) == 1
446474
assert output.children[0].rawsource.strip() == "4"
@@ -455,14 +483,15 @@ def test_thebe_code_below(doctree):
455483
"""
456484
tree = doctree(source, thebe_config)
457485
(cell,) = tree.traverse(JupyterCellNode)
486+
(cellinput, celloutput) = cell.children
458487
assert cell.attributes["code_below"] is True
459488

460-
output = cell.children[0]
489+
output = cellinput.children[0]
461490
assert type(output) is ThebeOutputNode
462491
assert len(output.children) == 1
463492
assert output.children[0].rawsource.strip() == "4"
464493

465-
source = cell.children[1]
494+
source = celloutput.children[0]
466495
assert type(source) is ThebeSourceNode
467496
assert len(source.children) == 1
468497
assert source.children[0].rawsource.strip() == "2 + 2"
@@ -513,7 +542,8 @@ def test_latex(doctree):
513542
for start, end in delimiter_pairs:
514543
tree = doctree(source.format(start, end))
515544
(cell,) = tree.traverse(JupyterCellNode)
516-
assert cell.children[1].astext() == r"\int"
545+
(cellinput, celloutput) = cell.children
546+
assert celloutput.children[0].astext() == r"\int"
517547

518548

519549
def test_image_mimetype_uri(doctree):
@@ -523,14 +553,14 @@ def test_image_mimetype_uri(doctree):
523553
img_locs = ['/_build/jupyter_execute/docs/image_1.png','/_build/jupyter_execute/image_2.png']
524554

525555
cells = [
526-
{'outputs':
556+
{'outputs':
527557
[{'data': {'image/png': 'Vxb6L1wAAAABJRU5ErkJggg==\n', 'text/plain': '<Figure size 432x288 with 1 Axes>'}, 'metadata': {'filenames': {'image/png': img_locs[0]}}, 'output_type': 'display_data'}]
528558
},
529-
{'outputs':
559+
{'outputs':
530560
[{'data': {'image/png': 'iVBOJggg==\n', 'text/plain': '<Figure size 432x288 with 1 Axes>'}, 'metadata': {'filenames': {'image/png': img_locs[1]}}, 'output_type': 'display_data'}]
531561
}]
532562

533563
for index, cell in enumerate(cells):
534564
cell = from_dict(cell)
535-
output_node = cell_output_to_nodes(cell, priority, True, output_dir, None)
565+
output_node = cell_output_to_nodes(cell["outputs"], priority, True, output_dir, None)
536566
assert output_node[0].attributes['uri'] == img_locs[index]

0 commit comments

Comments
 (0)