Skip to content

Commit 706ddf3

Browse files
committed
Add tests for thebelab support
1 parent d14bdcf commit 706ddf3

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

tests/test_execute.py

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
JupyterKernelNode,
1616
JupyterWidgetViewNode,
1717
JupyterWidgetStateNode,
18+
ThebeSourceNode,
19+
ThebeOutputNode,
20+
ThebeButtonNode,
1821
)
1922

2023
@pytest.fixture()
@@ -23,11 +26,13 @@ def doctree():
2326
apps = []
2427
syspath = sys.path[:]
2528

26-
def doctree(source):
29+
def doctree(source, config=None):
2730
src_dir = tempfile.mkdtemp()
2831
source_trees.append(src_dir)
2932
with open(os.path.join(src_dir, 'conf.py'), 'w') as f:
3033
f.write("extensions = ['jupyter_sphinx.execute']")
34+
if config is not None:
35+
f.write('\n' + config)
3136
with open(os.path.join(src_dir, 'index.rst'), 'w') as f:
3237
f.write(source)
3338
app = SphinxTestApp(srcdir=path(src_dir), status=StringIO(),
@@ -56,6 +61,7 @@ def test_basic(doctree):
5661
assert cell.attributes['code_below'] is False
5762
assert cell.attributes['hide_code'] is False
5863
assert cell.attributes['hide_output'] is False
64+
assert cell.attributes['no_thebelab'] is False
5965
assert cell.children[0].rawsource.strip() == "2 + 2"
6066
assert cell.children[1].rawsource.strip() == "4"
6167

@@ -226,3 +232,107 @@ def test_stderr(doctree):
226232
cell, = tree.traverse(JupyterCellNode)
227233
assert len(cell.children) == 2
228234
assert cell.children[1].rawsource.strip() == "hello world"
235+
236+
237+
thebe_config = "jupyter_sphinx_thebelab_config = {\"dummy\": True}"
238+
239+
240+
def test_thebe_hide_output(doctree):
241+
source = '''
242+
.. jupyter-execute::
243+
:hide-output:
244+
245+
2 + 2
246+
'''
247+
tree = doctree(source, thebe_config)
248+
cell, = tree.traverse(JupyterCellNode)
249+
assert cell.attributes['hide_output'] is True
250+
assert len(cell.children) == 1
251+
252+
source = cell.children[0]
253+
assert type(source) == ThebeSourceNode
254+
assert len(source.children) == 1
255+
assert source.children[0].rawsource.strip() == "2 + 2"
256+
257+
258+
def test_thebe_hide_code(doctree):
259+
source = '''
260+
.. jupyter-execute::
261+
:hide-code:
262+
263+
2 + 2
264+
'''
265+
tree = doctree(source, thebe_config)
266+
cell, = tree.traverse(JupyterCellNode)
267+
assert cell.attributes['hide_code'] is True
268+
assert len(cell.children) == 2
269+
270+
source = cell.children[0]
271+
assert type(source) == ThebeSourceNode
272+
assert source.attributes['hide_code'] is True
273+
assert len(source.children) == 1
274+
assert source.children[0].rawsource.strip() == "2 + 2"
275+
276+
output = cell.children[1]
277+
assert type(output) == ThebeOutputNode
278+
assert len(output.children) == 1
279+
assert output.children[0].rawsource.strip() == "4"
280+
281+
282+
def test_thebe_code_below(doctree):
283+
source = '''
284+
.. jupyter-execute::
285+
:code-below:
286+
287+
2 + 2
288+
'''
289+
tree = doctree(source, thebe_config)
290+
cell, = tree.traverse(JupyterCellNode)
291+
assert cell.attributes['code_below'] is True
292+
293+
output = cell.children[0]
294+
assert type(output) is ThebeOutputNode
295+
assert len(output.children) == 1
296+
assert output.children[0].rawsource.strip() == "4"
297+
298+
source = cell.children[1]
299+
assert type(source) is ThebeSourceNode
300+
assert len(source.children) == 1
301+
assert source.children[0].rawsource.strip() == "2 + 2"
302+
assert source.attributes['code_below'] is True
303+
304+
305+
def test_thebe_button_auto(doctree):
306+
config = "jupyter_sphinx_thebelab_config = {\"dummy\": True}"
307+
source = """
308+
.. jupyter-execute::
309+
310+
1 + 1
311+
"""
312+
tree = doctree(source, config=config)
313+
assert len(tree.traverse(ThebeButtonNode)) == 1
314+
315+
316+
def test_thebe_button_manual(doctree):
317+
config = "jupyter_sphinx_thebelab_config = {\"dummy\": True}"
318+
source = """
319+
.. jupyter-execute::
320+
321+
1 + 1
322+
323+
.. thebe-button::
324+
"""
325+
tree = doctree(source, config)
326+
assert len(tree.traverse(ThebeButtonNode)) == 1
327+
328+
329+
def test_thebe_button_none(doctree):
330+
config = "jupyter_sphinx_thebelab_config = {\"dummy\": True}"
331+
source = """
332+
.. jupyter-execute::
333+
:no-thebelab:
334+
335+
1 + 1
336+
"""
337+
tree = doctree(source, config)
338+
assert len(tree.traverse(ThebeButtonNode)) == 0

0 commit comments

Comments
 (0)