Debian Codegraph @

traitsui/testing/tester/tests/test_registry.py

raw symbols SCIP
1 # (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10
11 import unittest
12
13 from traitsui.testing.tester.target_registry import (
14 TargetRegistry,
15 )
16 from traitsui.testing.tester.exceptions import (
17 InteractionNotSupported,
18 LocationNotSupported,
19 )
20
21
22 class TestInteractionRegistry(unittest.TestCase):
23 def test_registry_empty(self):
24 class SpecificTarget:
25 pass
26
27 class Action:
28 pass
29
30 registry = TargetRegistry()
31 with self.assertRaises(InteractionNotSupported) as exception_context:
32 registry._get_handler(SpecificTarget(), Action())
33
34 self.assertEqual(
35 str(exception_context.exception),
36 f"No handler is found for target {SpecificTarget!r} with "
37 f"interaction {Action!r}. Supported these: []",
38 )
39
40 def test_register_editor_with_action(self):
41 registry = TargetRegistry()
42
43 class SpecificEditor:
44 pass
45
46 class UserAction:
47 pass
48
49 def handler(wrapper, interaction):
50 pass
51
52 # when
53 registry.register_interaction(
54 target_class=SpecificEditor,
55 interaction_class=UserAction,
56 handler=handler,
57 )
58
59 # then
60 actual = registry._get_handler(SpecificEditor(), UserAction())
61 self.assertIs(actual, handler)
62
63 def test_get_interactions_supported(self):
64 registry = TargetRegistry()
65
66 class SpecificEditor:
67 pass
68
69 class UserAction:
70 pass
71
72 class UserAction2:
73 pass
74
75 def handler(wrapper, interaction):
76 pass
77
78 # when
79 registry.register_interaction(
80 target_class=SpecificEditor,
81 interaction_class=UserAction,
82 handler=handler,
83 )
84 registry.register_interaction(
85 target_class=SpecificEditor,
86 interaction_class=UserAction2,
87 handler=handler,
88 )
89
90 # then
91 self.assertEqual(
92 registry._get_interactions(SpecificEditor()),
93 {UserAction, UserAction2},
94 )
95
96 def test_action_not_supported_report_supported_action(self):
97 # Test raise InteractionNotSupported contains information about what
98 # actions are supported.
99
100 class SpecificEditor:
101 pass
102
103 class SpecificEditor2:
104 pass
105
106 class UserAction:
107 pass
108
109 class UserAction2:
110 pass
111
112 class UserAction3:
113 pass
114
115 def handler(wrapper, interaction):
116 pass
117
118 registry = TargetRegistry()
119 registry.register_interaction(SpecificEditor, UserAction, handler)
120 registry.register_interaction(SpecificEditor2, UserAction2, handler)
121 registry.register_interaction(SpecificEditor2, UserAction3, handler)
122
123 with self.assertRaises(InteractionNotSupported) as exception_context:
124 registry._get_handler(SpecificEditor2(), None)
125
126 self.assertIn(UserAction2, exception_context.exception.supported)
127 self.assertIn(UserAction3, exception_context.exception.supported)
128 self.assertNotIn(UserAction, exception_context.exception.supported)
129
130 def test_error_conflict(self):
131 # Test the same target + interaction type cannot be registered twice.
132
133 class SpecificEditor:
134 pass
135
136 class UserAction:
137 pass
138
139 def handler(wrapper, interaction):
140 pass
141
142 registry = TargetRegistry()
143 registry.register_interaction(SpecificEditor, UserAction, handler)
144
145 with self.assertRaises(ValueError):
146 registry.register_interaction(SpecificEditor, UserAction, handler)
147
148 def test_error_get_interaction_doc(self):
149 # The registry is empty
150 registry = TargetRegistry()
151 with self.assertRaises(InteractionNotSupported):
152 registry._get_interaction_doc(2.1, int)
153
154 def test_get_default_interaction_doc(self):
155 class Action:
156 """Some action."""
157
158 pass
159
160 def handler(wrapper, interaction):
161 pass
162
163 registry = TargetRegistry()
164 registry.register_interaction(
165 target_class=float,
166 interaction_class=Action,
167 handler=handler,
168 )
169
170 actual = registry._get_interaction_doc(
171 target=21.2,
172 interaction_class=Action,
173 )
174 self.assertEqual(actual, "Some action.")
175
176
177 class TestLocationRegistry(unittest.TestCase):
178 def test_location_registry_empty(self):
179 class SpecificTarget:
180 pass
181
182 class Locator:
183 pass
184
185 registry = TargetRegistry()
186 with self.assertRaises(LocationNotSupported) as exception_context:
187 registry._get_solver(SpecificTarget(), Locator())
188
189 self.assertEqual(exception_context.exception.supported, [])
190 self.assertEqual(
191 str(exception_context.exception),
192 f"Location {Locator!r} is not supported for {SpecificTarget!r}. "
193 "Supported these: []",
194 )
195
196 def test_register_location(self):
197 def solver(wrapper, location):
198 return 1
199
200 registry = TargetRegistry()
201 registry.register_location(
202 target_class=float, locator_class=str, solver=solver
203 )
204
205 self.assertIs(registry._get_solver(2.1, "dummy"), solver)
206
207 def test_register_location_report_existing(self):
208 def solver(wrapper, location):
209 return 1
210
211 registry = TargetRegistry()
212 registry.register_location(
213 target_class=float, locator_class=str, solver=solver
214 )
215
216 with self.assertRaises(LocationNotSupported) as exception_context:
217 registry._get_solver(3.4, None)
218
219 self.assertEqual(exception_context.exception.supported, [str])
220
221 def test_get_locations_supported(self):
222 # Test _get_locations return the supported location types.
223 registry = TargetRegistry()
224
225 class SpecificEditor:
226 pass
227
228 class Locator1:
229 pass
230
231 class Locator2:
232 pass
233
234 def solver(wrapper, location):
235 return 1
236
237 # when
238 registry.register_location(
239 target_class=SpecificEditor,
240 locator_class=Locator1,
241 solver=solver,
242 )
243 registry.register_location(
244 target_class=SpecificEditor,
245 locator_class=Locator2,
246 solver=solver,
247 )
248
249 # then
250 self.assertEqual(
251 registry._get_locations(SpecificEditor()), {Locator1, Locator2}
252 )
253
254 def test_get_location_help_default(self):
255 class Locator:
256 """Some default documentation."""
257
258 pass
259
260 registry = TargetRegistry()
261 registry.register_location(
262 target_class=float,
263 locator_class=Locator,
264 solver=lambda w, l: 1,
265 )
266
267 help_text = registry._get_location_doc(
268 target=2.345,
269 locator_class=Locator,
270 )
271 self.assertEqual(help_text, "Some default documentation.")
272
273 def test_error_get_interaction_doc(self):
274 # The registry is empty
275 registry = TargetRegistry()
276 with self.assertRaises(LocationNotSupported):
277 registry._get_location_doc(3.456, int)