Skip to content

Commit dfeb934

Browse files
authored
Merge branch 'main' into 52-display-parser-warnings
2 parents 764a914 + bf3bd2c commit dfeb934

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

src/sphinx_codelinks/analyse/oneline_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def oneline_parser( # noqa: PLR0912, PLR0911 # handel warnings
5454

5555
# extract the string wrapped by start and end
5656
start_idx = start_idx + len(oneline_config.start_sequence)
57-
string = oneline[start_idx:end_idx]
57+
string = oneline[start_idx:end_idx].strip()
5858

5959
# numbers of needs_fields which are required
6060
cnt_required_fields = oneline_config.get_cnt_required_fields()

tests/test_oneline_parser.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,54 @@
2525
"end_column": 17,
2626
},
2727
),
28+
# Test case for leading space after start sequence (bug fix)
29+
(
30+
f"@ title 1, IMPL_1 {UNIX_NEWLINE}",
31+
{
32+
"title": "title 1",
33+
"id": "IMPL_1",
34+
"type": "impl",
35+
"links": [],
36+
"start_column": 1,
37+
"end_column": 18,
38+
},
39+
),
40+
# Test case for multiple leading spaces after start sequence
41+
(
42+
f"@ title 1, IMPL_1 {UNIX_NEWLINE}",
43+
{
44+
"title": "title 1",
45+
"id": "IMPL_1",
46+
"type": "impl",
47+
"links": [],
48+
"start_column": 1,
49+
"end_column": 20,
50+
},
51+
),
52+
# Test case for trailing space before end sequence
53+
(
54+
f"@title 1, IMPL_1 {UNIX_NEWLINE}",
55+
{
56+
"title": "title 1",
57+
"id": "IMPL_1",
58+
"type": "impl",
59+
"links": [],
60+
"start_column": 1,
61+
"end_column": 18,
62+
},
63+
),
64+
# Test case for both leading and trailing spaces
65+
(
66+
f"@ title 1, IMPL_1 {UNIX_NEWLINE}",
67+
{
68+
"title": "title 1",
69+
"id": "IMPL_1",
70+
"type": "impl",
71+
"links": [],
72+
"start_column": 1,
73+
"end_column": 21,
74+
},
75+
),
2876
],
2977
)
3078
def test_oneline_parser_default_config_positive(
@@ -34,6 +82,99 @@ def test_oneline_parser_default_config_positive(
3482
assert oneline_need == result
3583

3684

85+
# Test case for space as field separator (as mentioned by Kilian)
86+
# Example: @Implementation <field1> <field2>
87+
ONELINE_COMMENT_STYLE_SPACE_SEPARATOR = OneLineCommentStyle(
88+
start_sequence="@",
89+
end_sequence=UNIX_NEWLINE,
90+
field_split_char=" ",
91+
needs_fields=[
92+
{"name": "title"},
93+
{"name": "id"},
94+
{"name": "type", "default": "impl"},
95+
],
96+
)
97+
98+
99+
@pytest.mark.parametrize(
100+
"oneline, result",
101+
[
102+
# Basic space-separated fields
103+
(
104+
f"@Implementation IMPL_1{UNIX_NEWLINE}",
105+
{
106+
"title": "Implementation",
107+
"id": "IMPL_1",
108+
"type": "impl",
109+
"start_column": 1,
110+
"end_column": 22,
111+
},
112+
),
113+
# Space separator with explicit type
114+
(
115+
f"@MyFeature FEAT_001 feature{UNIX_NEWLINE}",
116+
{
117+
"title": "MyFeature",
118+
"id": "FEAT_001",
119+
"type": "feature",
120+
"start_column": 1,
121+
"end_column": 27,
122+
},
123+
),
124+
# Leading space after @ (the bug Kilian reported)
125+
(
126+
f"@ Implementation IMPL_2{UNIX_NEWLINE}",
127+
{
128+
"title": "Implementation",
129+
"id": "IMPL_2",
130+
"type": "impl",
131+
"start_column": 1,
132+
"end_column": 23,
133+
},
134+
),
135+
# Trailing space before newline
136+
(
137+
f"@Implementation IMPL_3 {UNIX_NEWLINE}",
138+
{
139+
"title": "Implementation",
140+
"id": "IMPL_3",
141+
"type": "impl",
142+
"start_column": 1,
143+
"end_column": 23,
144+
},
145+
),
146+
# Multiple leading spaces after @
147+
(
148+
f"@ Title ID_456{UNIX_NEWLINE}",
149+
{
150+
"title": "Title",
151+
"id": "ID_456",
152+
"type": "impl",
153+
"start_column": 1,
154+
"end_column": 15,
155+
},
156+
),
157+
# Title contain spaces
158+
(
159+
f"@ Title\ escape\ space ID_456{UNIX_NEWLINE}",
160+
{
161+
"title": "Title escape space",
162+
"id": "ID_456",
163+
"type": "impl",
164+
"start_column": 1,
165+
"end_column": 30,
166+
},
167+
),
168+
],
169+
)
170+
def test_oneline_parser_space_separator(
171+
oneline: str, result: dict[str, str | list[str]]
172+
) -> None:
173+
"""Test oneline parser with space as field separator."""
174+
oneline_need = oneline_parser(oneline, ONELINE_COMMENT_STYLE_SPACE_SEPARATOR)
175+
assert oneline_need == result
176+
177+
37178
@pytest.mark.parametrize(
38179
"oneline, result",
39180
[

0 commit comments

Comments
 (0)