1+ from string import ascii_lowercase
2+
13import pytest
24
3- from textual .binding import Bindings , Binding
5+ from textual .binding import Bindings , Binding , BindingError , NoBinding
46
57BINDING1 = Binding ("a,b" , action = "action1" , description = "description1" )
68BINDING2 = Binding ("c" , action = "action2" , description = "description2" )
@@ -14,18 +16,35 @@ def bindings():
1416def test_bindings_get_key (bindings ):
1517 assert bindings .get_key ("b" ) == Binding ("b" , action = "action1" , description = "description1" )
1618 assert bindings .get_key ("c" ) == BINDING2
17-
19+ with pytest .raises (NoBinding ):
20+ bindings .get_key ("control+meta+alt+shift+super+hyper+t" )
1821
1922def test_bindings_merge_simple (bindings ):
2023 left = Bindings ([BINDING1 ])
2124 right = Bindings ([BINDING2 ])
2225 assert Bindings .merge ([left , right ]).keys == bindings .keys
2326
24-
2527def test_bindings_merge_overlap ():
2628 left = Bindings ([BINDING1 ])
2729 another_binding = Binding ("a" , action = "another_action" , description = "another_description" )
2830 assert Bindings .merge ([left , Bindings ([another_binding ])]).keys == {
2931 "a" : another_binding ,
3032 "b" : Binding ("b" , action = "action1" , description = "description1" ),
3133 }
34+
35+ def test_bad_binding_tuple ():
36+ with pytest .raises (BindingError ):
37+ _ = Bindings ((("a" , "action" ),))
38+ with pytest .raises (BindingError ):
39+ _ = Bindings ((("a" , "action" , "description" ,"too much" ),))
40+
41+ def test_binding_from_tuples ():
42+ assert Bindings ((( BINDING2 .key , BINDING2 .action , BINDING2 .description ),)).get_key ("c" ) == BINDING2
43+
44+ def test_shown ():
45+ bindings = Bindings ([
46+ Binding (
47+ key , action = f"action_{ key } " , description = f"Emits { key } " ,show = bool (ord (key )% 2 )
48+ ) for key in ascii_lowercase
49+ ])
50+ assert len (bindings .shown_keys )== (len (ascii_lowercase )/ 2 )
0 commit comments