Coverage for tests/test_python.py: 100.00%
34 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-17 16:01 +0200
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-17 16:01 +0200
1"""Tests for the Python formatters."""
3from __future__ import annotations
5from textwrap import dedent
6from typing import TYPE_CHECKING
8if TYPE_CHECKING:
9 import pytest
10 from markdown import Markdown
13def test_output_markdown(md: Markdown) -> None:
14 """Assert Markdown is converted to HTML.
16 Parameters:
17 md: A Markdown instance (fixture).
18 """
19 html = md.convert(
20 dedent(
21 """
22 ```python exec="yes"
23 print("**Bold!**")
24 ```
25 """,
26 ),
27 )
28 assert html == "<p><strong>Bold!</strong></p>"
31def test_output_html(md: Markdown) -> None:
32 """Assert HTML is injected as is.
34 Parameters:
35 md: A Markdown instance (fixture).
36 """
37 html = md.convert(
38 dedent(
39 """
40 ```python exec="yes" html="yes"
41 print("**Bold!**")
42 ```
43 """,
44 ),
45 )
46 assert html == "<p>**Bold!**\n</p>"
49def test_error_raised(md: Markdown, caplog: pytest.LogCaptureFixture) -> None:
50 """Assert errors properly log a warning and return a formatted traceback.
52 Parameters:
53 md: A Markdown instance (fixture).
54 caplog: Pytest fixture to capture logs.
55 """
56 html = md.convert(
57 dedent(
58 """
59 ```python exec="yes"
60 raise ValueError("oh no!")
61 ```
62 """,
63 ),
64 )
65 assert "Traceback" in html
66 assert "ValueError" in html
67 assert "oh no!" in html
68 assert "Execution of python code block exited with errors" in caplog.text
71def test_can_print_non_string_objects(md: Markdown) -> None:
72 """Assert we can print non-string objects.
74 Parameters:
75 md: A Markdown instance (fixture).
76 """
77 html = md.convert(
78 dedent(
79 """
80 ```python exec="yes"
81 class NonString:
82 def __str__(self):
83 return "string"
85 nonstring = NonString()
86 print(nonstring, nonstring)
87 ```
88 """,
89 ),
90 )
91 assert "Traceback" not in html
94def test_sessions(md: Markdown) -> None:
95 """Assert sessions can be reused.
97 Parameters:
98 md: A Markdown instance (fixture).
99 """
100 html = md.convert(
101 dedent(
102 """
103 ```python exec="1" session="a"
104 a = 1
105 ```
107 ```pycon exec="1" session="b"
108 >>> b = 2
109 ```
111 ```pycon exec="1" session="a"
112 >>> print(f"a = {a}")
113 >>> try:
114 ... print(b)
115 ... except NameError:
116 ... print("ok")
117 ... else:
118 ... print("ko")
119 ```
121 ```python exec="1" session="b"
122 print(f"b = {b}")
123 try:
124 print(a)
125 except NameError:
126 print("ok")
127 else:
128 print("ko")
129 ```
130 """,
131 ),
132 )
133 assert "a = 1" in html
134 assert "b = 2" in html
135 assert "ok" in html
136 assert "ko" not in html
139def test_reporting_errors_in_sessions(md: Markdown, caplog: pytest.LogCaptureFixture) -> None:
140 """Assert errors and source lines are correctly reported across sessions.
142 Parameters:
143 md: A Markdown instance (fixture).
144 caplog: Pytest fixture to capture logs.
145 """
146 html = md.convert(
147 dedent(
148 """
149 ```python exec="1" session="a"
150 def fraise():
151 raise RuntimeError("strawberry")
152 ```
154 ```python exec="1" session="a"
155 print("hello")
156 fraise()
157 ```
158 """,
159 ),
160 )
161 assert "Traceback" in html
162 assert "strawberry" in html
163 assert "fraise()" in caplog.text
164 assert 'raise RuntimeError("strawberry")' in caplog.text
167def test_removing_output_from_pycon_code(md: Markdown) -> None:
168 """Assert output lines are removed from pycon snippets.
170 Parameters:
171 md: A Markdown instance (fixture).
172 """
173 html = md.convert(
174 dedent(
175 """
176 ```pycon exec="1" source="console"
177 >>> print("ok")
178 ko
179 ```
180 """,
181 ),
182 )
183 assert "ok" in html
184 assert "ko" not in html