22
33from pathlib import Path
44from unittest .mock import patch
5+
56import pytest
67
78from markdown_code_runner import (
9+ ProcessingState ,
810 _extract_backtick_options ,
11+ main ,
912 process_markdown ,
1013 update_markdown_file ,
11- main ,
1214)
1315
14- def test_extract_backtick_options_without_markdown_code_runner ():
16+
17+ def test_extract_backtick_options_without_markdown_code_runner () -> None :
1518 """Test _extract_backtick_options with basic language extraction."""
1619 # Test simple language extraction
1720 assert _extract_backtick_options ("```python" ) == {"language" : "python" }
1821 assert _extract_backtick_options ("```javascript" ) == {"language" : "javascript" }
19-
22+
2023 # Test with spaces and other content
21- assert _extract_backtick_options ("```python some other text" ) == {"language" : "python" }
24+ assert _extract_backtick_options ("```python some other text" ) == {
25+ "language" : "python" ,
26+ }
2227 assert _extract_backtick_options ("```rust " ) == {"language" : "rust" }
23-
28+
2429 # Test invalid/empty cases
2530 assert _extract_backtick_options ("```" ) == {}
2631 assert _extract_backtick_options ("some random text" ) == {}
2732
28- def test_extract_backtick_options_with_markdown_code_runner ():
33+
34+ def test_extract_backtick_options_with_markdown_code_runner () -> None :
2935 """Test _extract_backtick_options with markdown-code-runner."""
3036 # Test with markdown-code-runner and options
3137 assert _extract_backtick_options (
32- "```python markdown-code-runner filename=test.py"
38+ "```python markdown-code-runner filename=test.py" ,
3339 ) == {
3440 "language" : "python" ,
35- "filename" : "test.py"
41+ "filename" : "test.py" ,
3642 }
37-
43+
3844 # Test with multiple options
3945 assert _extract_backtick_options (
40- "```javascript markdown-code-runner filename=test.js debug=true"
46+ "```javascript markdown-code-runner filename=test.js debug=true" ,
4147 ) == {
4248 "language" : "javascript" ,
4349 "filename" : "test.js" ,
44- "debug" : "true"
50+ "debug" : "true" ,
4551 }
4652
47- def test_process_markdown_standardization ():
53+
54+ def test_process_markdown_standardization () -> None :
4855 """Test process_markdown with standardization enabled/disabled."""
4956 input_lines = [
5057 "# Test markdown" ,
@@ -54,20 +61,21 @@ def test_process_markdown_standardization():
5461 "Some text" ,
5562 "```javascript" ,
5663 "console.log('hi')" ,
57- "```"
64+ "```" ,
5865 ]
59-
66+
6067 # Test with standardization enabled (default)
6168 output = process_markdown (input_lines )
6269 assert output [1 ] == "```python" # markdown-code-runner should be removed
6370 assert output [5 ] == "```javascript" # unchanged
64-
71+
6572 # Test with standardization disabled
6673 output = process_markdown (input_lines , backtick_standardize = False )
6774 assert output [1 ] == "```python markdown-code-runner filename=test.py" # preserved
6875 assert output [5 ] == "```javascript" # unchanged
6976
70- def test_process_markdown_mixed_blocks ():
77+
78+ def test_process_markdown_mixed_blocks () -> None :
7179 """Test process_markdown with mixed block types."""
7280 input_lines = [
7381 "# Mixed blocks" ,
@@ -81,26 +89,27 @@ def test_process_markdown_mixed_blocks():
8189 "Another runner block:" ,
8290 "```javascript markdown-code-runner filename=test.js" ,
8391 "let z = 3;" ,
84- "```"
92+ "```" ,
8593 ]
86-
94+
8795 # With standardization
8896 output = process_markdown (input_lines )
8997 assert output [1 ] == "```python"
9098 assert output [5 ] == "```python"
9199 assert output [9 ] == "```javascript"
92-
100+
93101 # Without standardization
94102 output = process_markdown (input_lines , backtick_standardize = False )
95103 assert output [1 ] == "```python markdown-code-runner filename=test.py debug=true"
96104 assert output [5 ] == "```python"
97105 assert output [9 ] == "```javascript markdown-code-runner filename=test.js"
98106
99- def test_update_markdown_file_standardization (tmp_path : Path ):
107+
108+ def test_update_markdown_file_standardization (tmp_path : Path ) -> None :
100109 """Test update_markdown_file with standardization options."""
101110 input_file = tmp_path / "test.md"
102111 output_file = tmp_path / "test_output.md"
103-
112+
104113 content = """# Test
105114```python markdown-code-runner filename=test.py
106115print('hello')
@@ -109,46 +118,55 @@ def test_update_markdown_file_standardization(tmp_path: Path):
109118```javascript
110119console.log('hi')
111120```"""
112-
121+
113122 input_file .write_text (content )
114-
123+
115124 # Test with output file (standardization enabled by default)
116125 update_markdown_file (input_file , output_file )
117126 output_content = output_file .read_text ()
118127 assert "markdown-code-runner" not in output_content
119128 assert "```python\n " in output_content
120-
129+
121130 # Test with output file (standardization disabled)
122131 update_markdown_file (input_file , output_file , backtick_standardize = False )
123132 output_content = output_file .read_text ()
124133 assert "markdown-code-runner" in output_content
125-
134+
126135 # Test overwrite with standardization (should require force_overwrite)
127- with pytest .raises (SystemExit ):
128- with patch ('sys.argv' , ['markdown-code-runner' , str (input_file ), '--backtick-standardize' ]):
129- main ()
130-
131- def test_process_backticks_start ():
132- """Test the _process_backticks_start method directly."""
133- from markdown_code_runner import ProcessingState
134-
136+ with (
137+ pytest .raises (SystemExit ),
138+ patch (
139+ "sys.argv" ,
140+ ["markdown-code-runner" , str (input_file ), "--backtick-standardize" ],
141+ ),
142+ ):
143+ main ()
144+
145+
146+ def test_process_backticks_start () -> None :
147+ """Test the backtick standardization logic via _process_start_markers."""
135148 # Test with standardization enabled
136149 state = ProcessingState (backtick_standardize = True )
137-
150+
138151 # Should remove markdown-code-runner and options
139152 line = "```python markdown-code-runner filename=test.py"
140- assert state ._process_backticks_start (line ) == "```python"
141-
153+ result = state ._process_start_markers (line )
154+ assert result == "```python"
155+
142156 # Should preserve non-markdown-code-runner content
143157 line = "```javascript some other content"
144- assert state ._process_backticks_start (line ) == line
145-
158+ result = state ._process_start_markers (line )
159+ assert result is None # Not a marker, so returns None
160+
146161 # Test with standardization disabled
147162 state = ProcessingState (backtick_standardize = False )
148-
163+
149164 # Should preserve everything
150165 line = "```python markdown-code-runner filename=test.py"
151- assert state ._process_backticks_start (line ) == line
152-
166+ result = state ._process_start_markers (line )
167+ assert result == line
168+
169+ # Non-marker line should return None
153170 line = "```javascript some other content"
154- assert state ._process_backticks_start (line ) == line
171+ result = state ._process_start_markers (line )
172+ assert result is None
0 commit comments