def test_script_vars_globals(gallery_conf, tmpdir): """Assert the global vars get stored.""" filename = str(tmpdir.join('temp.py')) with open(filename, 'w') as f: f.write(""" ''' My example ---------- This is it. ''' a = 1. b = 'foo' """) file_conf, blocks = sg.split_code_and_text_blocks(filename) assert len(blocks) == 2 assert blocks[0][0] == 'text' assert blocks[1][0] == 'code' assert file_conf == {} script_vars = { 'execute_script': True, 'src_file': filename, 'image_path_iterator': [], 'target_file': filename } output_blocks, time_elapsed = sg.execute_script(blocks, script_vars, gallery_conf) assert 'example_globals' in script_vars assert script_vars['example_globals']['a'] == 1. assert script_vars['example_globals']['b'] == 'foo'
def test_rst_empty_code_block(gallery_conf, tmpdir): """Test that we can "execute" a code block containing only comments.""" filename = str(tmpdir.join('temp.py')) with open(filename, 'w') as f: f.write('\n'.join([ '"Docstring"', '####################', '# Paragraph 1', '', '# just a comment' '' ])) file_conf, blocks = sg.split_code_and_text_blocks(filename) assert file_conf == {} assert len(blocks) == 3 assert blocks[0][0] == 'text' assert blocks[1][0] == 'text' assert blocks[2][0] == 'code' gallery_conf['abort_on_example_error'] = True script_vars = dict(execute_script=True, src_file=filename, image_path_iterator=[], target_file=filename) output_blocks, time_elapsed = sg.execute_script(blocks, script_vars, gallery_conf) example_rst = sg.rst_blocks(blocks, output_blocks, file_conf, gallery_conf) assert example_rst.rstrip('\n') == """Docstring
def test_rst_block_after_docstring(gallery_conf, tmpdir): """Assert there is a blank line between the docstring and rst blocks.""" filename = str(tmpdir.join('temp.py')) with open(filename, 'w') as f: f.write('\n'.join([ '"Docstring"', '####################', '# Paragraph 1', '', '#%%', '# Paragraph 2', '', '# %%', '# Paragraph 3', '' ])) file_conf, blocks = sg.split_code_and_text_blocks(filename) assert file_conf == {} assert len(blocks) == 4 assert blocks[0][0] == 'text' assert blocks[1][0] == 'text' assert blocks[2][0] == 'text' assert blocks[3][0] == 'text' script_vars = {'execute_script': ''} output_blocks, time_elapsed = sg.execute_script(blocks, script_vars, gallery_conf) example_rst = sg.rst_blocks(blocks, output_blocks, file_conf, gallery_conf) assert example_rst == '\n'.join([ 'Docstring', '', 'Paragraph 1', '', 'Paragraph 2', '', 'Paragraph 3', '', '' ])
def test_rst_block_after_docstring(gallery_conf, tmpdir): """Assert there is a blank line between the docstring and rst blocks.""" filename = str(tmpdir.join('temp.py')) with open(filename, 'w') as f: f.write('\n'.join(['"Docstring"', '####################', '# Paragraph 1', '', '####################', '# Paragraph 2', ''])) file_conf, blocks = sg.split_code_and_text_blocks(filename) assert file_conf == {} assert blocks[0][0] == 'text' assert blocks[1][0] == 'text' assert blocks[2][0] == 'text' script_vars = {'execute_script': ''} output_blocks, time_elapsed = sg.execute_script(blocks, script_vars, gallery_conf) example_rst = sg.rst_blocks(blocks, output_blocks, file_conf, gallery_conf) assert example_rst == '\n'.join([ 'Docstring', '', 'Paragraph 1', '', 'Paragraph 2', '', ''])
def test_rst_block_after_docstring(gallery_conf, tmpdir): """Assert there is a blank line between the docstring and rst blocks.""" filename = str(tmpdir.join('temp.py')) with open(filename, 'w') as f: f.write('\n'.join([ '"Docstring"', '####################', '# Paragraph 1', '# is long.', '', '#%%', '# Paragraph 2', '', '# %%', '# Paragraph 3', '' ])) file_conf, blocks = sg.split_code_and_text_blocks(filename) assert file_conf == {} assert len(blocks) == 4 assert blocks[0][0] == 'text' assert blocks[1][0] == 'text' assert blocks[2][0] == 'text' assert blocks[3][0] == 'text' script_vars = {'execute_script': ''} file_conf = {} output_blocks, time_elapsed = sg.execute_script(blocks, script_vars, gallery_conf, file_conf) example_rst = sg.rst_blocks(blocks, output_blocks, file_conf, gallery_conf) want_rst = """\ Docstring .. GENERATED FROM PYTHON SOURCE LINES 3-5 Paragraph 1 is long. .. GENERATED FROM PYTHON SOURCE LINES 7-8 Paragraph 2 .. GENERATED FROM PYTHON SOURCE LINES 10-11 Paragraph 3 """ assert example_rst == want_rst