Skip to content

Commit ffd9471

Browse files
authored
🧪 Add tests for variants in links (useblocks#1669)
1 parent f183dde commit ffd9471

2 files changed

Lines changed: 248 additions & 0 deletions

File tree

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
variant_resolve_match:
2+
conf: |
3+
extensions = ["sphinx_needs"]
4+
needs_from_toml = "ubproject.toml"
5+
ubproject: |
6+
[needs.fields.assignee]
7+
nullable = true
8+
9+
[needs.links.links]
10+
outgoing = "links"
11+
incoming = "linked by"
12+
parse_variants = true
13+
rst: |
14+
.. spec:: Spec A
15+
:id: SPEC_1
16+
17+
.. spec:: Spec B
18+
:id: SPEC_2
19+
20+
.. impl:: Implementation
21+
:id: IMPL_1
22+
:assignee: Alice
23+
:links: <<[assignee == 'Alice']:SPEC_1, SPEC_2>>
24+
expect:
25+
IMPL_1:
26+
assignee: Alice
27+
links:
28+
- SPEC_1
29+
30+
variant_resolve_match_multiple:
31+
conf: |
32+
extensions = ["sphinx_needs"]
33+
needs_from_toml = "ubproject.toml"
34+
ubproject: |
35+
[needs.fields.assignee]
36+
nullable = true
37+
38+
[needs.links.links]
39+
outgoing = "links"
40+
incoming = "linked by"
41+
parse_variants = true
42+
rst: |
43+
.. spec:: Spec A
44+
:id: SPEC_1
45+
46+
.. spec:: Spec B
47+
:id: SPEC_2
48+
49+
.. impl:: Implementation
50+
:id: IMPL_1
51+
:assignee: Alice
52+
:links: <<[assignee == 'Alice']:SPEC_1, SPEC_2>>, <<[assignee == 'Bob']:SPEC_1, SPEC_2>>
53+
expect:
54+
IMPL_1:
55+
assignee: Alice
56+
links:
57+
- SPEC_1
58+
- SPEC_2
59+
60+
variant_resolve_fallback:
61+
conf: |
62+
extensions = ["sphinx_needs"]
63+
needs_from_toml = "ubproject.toml"
64+
ubproject: |
65+
[needs.fields.assignee]
66+
nullable = true
67+
68+
[needs.links.links]
69+
outgoing = "links"
70+
incoming = "linked by"
71+
parse_variants = true
72+
rst: |
73+
.. spec:: Spec A
74+
:id: SPEC_1
75+
76+
.. spec:: Spec B
77+
:id: SPEC_2
78+
79+
.. impl:: Implementation
80+
:id: IMPL_1
81+
:assignee: Bob
82+
:links: <<[assignee == 'Alice']:SPEC_1, SPEC_2>>
83+
expect:
84+
IMPL_1:
85+
assignee: Bob
86+
links:
87+
- SPEC_2
88+
89+
variant_named:
90+
conf: |
91+
extensions = ["sphinx_needs"]
92+
needs_from_toml = "ubproject.toml"
93+
ubproject: |
94+
[needs.variants]
95+
is_alice = "assignee == 'Alice'"
96+
97+
[needs.fields.assignee]
98+
nullable = true
99+
100+
[needs.links.links]
101+
outgoing = "links"
102+
incoming = "linked by"
103+
parse_variants = true
104+
rst: |
105+
.. spec:: Spec A
106+
:id: SPEC_1
107+
108+
.. spec:: Spec B
109+
:id: SPEC_2
110+
111+
.. impl:: Implementation
112+
:id: IMPL_1
113+
:assignee: Alice
114+
:links: <<is_alice:SPEC_1, SPEC_2>>
115+
expect:
116+
IMPL_1:
117+
assignee: Alice
118+
links:
119+
- SPEC_1
120+
121+
variant_named_fallback:
122+
conf: |
123+
extensions = ["sphinx_needs"]
124+
needs_from_toml = "ubproject.toml"
125+
ubproject: |
126+
[needs.variants]
127+
is_alice = "assignee == 'Alice'"
128+
129+
[needs.fields.assignee]
130+
nullable = true
131+
132+
[needs.links.links]
133+
outgoing = "links"
134+
incoming = "linked by"
135+
parse_variants = true
136+
rst: |
137+
.. spec:: Spec A
138+
:id: SPEC_1
139+
140+
.. spec:: Spec B
141+
:id: SPEC_2
142+
143+
.. impl:: Implementation
144+
:id: IMPL_1
145+
:assignee: Bob
146+
:links: <<is_alice:SPEC_1, SPEC_2>>
147+
expect:
148+
IMPL_1:
149+
assignee: Bob
150+
links:
151+
- SPEC_2
152+
153+
variant_multiple_expressions:
154+
conf: |
155+
extensions = ["sphinx_needs"]
156+
needs_from_toml = "ubproject.toml"
157+
ubproject: |
158+
[needs.fields.role]
159+
nullable = true
160+
161+
[needs.links.links]
162+
outgoing = "links"
163+
incoming = "linked by"
164+
parse_variants = true
165+
rst: |
166+
.. spec:: Spec A
167+
:id: SPEC_1
168+
169+
.. spec:: Spec B
170+
:id: SPEC_2
171+
172+
.. spec:: Spec C
173+
:id: SPEC_3
174+
175+
.. impl:: Implementation
176+
:id: IMPL_1
177+
:role: developer
178+
:links: <<[role == 'manager']:SPEC_1, [role == 'developer']:SPEC_2, SPEC_3>>
179+
expect:
180+
IMPL_1:
181+
role: developer
182+
links:
183+
- SPEC_2
184+
185+
variant_disabled:
186+
conf: |
187+
extensions = ["sphinx_needs"]
188+
needs_from_toml = "ubproject.toml"
189+
ubproject: |
190+
[needs.fields.assignee]
191+
nullable = true
192+
193+
[needs.links.links]
194+
outgoing = "links"
195+
incoming = "linked by"
196+
rst: |
197+
.. spec:: Spec A
198+
:id: SPEC_1
199+
200+
.. impl:: Implementation
201+
:id: IMPL_1
202+
:assignee: Alice
203+
:links: SPEC_1
204+
expect:
205+
IMPL_1:
206+
assignee: Alice
207+
links:
208+
- SPEC_1
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from collections.abc import Callable
2+
from pathlib import Path
3+
from typing import Any
4+
5+
import pytest
6+
from sphinx.testing.util import SphinxTestApp
7+
8+
from sphinx_needs.api import get_needs_view
9+
10+
CURR_DIR = Path(__file__).parent
11+
12+
13+
@pytest.mark.fixture_file(
14+
"variants/fixtures/link_variants.yml",
15+
)
16+
def test_link_variants(
17+
tmpdir: Path,
18+
content: dict[str, Any],
19+
make_app: Callable[[], SphinxTestApp],
20+
write_fixture_files: Callable[[Path, dict[str, Any]], None],
21+
) -> None:
22+
write_fixture_files(tmpdir, content)
23+
24+
app: SphinxTestApp = make_app(srcdir=Path(tmpdir), freshenv=True)
25+
app.build()
26+
assert app.statuscode == 0
27+
28+
view = get_needs_view(app)
29+
30+
expect: dict[str, dict[str, Any]] = content["expect"]
31+
for need_id, expected_fields in expect.items():
32+
need = view[need_id]
33+
for field, expected_value in expected_fields.items():
34+
actual = need[field]
35+
if isinstance(expected_value, list):
36+
actual = list(actual)
37+
assert actual == expected_value, (
38+
f"Need {need_id!r} field {field!r}: expected {expected_value}, got {actual}"
39+
)
40+
app.cleanup()

0 commit comments

Comments
 (0)