Exemple #1
0
    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)
Exemple #2
0
    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_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!

            """)
        )
Exemple #4
0
    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)
Exemple #5
0
    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.")
Exemple #6
0
    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)
Exemple #7
0
    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)