def test_empty_message(): # GH 52 messages = [{ 'parent_header': { 'username': '******', 'session': 'a', 'msg_type': 'execute_request', 'msg_id': '3', 'date': datetime.datetime(2016, 9, 27, 7, 20, 13, 790481), 'version': '5.0' }, 'metadata': {}, 'buffers': [], 'msg_type': 'display_data', 'header': { 'username': '******', 'session': 'a', 'msg_type': 'display_data', 'version': '5.0', 'date': '2016-09-27T07:20:17.461893', 'msg_id': '6' }, 'content': { 'metadata': {}, 'data': {} }, 'msg_id': '6' }] s = R.Stitch('foo') result = s.wrap_output('bar', messages, {}) assert result == []
def test_error(self): s = R.Stitch('') assert s.error == 'continue' s.error = 'raise' assert s.error == 'raise' with pytest.raises(TraitError): s.error = 'foo'
def test_getattr(self): s = R.Stitch('') assert getattr(s, 'fig.width') is None assert s.fig.width is None with pytest.raises(AttributeError): assert getattr(s, 'foo.bar') with pytest.raises(AttributeError): assert getattr(s, 'foo')
def test_image_chunkname(self): code = dedent('''\ ```{python, chunk} %matplotlib inline import matplotlib.pyplot as plt plt.plot(range(4), range(4)); ``` ''') result = R.Stitch('foo', to='pdf', standalone=False).stitch(code) assert 'chunk' in result[1][1]['c'][0]['c'][0][0]
def test_image(self, to, value, global_python_kernel): code = dedent('''\ ```{python} %matplotlib inline import matplotlib.pyplot as plt plt.plot(range(4), range(4)); ``` ''') result = R.Stitch('foo', to=to).stitch(code) assert result[1][1]['c'][0]['t'] == 'Image'
def test_ipython_display(self, clean_python_kernel, to): s = R.Stitch('', to=to) code = dedent('''\ from IPython import display import math display.Markdown("$\\alpha^{pi:1.3f}$".format(pi=math.pi)) ''') messages = R.run_code(code, clean_python_kernel) wrapped = s.wrap_output('', messages, None)[0] assert wrapped['t'] == 'Para' assert wrapped['c'][0]['c'][0]['t'] == 'InlineMath'
def test_error_raises(self): s = R.Stitch('', error='raise') code = dedent('''\ ```{python} 1 / 0 ``` ''') with pytest.raises(R.StitchError): s.stitch(code) s.error = 'continue' s.stitch(code)
def test_warning(self, clean_python_kernel, warning, length): code = dedent('''\ ```{python} import warnings warnings.warn("Hi") 2 ``` ''') r = R.Stitch('foo', to='html', warning=warning) r._kernel_pairs['python'] = clean_python_kernel result = r.stitch(code) assert len(result['blocks']) == length
def test_image(self, to, value, global_python_kernel): code = dedent('''\ ```{python} %matplotlib inline import matplotlib.pyplot as plt plt.plot(range(4), range(4)) plt.title('Foo — Bar'); # That's an em dash ``` ''') result = R.Stitch('foo', to=to).stitch(code) blocks = result['blocks'] assert blocks[1]['c'][0]['t'] == 'Image'
def test_image_attrs(self): code = dedent('''\ ```{python, chunk, width=10, height=10px} %matplotlib inline import matplotlib.pyplot as plt plt.plot(range(4), range(4)); ``` ''') result = R.Stitch('foo', to='html', standalone=False).stitch(code) attrs = result[1][1]['c'][0]['c'][0][2] assert ('width', '10') in attrs assert ('height', '10px') in attrs
def test_rich_output(self, to, clean_python_kernel): code = dedent('''\ ```{python} import pandas as pd pd.options.display.latex.repr = True pd.DataFrame({'a': [1, 2]}) ``` ''') stitch = R.Stitch('foo', to, ) stitch._kernel_pairs['python'] = clean_python_kernel meta, blocks = stitch.stitch(code) result = blocks[1]['c'][1] assert '\\begin{tabular}' in result
def test_image_no_self_contained(self, clean_python_kernel, clean_name): code = dedent('''\ ```{python} %matplotlib inline import matplotlib.pyplot as plt plt.plot(range(4)) ``` ''') s = R.Stitch(clean_name, self_contained=False) s._kernel_pairs['python'] = clean_python_kernel result = s.stitch(code) blocks = result['blocks'] expected = ('{}_files/unnamed_chunk_0.png'.format(clean_name), r'{}_files\unnamed_chunk_0.png'.format(clean_name)) result = blocks[-1]['c'][0]['c'][2][0] assert result in expected
def test_image_no_self_contained_formats(self, clean_python_kernel, clean_name, fmt): code = dedent('''\ ```{{python}} %matplotlib inline from IPython.display import set_matplotlib_formats import numpy as np import matplotlib.pyplot as plt set_matplotlib_formats('{fmt}') x = np.linspace(-np.pi / 2, np.pi / 2) plt.plot(x, np.sin(x)) plt.plot(x, np.cos(x)) ``` ''').format(fmt=fmt) s = R.Stitch(clean_name, self_contained=False) s._kernel_pairs['python'] = clean_python_kernel s.stitch(code) expected = os.path.join(clean_name + '_files', 'unnamed_chunk_0.' + fmt) assert os.path.exists(expected)
def test_has_trait(self): s = R.Stitch('') assert s.has_trait('fig.width') assert not s.has_trait('fake.width') assert not s.has_trait('fig.fake')