def test_location_registry_empty(self): registry = TargetRegistry() with self.assertRaises(LocationNotSupported) as exception_context: registry.get_solver(None, None) self.assertEqual(exception_context.exception.supported, []) self.assertEqual( str(exception_context.exception), "Location None is not supported for None. Supported these: []", )
def test_register_location(self): def solver(wrapper, location): return 1 registry = TargetRegistry() registry.register_location(target_class=float, locator_class=str, solver=solver) self.assertIs(registry.get_solver(float, str), solver)
def test_registry_empty(self): registry = TargetRegistry() with self.assertRaises(InteractionNotSupported) as exception_context: registry.get_handler(None, None) self.assertEqual( str(exception_context.exception), "No handler is found for target None with interaction None. " "Supported these: []", )
def test_action_not_supported_report_supported_action(self): # Test raise InteractionNotSupported contains information about what # actions are supported. class SpecificEditor: pass class SpecificEditor2: pass class UserAction: pass class UserAction2: pass class UserAction3: pass def handler(wrapper, interaction): pass registry = TargetRegistry() registry.register_interaction(SpecificEditor, UserAction, handler) registry.register_interaction(SpecificEditor2, UserAction2, handler) registry.register_interaction(SpecificEditor2, UserAction3, handler) with self.assertRaises(InteractionNotSupported) as exception_context: registry.get_handler(SpecificEditor2, None) self.assertIn(UserAction2, exception_context.exception.supported) self.assertIn(UserAction3, exception_context.exception.supported) self.assertNotIn(UserAction, exception_context.exception.supported)
def test_registry_empty(self): class SpecificTarget: pass class Action: pass registry = TargetRegistry() with self.assertRaises(InteractionNotSupported) as exception_context: registry._get_handler(SpecificTarget(), Action()) self.assertEqual( str(exception_context.exception), f"No handler is found for target {SpecificTarget!r} with " f"interaction {Action!r}. Supported these: []", )
def get_default_registry(): """ Creates a default registry for UITester that is toolkit specific. Returns ------- registry : TargetRegistry The default registry containing implementations for TraitsUI editors that is toolkit specific. """ # side-effect to determine current toolkit from pyface.toolkit import toolkit_object # noqa if ETSConfig.toolkit == "null": registry = TargetRegistry() else: toolkit = {'wx': 'wx', 'qt4': 'qt4', 'qt': 'qt4'}[ETSConfig.toolkit] this_package, _ = __name__.rsplit(".", 1) module = importlib.import_module(".default_registry", this_package + '.' + toolkit) registry = module.get_default_registry() register_traitsui_ui_solvers( registry=registry, target_class=UI, traitsui_ui_getter=lambda target: target, ) return registry
def test_get_location_help_default(self): class Locator: """ Some default documentation.""" pass registry = TargetRegistry() registry.register_location( target_class=float, locator_class=Locator, solver=lambda w, l: 1, ) help_text = registry.get_location_doc( target_class=float, locator_class=Locator, ) self.assertEqual(help_text, "Some default documentation.")
def test_location_registry_empty(self): class SpecificTarget: pass class Locator: pass registry = TargetRegistry() with self.assertRaises(LocationNotSupported) as exception_context: registry._get_solver(SpecificTarget(), Locator()) self.assertEqual(exception_context.exception.supported, []) self.assertEqual( str(exception_context.exception), f"Location {Locator!r} is not supported for {SpecificTarget!r}. " "Supported these: []", )
def get_default_registries(): """Creates the default registries for UITester that are qt specific. Returns ------- registries : list of AbstractTargetRegistry The default registries containing implementations for TraitsUI editors that are qt specific. """ registry = TargetRegistry() # BooleanEditor boolean_editor.register(registry) # ButtonEditor button_editor.register(registry) # CheckListEditor check_list_editor.register(registry) # DirectoryEditor directory_editor.register(registry) # EnumEditor enum_editor.register(registry) # FileEditor file_editor.register(registry) # FontEditor font_editor.register(registry) # TextEditor text_editor.register(registry) # ListEditor list_editor.register(registry) # RangeEditor range_editor.register(registry) # ui_base ui_base.register(registry) # InstanceEditor instance_editor.register(registry) # Editor Factory editor_factory.register(registry) # TableEditor table_editor.register(registry) # The more general registry goes after the more specific registry. return [ registry, get_widget_registry(), ]
def test_get_interactions_supported(self): registry = TargetRegistry() class SpecificEditor: pass class UserAction: pass class UserAction2: pass def handler(wrapper, interaction): pass # when registry.register_interaction( target_class=SpecificEditor, interaction_class=UserAction, handler=handler, ) registry.register_interaction( target_class=SpecificEditor, interaction_class=UserAction2, handler=handler, ) # then self.assertEqual( registry._get_interactions(SpecificEditor()), {UserAction, UserAction2}, )
def test_get_locations_supported(self): # Test _get_locations return the supported location types. registry = TargetRegistry() class SpecificEditor: pass class Locator1: pass class Locator2: pass def solver(wrapper, location): return 1 # when registry.register_location( target_class=SpecificEditor, locator_class=Locator1, solver=solver, ) registry.register_location( target_class=SpecificEditor, locator_class=Locator2, solver=solver, ) # then self.assertEqual(registry._get_locations(SpecificEditor()), {Locator1, Locator2})
def test_help_message(self): class Action: """ Say hello. Say bye. """ pass class Locator: """ Return anything you like. Good day! """ pass registry1 = TargetRegistry() registry1.register_interaction( target_class=str, interaction_class=Action, handler=mock.Mock(), ) registry2 = TargetRegistry() registry2.register_location( target_class=str, locator_class=Locator, solver=mock.Mock(), ) wrapper = example_ui_wrapper( target="dummy", registries=[registry1, registry2] ) # when stream = io.StringIO() with mock.patch("sys.stdout", stream): wrapper.help() # then self.assertEqual( stream.getvalue(), textwrap.dedent(f"""\ Interactions ------------ {Action!r} Say hello. Say bye. Locations --------- {Locator!r} Return anything you like. Good day! """) )
def test_get_default_interaction_doc(self): class Action: """Some action.""" pass def handler(wrapper, interaction): pass registry = TargetRegistry() registry.register_interaction( target_class=float, interaction_class=Action, handler=handler, ) actual = registry.get_interaction_doc( target_class=float, interaction_class=Action, ) self.assertEqual(actual, "Some action.")
def test_register_editor_with_action(self): registry = TargetRegistry() class SpecificEditor: pass class UserAction: pass def handler(wrapper, interaction): pass # when registry.register_interaction( target_class=SpecificEditor, interaction_class=UserAction, handler=handler, ) # then actual = registry.get_handler(SpecificEditor, UserAction) self.assertIs(actual, handler)
def test_traitsui_registry_added(self): # Even if we have a custom registry list, the builtin TraitsUI # registry is always added. custom_registry = TargetRegistry() tester = UITester(registries=[custom_registry]) view = View(Item("submit_button")) with tester.create_ui(Order(), dict(view=view)) as ui: # this relies on TraitsUI builtin registry. wrapper = tester.find_by_name(ui, "submit_button") # custom registry is accessible # sanity check with self.assertRaises(InteractionNotSupported): wrapper.perform(1) custom_registry.register_interaction( target_class=wrapper._target.__class__, interaction_class=int, handler=lambda wrapper, interaction: None, ) wrapper.perform(1)
def get_default_registry(): """ Creates a default registry for UITester that is wx specific. Returns ------- registry : TargetRegistry The default registry containing implementations for TraitsUI editors that is wx specific. """ registry = TargetRegistry() # BooleanEditor boolean_editor.register(registry) # ButtonEditor button_editor.register(registry) # CheckListEditor check_list_editor.register(registry) # EnumEditor enum_editor.register(registry) # FontEditor font_editor.register(registry) # TextEditor text_editor.register(registry) # ListEditor list_editor.register(registry) # RangeEditor range_editor.register(registry) # ui_base ui_base.register(registry) # InstanceEditor instance_editor.register(registry) # Editor Factory editor_factory.register(registry) return registry
def test_register_location_report_existing(self): def solver(wrapper, location): return 1 registry = TargetRegistry() registry.register_location(target_class=float, locator_class=str, solver=solver) with self.assertRaises(LocationNotSupported) as exception_context: registry.get_solver(float, None) self.assertEqual(exception_context.exception.supported, [str])
def test_help_message_nothing_is_supported(self): registry = TargetRegistry() wrapper = example_ui_wrapper(registries=[registry]) # when stream = io.StringIO() with mock.patch("sys.stdout", stream): wrapper.help() # then self.assertEqual( stream.getvalue(), textwrap.dedent(f"""\ Interactions ------------ No interactions are supported. Locations --------- No locations are supported. """))
def test_error_conflict(self): # Test the same target + interaction type cannot be registered twice. class SpecificEditor: pass class UserAction: pass def handler(wrapper, interaction): pass registry = TargetRegistry() registry.register_interaction(SpecificEditor, UserAction, handler) with self.assertRaises(ValueError): registry.register_interaction(SpecificEditor, UserAction, handler)
def test_error_get_interaction_doc(self): # The registry is empty registry = TargetRegistry() with self.assertRaises(InteractionNotSupported): registry._get_interaction_doc(2.1, int)
def test_error_get_interaction_doc(self): # The registry is empty registry = TargetRegistry() with self.assertRaises(LocationNotSupported): registry.get_location_doc(float, int)