From bd55830d536d9e0a0e94b2e21d087a3dcd54cfec Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 25 Oct 2022 10:24:40 +0100 Subject: [PATCH 1/8] Add query tests relating to first/last --- tests/test_query.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_query.py b/tests/test_query.py index be6108046..c59b3cb41 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -1,4 +1,7 @@ +import pytest + from textual.widget import Widget +from textual.css.query import WrongType, NoMatches def test_query(): @@ -78,3 +81,16 @@ def test_query(): assert list(app.query("#widget1, #widget2")) == [widget1, widget2] assert list(app.query("#widget1 , #widget2")) == [widget1, widget2] assert list(app.query("#widget1, #widget2, App")) == [app, widget1, widget2] + + assert app.query(".float").first() == sidebar + assert app.query(".float").last() == helpbar + + with pytest.raises(NoMatches): + _ = app.query(".no_such_class").first() + with pytest.raises(NoMatches): + _ = app.query(".no_such_class").last() + + with pytest.raises(WrongType): + _ = app.query(".float").first(View) + with pytest.raises(WrongType): + _ = app.query(".float").last(View) From aa9f53fa5b3b3337328007034b13b088a30f5175 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 25 Oct 2022 10:29:10 +0100 Subject: [PATCH 2/8] Add testing for the __bool__ method of a query --- tests/test_query.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_query.py b/tests/test_query.py index c59b3cb41..893549200 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -47,6 +47,9 @@ def test_query(): assert list(app.query(".frob")) == [] assert list(app.query("#frob")) == [] + assert app.query("App") + assert not app.query("NotAnApp") + assert list(app.query("App")) == [app] assert list(app.query("#main")) == [main_view] assert list(app.query("View#main")) == [main_view] From e68a07d22840880a2940f08027f8fe9c29615059 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 25 Oct 2022 10:31:11 +0100 Subject: [PATCH 3/8] Add tests for reversing a query --- tests/test_query.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_query.py b/tests/test_query.py index 893549200..70d45c564 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -57,6 +57,7 @@ def test_query(): assert list(app.query("#widget2")) == [widget2] assert list(app.query("Widget.float")) == [sidebar, tooltip, helpbar] + assert list(reversed(app.query("Widget.float"))) == [helpbar, tooltip, sidebar] assert list(app.query("Widget.float").results(Widget)) == [ sidebar, tooltip, From 0019f266173f08cfa31370ca933eb680cb5ff488 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 25 Oct 2022 10:50:02 +0100 Subject: [PATCH 4/8] Add tests for a query __getitem__ --- tests/test_query.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_query.py b/tests/test_query.py index 70d45c564..2059db9b0 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -65,6 +65,9 @@ def test_query(): ] assert list(app.query("Widget.float").results(View)) == [] + assert app.query("Widget.float")[0] == sidebar + assert app.query("Widget.float")[0:2] == [sidebar, tooltip] + assert list(app.query("Widget.float.transient")) == [tooltip] assert list(app.query("App > View")) == [main_view, help_view] From 661fabb62cb3cfb04d6d8edf82c1111a39fa8a0a Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 25 Oct 2022 11:26:09 +0100 Subject: [PATCH 5/8] Add tests for a query filtering --- tests/test_query.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_query.py b/tests/test_query.py index 2059db9b0..869b67cf9 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -57,6 +57,7 @@ def test_query(): assert list(app.query("#widget2")) == [widget2] assert list(app.query("Widget.float")) == [sidebar, tooltip, helpbar] + assert list(app.query(Widget).filter(".float")) == [sidebar, tooltip, helpbar] assert list(reversed(app.query("Widget.float"))) == [helpbar, tooltip, sidebar] assert list(app.query("Widget.float").results(Widget)) == [ sidebar, From 6fcd93bd2a846af903afa09bb3dad2238fe72284 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 25 Oct 2022 11:34:58 +0100 Subject: [PATCH 6/8] Add tests for a query exclusion --- tests/test_query.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_query.py b/tests/test_query.py index 869b67cf9..ae24839da 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -58,6 +58,16 @@ def test_query(): assert list(app.query("Widget.float")) == [sidebar, tooltip, helpbar] assert list(app.query(Widget).filter(".float")) == [sidebar, tooltip, helpbar] + assert list( + app.query(Widget) + .exclude("App") + .exclude("#sub") + .exclude("#markdown") + .exclude("#main") + .exclude("#help") + .exclude("#widget1") + .exclude("#widget2") + ) == [sidebar, tooltip, helpbar] assert list(reversed(app.query("Widget.float"))) == [helpbar, tooltip, sidebar] assert list(app.query("Widget.float").results(Widget)) == [ sidebar, From f217310b60f65fc1146ba4589b3e787c582e744c Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 25 Oct 2022 11:38:34 +0100 Subject: [PATCH 7/8] Add a test for a query result with no filter --- tests/test_query.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_query.py b/tests/test_query.py index ae24839da..a7ac85fe8 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -74,6 +74,11 @@ def test_query(): tooltip, helpbar, ] + assert list(app.query("Widget.float").results()) == [ + sidebar, + tooltip, + helpbar, + ] assert list(app.query("Widget.float").results(View)) == [] assert app.query("Widget.float")[0] == sidebar From 22d53c827edf2bf866c4835ce9d9e8a2c1259f43 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 25 Oct 2022 14:03:43 +0100 Subject: [PATCH 8/8] Add tests for doing class work via a query --- tests/test_query.py | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/test_query.py b/tests/test_query.py index a7ac85fe8..b488391be 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -117,3 +117,51 @@ def test_query(): _ = app.query(".float").first(View) with pytest.raises(WrongType): _ = app.query(".float").last(View) + + +def test_query_classes(): + + class App(Widget): + pass + + class ClassTest(Widget): + pass + + CHILD_COUNT=100 + + # Create a fake app to hold everything else. + app = App() + + # Now spin up a bunch of children. + for n in range(CHILD_COUNT): + app._add_child(ClassTest(id=f"child{n}")) + + # Let's just be 100% sure everything was created fine. + assert len(app.query(ClassTest))==CHILD_COUNT + + # Now, let's check there are *no* children with the test class. + assert len(app.query(".test"))==0 + + # Add the test class to everything and then check again. + app.query(ClassTest).add_class("test") + assert len(app.query(".test"))==CHILD_COUNT + + # Remove the test class from everything then try again. + app.query(ClassTest).remove_class("test") + assert len(app.query(".test"))==0 + + # Add the test class to everything using set_class. + app.query(ClassTest).set_class(True, "test") + assert len(app.query(".test"))==CHILD_COUNT + + # Remove the test class from everything using set_class. + app.query(ClassTest).set_class(False, "test") + assert len(app.query(".test"))==0 + + # Add the test class to everything using toggle_class. + app.query(ClassTest).toggle_class("test") + assert len(app.query(".test"))==CHILD_COUNT + + # Remove the test class from everything using toggle_class. + app.query(ClassTest).toggle_class("test") + assert len(app.query(".test"))==0