|
| 1 | +from typing import Literal |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 |
|
3 | 5 | from src.script_metadata import ScriptMetadata
|
4 | 6 | from src.script_metadata_extractor import ScriptMetadataExtractor
|
5 |
| -from tests.conftest import create_script_metadata |
6 | 7 |
|
7 |
| -test_cases = [ |
8 |
| - pytest.param( |
9 |
| - ["```python:main.py", "print('Hello, World!')", "```"], |
10 |
| - [create_script_metadata(0, 2, "main.py")], |
11 |
| - id="one_tagged_script", |
12 |
| - ), |
13 |
| - pytest.param(["```", "print('Hello, World!')", "```"], [], id="one_untagged_script"), |
14 |
| - pytest.param([], [], id="empty_readme"), |
15 |
| - pytest.param( |
16 |
| - ["```javascript", "console.log('Hello, World!');", "```"], |
17 |
| - [], |
18 |
| - id="one_untagged_script_language_specified", |
19 |
| - ), |
20 |
| - pytest.param( |
21 |
| - [ |
22 |
| - "```python:example.py", |
23 |
| - "import os", |
24 |
| - "print('Hello, World!')", |
25 |
| - "```", |
26 |
| - "```", |
27 |
| - "print('Do not replace')", |
28 |
| - "```", |
29 |
| - ], |
30 |
| - [create_script_metadata(0, 3, "example.py")], |
31 |
| - id="one_tagged_script_one_untagged_script", |
32 |
| - ), |
33 |
| - pytest.param( |
34 |
| - [ |
35 |
| - "```python:main.py", |
36 |
| - "print('Hello, World!')", |
37 |
| - "```", |
38 |
| - "```python:example.py", |
39 |
| - "import os", |
40 |
| - "print('Hello, World!')", |
41 |
| - "```", |
42 |
| - "```", |
43 |
| - "print('Do not replace')", |
44 |
| - "```", |
45 |
| - ], |
46 |
| - [ |
47 |
| - create_script_metadata(0, 2, "main.py"), |
48 |
| - create_script_metadata(3, 6, "example.py"), |
49 |
| - ], |
50 |
| - id="two_tagged_scripts_one_untagged_script", |
51 |
| - ), |
52 |
| - pytest.param( |
53 |
| - ["```python:main.py:s:section_name", "print('Hello, World!')", "```"], |
54 |
| - [create_script_metadata(0, 2, "main.py", "section", "section_name")], |
55 |
| - id="one_tagged_script_with_section_name", |
56 |
| - ), |
57 |
| - pytest.param( |
58 |
| - [ |
59 |
| - "```python:main.py:s:section_name", |
60 |
| - "print('Hello, World!')", |
61 |
| - "```", |
62 |
| - "```python:example.py", |
63 |
| - "print('Hello, World!')", |
64 |
| - "```", |
65 |
| - ], |
66 |
| - [ |
67 |
| - create_script_metadata(0, 2, "main.py", "section", "section_name"), |
68 |
| - create_script_metadata(3, 5, "example.py"), |
69 |
| - ], |
70 |
| - id="one_tagged_script_with_section_name_one_tagged_script", |
71 |
| - ), |
72 |
| - pytest.param( |
73 |
| - [ |
74 |
| - "```python:main.py:s:A", |
75 |
| - "print('Hello, World!')", |
76 |
| - "```", |
77 |
| - "```python:example.py:s:B", |
78 |
| - "print('Hello, World!')", |
79 |
| - "```", |
80 |
| - ], |
81 |
| - [ |
82 |
| - create_script_metadata(0, 2, "main.py", "section", "A"), |
83 |
| - create_script_metadata(3, 5, "example.py", "section", "B"), |
84 |
| - ], |
85 |
| - id="two_tagged_scripts_with_section_names", |
86 |
| - ), |
87 |
| - pytest.param( |
88 |
| - [ |
89 |
| - "```python:main.py:o:print_hello", |
90 |
| - "def print_hello() -> None:", |
91 |
| - " print('Hello, World!')", |
92 |
| - "```", |
93 |
| - ], |
94 |
| - [ |
95 |
| - create_script_metadata(0, 3, "main.py", "object", "print_hello"), |
96 |
| - ], |
97 |
| - id="one_tagged_script_with_object_name", |
98 |
| - ), |
99 |
| -] |
| 8 | + |
| 9 | +def create_script_metadata( |
| 10 | + readme_start: int, |
| 11 | + readme_end: int, |
| 12 | + path: str, |
| 13 | + extraction_type: Literal["section", "object", "full"] = "full", |
| 14 | + extraction_part: str | None = None, |
| 15 | + content: str = "", |
| 16 | +) -> ScriptMetadata: |
| 17 | + return ScriptMetadata( |
| 18 | + readme_start=readme_start, |
| 19 | + readme_end=readme_end, |
| 20 | + path=path, |
| 21 | + extraction_type=extraction_type, |
| 22 | + extraction_part=extraction_part, |
| 23 | + content=content, |
| 24 | + ) |
100 | 25 |
|
101 | 26 |
|
102 |
| -@pytest.mark.parametrize("readme_content, expected", test_cases) |
| 27 | +@pytest.mark.parametrize( |
| 28 | + "readme_content, expected_script_metadata", |
| 29 | + [ |
| 30 | + ( |
| 31 | + ["```python:main.py", "print('Hello, World!')", "```"], |
| 32 | + [create_script_metadata(readme_start=0, readme_end=2, path="main.py")], |
| 33 | + ), |
| 34 | + ( |
| 35 | + ["```", "print('Hello, World!')", "```"], |
| 36 | + [], |
| 37 | + ), |
| 38 | + ([], []), |
| 39 | + ( |
| 40 | + ["```javascript", "console.log('Hello, World!');", "```"], |
| 41 | + [], |
| 42 | + ), |
| 43 | + ( |
| 44 | + [ |
| 45 | + "```python:example.py", |
| 46 | + "import os", |
| 47 | + "print('Hello, World!')", |
| 48 | + "```", |
| 49 | + "```", |
| 50 | + "print('Do not replace')", |
| 51 | + "```", |
| 52 | + ], |
| 53 | + [create_script_metadata(readme_start=0, readme_end=3, path="example.py")], |
| 54 | + ), |
| 55 | + ( |
| 56 | + [ |
| 57 | + "```python:main.py", |
| 58 | + "print('Hello, World!')", |
| 59 | + "```", |
| 60 | + "```python:example.py", |
| 61 | + "import os", |
| 62 | + "print('Hello, World!')", |
| 63 | + "```", |
| 64 | + "```", |
| 65 | + "print('Do not replace')", |
| 66 | + "```", |
| 67 | + ], |
| 68 | + [ |
| 69 | + create_script_metadata(readme_start=0, readme_end=2, path="main.py"), |
| 70 | + create_script_metadata(readme_start=3, readme_end=6, path="example.py"), |
| 71 | + ], |
| 72 | + ), |
| 73 | + ( |
| 74 | + ["```python:main.py:s:section_name", "print('Hello, World!')", "```"], |
| 75 | + [ |
| 76 | + create_script_metadata( |
| 77 | + readme_start=0, |
| 78 | + readme_end=2, |
| 79 | + path="main.py", |
| 80 | + extraction_type="section", |
| 81 | + extraction_part="section_name", |
| 82 | + ) |
| 83 | + ], |
| 84 | + ), |
| 85 | + ( |
| 86 | + [ |
| 87 | + "```python:main.py:s:section_name", |
| 88 | + "print('Hello, World!')", |
| 89 | + "```", |
| 90 | + "```python:example.py", |
| 91 | + "print('Hello, World!')", |
| 92 | + "```", |
| 93 | + ], |
| 94 | + [ |
| 95 | + create_script_metadata( |
| 96 | + readme_start=0, |
| 97 | + readme_end=2, |
| 98 | + path="main.py", |
| 99 | + extraction_type="section", |
| 100 | + extraction_part="section_name", |
| 101 | + ), |
| 102 | + create_script_metadata(readme_start=3, readme_end=5, path="example.py"), |
| 103 | + ], |
| 104 | + ), |
| 105 | + ( |
| 106 | + [ |
| 107 | + "```python:main.py:s:A", |
| 108 | + "print('Hello, World!')", |
| 109 | + "```", |
| 110 | + "```python:example.py:s:B", |
| 111 | + "print('Hello, World!')", |
| 112 | + "```", |
| 113 | + ], |
| 114 | + [ |
| 115 | + create_script_metadata( |
| 116 | + readme_start=0, |
| 117 | + readme_end=2, |
| 118 | + path="main.py", |
| 119 | + extraction_type="section", |
| 120 | + extraction_part="A", |
| 121 | + ), |
| 122 | + create_script_metadata( |
| 123 | + readme_start=3, |
| 124 | + readme_end=5, |
| 125 | + path="example.py", |
| 126 | + extraction_type="section", |
| 127 | + extraction_part="B", |
| 128 | + ), |
| 129 | + ], |
| 130 | + ), |
| 131 | + ( |
| 132 | + [ |
| 133 | + "```python:main.py:o:print_hello", |
| 134 | + "def print_hello() -> None:", |
| 135 | + " print('Hello, World!')", |
| 136 | + "```", |
| 137 | + ], |
| 138 | + [ |
| 139 | + create_script_metadata( |
| 140 | + readme_start=0, |
| 141 | + readme_end=3, |
| 142 | + path="main.py", |
| 143 | + extraction_type="object", |
| 144 | + extraction_part="print_hello", |
| 145 | + ), |
| 146 | + ], |
| 147 | + ), |
| 148 | + ], |
| 149 | + ids=[ |
| 150 | + "one_tagged_script", |
| 151 | + "one_untagged_script", |
| 152 | + "empty_readme", |
| 153 | + "one_untagged_script_language_specified", |
| 154 | + "one_tagged_script_one_untagged_script", |
| 155 | + "two_tagged_scripts_one_untagged_script", |
| 156 | + "one_tagged_script_with_section_name", |
| 157 | + "one_tagged_script_with_section_name_one_tagged_script", |
| 158 | + "two_tagged_scripts_with_section_names", |
| 159 | + "one_tagged_script_with_object_name", |
| 160 | + ], |
| 161 | +) |
103 | 162 | def test_script_path_extractor(
|
104 |
| - readme_content: list[str], expected: list[ScriptMetadata] |
| 163 | + readme_content: list[str], expected_script_metadata: list[ScriptMetadata] |
105 | 164 | ) -> None:
|
106 | 165 | script_metadata_extractor = ScriptMetadataExtractor()
|
107 | 166 | result = script_metadata_extractor.extract(readme_content=readme_content)
|
108 |
| - assert result == expected |
| 167 | + assert result == expected_script_metadata |
0 commit comments