|
1 | 1 | import pytest |
2 | 2 |
|
| 3 | +from textual.app import App |
3 | 4 | from textual.css.errors import StyleValueError |
4 | 5 | from textual.dom import BadIdentifier, DOMNode |
| 6 | +from textual.widget import Widget |
| 7 | +from textual.widgets import Input, Select, Static |
5 | 8 |
|
6 | 9 |
|
7 | 10 | def test_display_default(): |
@@ -280,3 +283,78 @@ def test_id_validation(identifier: str): |
280 | 283 | """Regression tests for https://github.com/Textualize/textual/issues/3954.""" |
281 | 284 | with pytest.raises(BadIdentifier): |
282 | 285 | DOMNode(id=identifier) |
| 286 | + |
| 287 | + |
| 288 | +class SimpleApp(App): |
| 289 | + def compose(self): |
| 290 | + yield Input(id="input1") |
| 291 | + yield Select([], id="select1") |
| 292 | + yield Static("Hello", id="static1") |
| 293 | + yield Input(id="input2") |
| 294 | + |
| 295 | + |
| 296 | +async def test_query_union_type(): |
| 297 | + # Test with a UnionType |
| 298 | + simple_app = SimpleApp() |
| 299 | + async with simple_app.run_test(): |
| 300 | + results = simple_app.query(Input | Select) |
| 301 | + assert len(results) == 3 |
| 302 | + assert {w.id for w in results} == {"input1", "select1", "input2"} |
| 303 | + |
| 304 | + # Test with a single type |
| 305 | + results2 = simple_app.query(Input) |
| 306 | + assert len(results2) == 2 |
| 307 | + assert {w.id for w in results2} == {"input1", "input2"} |
| 308 | + |
| 309 | + # Test with string selector |
| 310 | + results3 = simple_app.query("#input1") |
| 311 | + assert len(results3) == 1 |
| 312 | + assert results3[0].id == "input1" |
| 313 | + |
| 314 | + |
| 315 | +async def test_query_nested_unions(): |
| 316 | + """Test handling of nested unions.""" |
| 317 | + |
| 318 | + simple_app = SimpleApp() |
| 319 | + async with simple_app.run_test(): |
| 320 | + # Create nested union types |
| 321 | + InputOrSelect = Input | Select |
| 322 | + InputSelectOrStatic = InputOrSelect | Static |
| 323 | + |
| 324 | + # Test nested union query |
| 325 | + results = simple_app.query(InputSelectOrStatic) |
| 326 | + |
| 327 | + # Verify that we find all our explicitly defined widgets |
| 328 | + widget_ids = {w.id for w in results if w.id is not None} |
| 329 | + expected_ids = {"input1", "select1", "static1", "input2"} |
| 330 | + assert expected_ids.issubset(widget_ids), "Not all expected widgets were found" |
| 331 | + |
| 332 | + # Verify we get the right types of widgets |
| 333 | + assert all( |
| 334 | + isinstance(w, (Input, Select, Static)) for w in results |
| 335 | + ), "Found unexpected widget types" |
| 336 | + |
| 337 | + # Verify each expected widget appears exactly once |
| 338 | + for expected_id in expected_ids: |
| 339 | + matching_widgets = [w for w in results if w.id == expected_id] |
| 340 | + assert ( |
| 341 | + len(matching_widgets) == 1 |
| 342 | + ), f"Widget with id {expected_id} should appear exactly once" |
| 343 | + |
| 344 | + |
| 345 | +async def test_query_empty_union(): |
| 346 | + """Test querying with empty or invalid unions.""" |
| 347 | + |
| 348 | + class AnotherWidget(Widget): |
| 349 | + pass |
| 350 | + |
| 351 | + simple_app = SimpleApp() |
| 352 | + async with simple_app.run_test(): |
| 353 | + |
| 354 | + # Test with a type that exists but has no matches |
| 355 | + results = simple_app.query(AnotherWidget) |
| 356 | + assert len(results) == 0 |
| 357 | + |
| 358 | + # Test with widget union that has no matches |
| 359 | + results = simple_app.query(AnotherWidget | AnotherWidget) |
| 360 | + assert len(results) == 0 |
0 commit comments