def test_omit_on_elif(): code = """\ a = 50 if a < 5: c = "good" elif a < 10: # phmdoctest:omit c = "better" elif a < 15: c = "best" else: c = "mediocre" """ want = """\ a = 50 if a < 5: c = "good" # elif a < 10: # phmdoctest:omit # c = "better" elif a < 15: c = "best" else: c = "mediocre" """ got, num_changed_sections = phmdoctest.inline.apply_inline_commands(code) assert num_changed_sections == 1 verify.a_and_b_are_the_same(want, got) ast.parse(code) ast.parse(want)
def test_pass_needed(): code = """\ a = 10 if a < 5: c = "good" # phmdoctest:pass elif a < 10: c = "better" elif a < 15: c = "best" else: c = "mediocre" """ want = """\ a = 10 if a < 5: pass # c = "good" # phmdoctest:pass elif a < 10: c = "better" elif a < 15: c = "best" else: c = "mediocre" """ got, num_changed_sections = phmdoctest.inline.apply_inline_commands(code) assert num_changed_sections == 1 verify.a_and_b_are_the_same(want, got) ast.parse(code) ast.parse(want)
def test_omit_if_with_else(): """It is possible to break the code with a misplaced :omit. Here the if part is commented out. The commenting stops at the else since it is at the same indent level as the if. This introduces the syntax error since a statement can't start with else. """ code = """\ a = 10 if a < 5: # phmdoctest:omit c = "good" else: c = "mediocre" """ want = """\ a = 10 # if a < 5: # phmdoctest:omit # c = "good" else: c = "mediocre" """ got, num_changed_sections = phmdoctest.inline.apply_inline_commands(code) assert num_changed_sections == 1 verify.a_and_b_are_the_same(want, got) ast.parse(code) with pytest.raises(SyntaxError): ast.parse(want)
def test_fcb_chooser(capsys): """Check 3 parts of the label on any fenced code block example.""" # 1. The .md shown in the fenced code block is the same # as the file on disk. want = labeled.contents(label="my-markdown-file") with open("doc/my_markdown_file.md", "r", encoding="utf-8") as fp: got = fp.read() verify.a_and_b_are_the_same(want, got) # 2. The Python code example in the fenced code block is the same as # the body of the function chooser_example_code() above. verify.example_code_checker( callable_function=chooser_example_code, example_string=labeled.contents(label="fetch-it"), ) # 3. The example output shown in the fenced code block is # the same as the output from running chooser_example_code(). want3 = labeled.contents(label="fetched-contents") assert want3 chooser_example_code() got3 = capsys.readouterr().out assert got3.endswith("\n\n"), "FCB newline + print() newline" got3 = got3[:-1] # drop the newline that print() added. verify.a_and_b_are_the_same(want3, got3)
def check_first_block(markdown_path, contents_path): """Check that first FCB in Markdown is same as the file contents.""" with open(contents_path, "r", encoding="utf-8") as f: want = f.read() blocks = phmdoctest.tool.fenced_code_blocks(markdown_path) got = blocks[0] verify.a_and_b_are_the_same(a=want, b=got)
def test_omit_one_line(): code = """\ def myfunc(): import math mylist = [1, 2, 3] # phmdoctest:omit a, b = 10, 11 """ want = """\ def myfunc(): import math # mylist = [1, 2, 3] # phmdoctest:omit a, b = 10, 11 """ got, num_changed_sections = phmdoctest.inline.apply_inline_commands(code) assert num_changed_sections == 1 verify.a_and_b_are_the_same(want, got) # Make sure the example code and expected result are legal Python. # Be aware that this test case will be run on all Python versions # supported by phmdoctest. ast.parse(code) ast.parse(want)
def test_inline_omit(): """Show the result block is produced by the code block.""" code = labeled.contents(label="omit-code") assert code want, num_commented_out = phmdoctest.inline.apply_inline_commands(code) got = labeled.contents(label="omit-result") assert num_commented_out == 2 verify.a_and_b_are_the_same(want, got)
def test_report(): """README report output is same as produced by the command.""" report_command = next(readme_blocks) want = next(readme_blocks) simulator_status = phmdoctest.simulator.run_and_pytest(report_command, pytest_options=None) got = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got)
def test_report(): """README report output is same as produced by the command.""" report_command = labeled.contents(label="report-command") want = labeled.contents(label="example2-report") simulator_status = phmdoctest.simulator.run_and_pytest(report_command, pytest_options=None) got = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got)
def test_setup_report_example(): """Make sure report in README is correct.""" command = labeled.contents(label="setup-command-report") want = labeled.contents(label="setup-report") simulator_status = verify.one_example(command, want_file_name=None, pytest_options=None) got1 = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got1)
def test_simulator_python_code(): """Assure the guts of example_code() above are same as in Markdown.""" want1 = next(readme_blocks) want2 = textwrap.indent(want1, ' ') got1 = inspect.getsource(example_code) got2 = got1.replace('def example_code():\n', '') verify.a_and_b_are_the_same(want2, got2) # also make sure the code runs with no assertions example_code()
def test_no_inline_commands(): code = """\ def myfunc(): import math mylist = [1, 2, 3] a, b = 10, 11 def doubler(x): if x > 5: x += 2 #x += 3 x += 4 # comment x = 9 return x * 2 for item in mylist: print(item) print(item * 2) if __name__ == "__main__": myfunc() """ want = """\ def myfunc(): import math mylist = [1, 2, 3] a, b = 10, 11 def doubler(x): if x > 5: x += 2 #x += 3 x += 4 # comment x = 9 return x * 2 for item in mylist: print(item) print(item * 2) if __name__ == "__main__": myfunc() """ got, num_changed_sections = phmdoctest.inline.apply_inline_commands(code) assert num_changed_sections == 0 verify.a_and_b_are_the_same(want, got) ast.parse(code) ast.parse(want)
def test_blankline_in_omitted_pass_ignored(): code = """\ def myfunc(): import math mylist = [1, 2, 3] a, b = 10, 11 def doubler(x): # phmdoctest:omit if x > 5: x += 2 #x += 3 x += 4 # phmdoctest:pass # comment x = 9 return x * 2 for item in mylist: print(item) print(item * 2) if __name__ == "__main__": myfunc() """ want = """\ def myfunc(): import math mylist = [1, 2, 3] a, b = 10, 11 # def doubler(x): # phmdoctest:omit # if x > 5: # x += 2 # #x += 3 # x += 4 # phmdoctest:pass # # comment # x = 9 # # return x * 2 for item in mylist: print(item) print(item * 2) if __name__ == "__main__": myfunc() """ got, num_changed_sections = phmdoctest.inline.apply_inline_commands(code) assert num_changed_sections == 1 verify.a_and_b_are_the_same(want, got) ast.parse(code) ast.parse(want)
def test_empty_code_blocks_report(): """Report counts empty code and output blocks.""" command = "phmdoctest tests/empty_code_block.md --report" simulator_status = phmdoctest.simulator.run_and_pytest( well_formed_command=command, pytest_options=None) assert simulator_status.runner_status.exit_code == 0 stdout = simulator_status.runner_status.stdout with open("tests/empty_code_report.txt", "r", encoding="utf-8") as f: want = f.read() verify.a_and_b_are_the_same(want, stdout)
def test_one_skip_many_matches(): """Every block matches the skip pattern presenting multi-line report.""" command = "phmdoctest tests/twentysix_session_blocks.md" ' --skip=">>>" --report' simulator_status = phmdoctest.simulator.run_and_pytest( well_formed_command=command, pytest_options=None) assert simulator_status.runner_status.exit_code == 0 stdout = simulator_status.runner_status.stdout with open("tests/twentysix_report.txt", "r", encoding="utf-8") as f: want = f.read() verify.a_and_b_are_the_same(want, stdout)
def test_example2_report(): """Check example2_report.txt used in .travis.yml.""" simulator_status = verify.one_example( 'phmdoctest doc/example2.md --skip "Python 3.7" --skip LAST --report' ' --outfile discarded.py', want_file_name=None, pytest_options=None) assert simulator_status.runner_status.exit_code == 0 stdout = simulator_status.runner_status.stdout with open('tests/example2_report.txt', 'r', encoding='utf-8') as f: want = f.read() verify.a_and_b_are_the_same(a=want, b=stdout)
def test_empty_code_block_report(): """Empty code block and associated output block get del'd.""" simulator_status = verify.one_example( "phmdoctest tests/empty_code_block.md" " --report --outfile discarded.py", want_file_name=None, pytest_options=["--doctest-modules", "-v"], ) assert simulator_status.runner_status.exit_code == 0 assert simulator_status.pytest_exit_code == 0 stdout = simulator_status.runner_status.stdout with open("tests/empty_code_report.txt", "r", encoding="utf-8") as f: want = f.read() verify.a_and_b_are_the_same(a=want, b=stdout)
def test_directive_example(): """Make sure generated --outfile is as expected; Run pytest. Check the --outfile against the copy in the fenced code block. """ example1_command = labeled.contents(label="directive-example-command") want = labeled.contents(label="directive-example-outfile") simulator_status = verify.one_example( example1_command, want_file_name=None, pytest_options=["--doctest-modules", "-v"], ) # Fenced code block in README.md is the same as the --outfile. got = simulator_status.outfile verify.a_and_b_are_the_same(want, got) assert simulator_status.pytest_exit_code == 0
def test_outfile_to_stdout(): """Make sure generated --outfile and --report are as expected.""" outfile_command1 = next(readme_blocks) outfile_command2 = next(readme_blocks) simulator_status = verify.one_example(outfile_command1, want_file_name=None, pytest_options=None) with open('doc/test_example2.py') as fp: want = fp.read() got1 = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got1) simulator_status = verify.one_example(outfile_command2, want_file_name=None, pytest_options=None) got2 = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got2)
def test_outfile_to_stdout(): """Make sure generated --outfile and --report are as expected.""" outfile_command1 = labeled.contents(label="outfile-dash1") outfile_command2 = labeled.contents(label="outfile-dash2") simulator_status = verify.one_example(outfile_command1, want_file_name=None, pytest_options=None) with open("doc/test_example2.py", "r", encoding="utf-8") as fp: want = fp.read() got1 = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got1) simulator_status = verify.one_example(outfile_command2, want_file_name=None, pytest_options=None) got2 = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got2)
def test_skip_example(): """Make sure generated --outfile and --report are as expected.""" skip_command = next(readme_blocks) want = next(readme_blocks) # get the skip report short_form_command = next(readme_blocks) simulator_status = verify.one_example( skip_command, want_file_name='doc/test_example2.py', pytest_options=None) got1 = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got1) # test the first -s form of the --skip simulator_status = verify.one_example( short_form_command, want_file_name='doc/test_example2.py', pytest_options=None) got2 = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got2)
def test_skip_example(): """Make sure generated --outfile and --report are as expected.""" skip_command = labeled.contents(label="skip-command") want = labeled.contents(label="skip-report") short_form_command = labeled.contents(label="short-skip-command") simulator_status = verify.one_example( skip_command, want_file_name="doc/test_example2.py", pytest_options=None) got1 = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got1) # test the first -s form of the --skip simulator_status = verify.one_example( short_form_command, want_file_name="doc/test_example2.py", pytest_options=None) got2 = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got2)
def test_directive3_example(): """Make sure generated --outfile and --report are as expected.""" # Note that the report_command is hard coded here. # The command shown in README.md is not tested. report_command = "phmdoctest doc/directive3.md --report" directive_command = labeled.contents(label="directive-3-outfile") _ = verify.one_example(directive_command, want_file_name="doc/test_directive3.py", pytest_options=None) with open("doc/directive3_report.txt") as f: want = f.read() simulator_status = verify.one_example(report_command, want_file_name=None, pytest_options=None) got = simulator_status.runner_status.stdout verify.a_and_b_are_the_same(want, got)
def test_omit_to_end(): code = """\ def myfunc(): import math mylist = [1, 2, 3] a, b = 10, 11 def doubler(x): # phmdoctest:omit if x > 5: x += 2 #x += 3 x += 4 # comment x = 9 return x * 2 """ want = """\ def myfunc(): import math mylist = [1, 2, 3] a, b = 10, 11 # def doubler(x): # phmdoctest:omit # if x > 5: # x += 2 # #x += 3 # x += 4 # # comment # x = 9 # # return x * 2 """ got, num_changed_sections = phmdoctest.inline.apply_inline_commands(code) assert num_changed_sections == 1 verify.a_and_b_are_the_same(want, got) ast.parse(code) ast.parse(want)
def test_usage(): """Example usage near the bottom.""" # Note- There may be differences in whitespace between # the README.md fenced code block and the # phmdoctest --help output caused by the # the terminal width when the installed # phmdoctest command is executed. # Note- This test runs phmdoctest by calling the simulator # which calls Click.CliRunner.invoke(). invoke() # displays entry-point as the calling program. # The test here replaces 'entry-point' with 'phmdoctest'. simulator_status = phmdoctest.simulator.run_and_pytest("phmdoctest --help", pytest_options=None) want1 = labeled.contents(label="usage") want2 = re.sub(r"\s+", " ", want1) got1 = simulator_status.runner_status.stdout got2 = got1.replace("entry-point", "phmdoctest", 1) got3 = re.sub(r"\s+", " ", got2) verify.a_and_b_are_the_same(want2, got3)
def test_yaml(): """Show Markdown example and .travis.yml have the same commands.""" markdown_example_text = next(readme_blocks) expected = """\ dist: xenial language: python sudo: false matrix: include: - python: 3.5 install: - pip install "." pytest script: - mkdir tests/tmp - phmdoctest project.md --report --outfile tests/tmp/test_project.py - pytest --strict --doctest-modules -vv tests""" verify.a_and_b_are_the_same(expected, markdown_example_text) with open('.travis.yml', 'r', encoding='utf-8') as f: travis_text = f.read() assert travis_text.startswith(expected)
def test_pass_on_last_line(): code = """\ def myfunc(): import math mylist = [1, 2, 3] a, b = 10, 11 # phmdoctest:pass """ want = """\ def myfunc(): import math mylist = [1, 2, 3] pass # a, b = 10, 11 # phmdoctest:pass """ got, num_changed_sections = phmdoctest.inline.apply_inline_commands(code) assert num_changed_sections == 1 verify.a_and_b_are_the_same(want, got) ast.parse(code) ast.parse(want)
def test_omit_line_before_blank(): code = """\ def myfunc(): import math mylist = [1, 2, 3] a, b = 10, 11 # phmdoctest:omit def doubler(x): if x > 5: x += 2 #x += 3 x += 4 # comment return x """ want = """\ def myfunc(): import math mylist = [1, 2, 3] # a, b = 10, 11 # phmdoctest:omit def doubler(x): if x > 5: x += 2 #x += 3 x += 4 # comment return x """ got, num_changed_sections = phmdoctest.inline.apply_inline_commands(code) assert num_changed_sections == 1 verify.a_and_b_are_the_same(want, got) ast.parse(code) ast.parse(want)
def test_example1(): """Make sure generated --outfile is as expected; Run pytest. Check the copy of test_example1.py in the fenced code block. """ # The helper checks the generated --outfile against the disk file. example1_command = next(readme_blocks) want = next(readme_blocks) _ = verify.one_example(example1_command, want_file_name='doc/test_example1.py', pytest_options=None) # Make sure the copy of test_example1.py in README.md # is the same as the disk file. with open('doc/test_example1.py') as fp: got = fp.read() verify.a_and_b_are_the_same(want, got) # Run again and call pytest to make sure the file works with pytest. simulator_status = verify.one_example( example1_command, want_file_name=None, pytest_options=['--strict', '--doctest-modules', '-v']) assert simulator_status.pytest_exit_code == 0
def test_example1(): """Make sure generated --outfile is as expected; Run pytest. Check the copy of test_example1.py in the fenced code block. """ # The helper checks the generated --outfile against the disk file. example1_command = labeled.contents(label="example1-command") want = labeled.contents(label="example1-outfile") _ = verify.one_example(example1_command, want_file_name="doc/test_example1.py", pytest_options=None) # Make sure the copy of test_example1.py in README.md # is the same as the disk file. with open("doc/test_example1.py", "r", encoding="utf-8") as fp: got = fp.read() verify.a_and_b_are_the_same(want, got) # Run again and call pytest to make sure the file works with pytest. simulator_status = verify.one_example( example1_command, want_file_name=None, pytest_options=["--doctest-modules", "-v"], ) assert simulator_status.pytest_exit_code == 0