11import pytest
22
3+ from textual .app import App , ComposeResult
4+ from textual .containers import Container
35from textual .widget import Widget
46from textual .css .query import InvalidQueryFormat , WrongType , NoMatches , TooManyMatches
57
@@ -50,17 +52,16 @@ class App(Widget):
5052 assert list (app .query (".frob" )) == []
5153 assert list (app .query ("#frob" )) == []
5254
53- assert app .query ("App" )
5455 assert not app .query ("NotAnApp" )
5556
56- assert list (app .query ("App" )) == [app ]
57+ assert list (app .query ("App" )) == [] # Root is not included in queries
5758 assert list (app .query ("#main" )) == [main_view ]
5859 assert list (app .query ("View#main" )) == [main_view ]
5960 assert list (app .query ("View2#help" )) == [help_view ]
6061 assert list (app .query ("#widget1" )) == [widget1 ]
61- assert list (app .query ("#Widget1" )) == [] # Note case.
62+ assert list (app .query ("#Widget1" )) == [] # Note case.
6263 assert list (app .query ("#widget2" )) == [widget2 ]
63- assert list (app .query ("#Widget2" )) == [] # Note case.
64+ assert list (app .query ("#Widget2" )) == [] # Note case.
6465
6566 assert list (app .query ("Widget.float" )) == [sidebar , tooltip , helpbar ]
6667 assert list (app .query (Widget ).filter (".float" )) == [sidebar , tooltip , helpbar ]
@@ -110,10 +111,10 @@ class App(Widget):
110111 tooltip ,
111112 ]
112113
113- assert list (app .query ("App, View" )) == [app , main_view , sub_view , help_view ]
114+ assert list (app .query ("View" )) == [main_view , sub_view , help_view ]
114115 assert list (app .query ("#widget1, #widget2" )) == [widget1 , widget2 ]
115116 assert list (app .query ("#widget1 , #widget2" )) == [widget1 , widget2 ]
116- assert list (app .query ("#widget1, #widget2, App" )) == [app , widget1 , widget2 ]
117+ assert list (app .query ("#widget1, #widget2, App" )) == [widget1 , widget2 ]
117118
118119 assert app .query (".float" ).first () == sidebar
119120 assert app .query (".float" ).last () == helpbar
@@ -130,14 +131,13 @@ class App(Widget):
130131
131132
132133def test_query_classes ():
133-
134134 class App (Widget ):
135135 pass
136136
137137 class ClassTest (Widget ):
138138 pass
139139
140- CHILD_COUNT = 100
140+ CHILD_COUNT = 100
141141
142142 # Create a fake app to hold everything else.
143143 app = App ()
@@ -147,34 +147,35 @@ class ClassTest(Widget):
147147 app ._add_child (ClassTest (id = f"child{ n } " ))
148148
149149 # Let's just be 100% sure everything was created fine.
150- assert len (app .query (ClassTest ))== CHILD_COUNT
150+ assert len (app .query (ClassTest )) == CHILD_COUNT
151151
152152 # Now, let's check there are *no* children with the test class.
153- assert len (app .query (".test" ))== 0
153+ assert len (app .query (".test" )) == 0
154154
155155 # Add the test class to everything and then check again.
156156 app .query (ClassTest ).add_class ("test" )
157- assert len (app .query (".test" ))== CHILD_COUNT
157+ assert len (app .query (".test" )) == CHILD_COUNT
158158
159159 # Remove the test class from everything then try again.
160160 app .query (ClassTest ).remove_class ("test" )
161- assert len (app .query (".test" ))== 0
161+ assert len (app .query (".test" )) == 0
162162
163163 # Add the test class to everything using set_class.
164164 app .query (ClassTest ).set_class (True , "test" )
165- assert len (app .query (".test" ))== CHILD_COUNT
165+ assert len (app .query (".test" )) == CHILD_COUNT
166166
167167 # Remove the test class from everything using set_class.
168168 app .query (ClassTest ).set_class (False , "test" )
169- assert len (app .query (".test" ))== 0
169+ assert len (app .query (".test" )) == 0
170170
171171 # Add the test class to everything using toggle_class.
172172 app .query (ClassTest ).toggle_class ("test" )
173- assert len (app .query (".test" ))== CHILD_COUNT
173+ assert len (app .query (".test" )) == CHILD_COUNT
174174
175175 # Remove the test class from everything using toggle_class.
176176 app .query (ClassTest ).toggle_class ("test" )
177- assert len (app .query (".test" ))== 0
177+ assert len (app .query (".test" )) == 0
178+
178179
179180def test_invalid_query ():
180181 class App (Widget ):
@@ -187,3 +188,26 @@ class App(Widget):
187188
188189 with pytest .raises (InvalidQueryFormat ):
189190 app .query ("#foo" ).exclude ("#2" )
191+
192+
193+ async def test_universal_selector_doesnt_select_self ():
194+ class ExampleApp (App ):
195+ def compose (self ) -> ComposeResult :
196+ yield Container (
197+ Widget (
198+ Widget (),
199+ Widget (
200+ Widget (),
201+ ),
202+ ),
203+ Widget (),
204+ id = "root-container" ,
205+ )
206+
207+ app = ExampleApp ()
208+ async with app .run_test ():
209+ container = app .query_one ("#root-container" , Container )
210+ query = container .query ("*" )
211+ results = list (query .results ())
212+ assert len (results ) == 5
213+ assert not any (node .id == "root-container" for node in results )
0 commit comments