def wrap(func): project = test_plugin_manager.project_name HookImplementationMarker(project)( tryfirst=tryfirst, trylast=trylast, hookwrapper=hookwrapper, specname=specname, )(func) _specname = specname or func.__name__ hook_caller = getattr(test_plugin_manager.hook, _specname, None) assert hook_caller, f"No hook with with name: {_specname}" opts = getattr(func, HookImplementation.format_tag(project)) hook_caller._add_hookimpl(HookImplementation(func, **opts)) return func
def wrap(func): example_implementation(tryfirst=tryfirst, trylast=trylast, hookwrapper=hookwrapper)(func) hook_caller._add_hookimpl( HookImplementation(func, **func.example_impl)) return func
def wrap(func, specname=None, *, tryfirst=True, trylast=None): project = test_plugin_manager.project_name marker = HookImplementationMarker(project) marker(tryfirst=tryfirst, trylast=trylast, specname=specname)(func) _specname = specname or func.__name__ hook_caller = getattr(test_plugin_manager.hook, _specname, None) assert hook_caller, f"No hook with with name: {_specname}" opts = getattr(func, HookImplementation.format_tag(project)) impl = HookImplementation(func, **opts) hook_caller._add_hookimpl(impl) try: yield hook_caller finally: if impl in hook_caller._nonwrappers: hook_caller._nonwrappers.remove(impl) if impl in hook_caller._wrappers: hook_caller._wrappers.remove(impl) assert impl not in hook_caller.get_hookimpls()
def multicall(methods, kwargs, firstresult=False): """utility function to execute the hook implementations loop""" caller = _multicall hookfuncs = [] for method in methods: f = HookImplementation(method, **method.example_impl) hookfuncs.append(f) # our _multicall function returns our own HookResult object. # so to make these pluggy tests pass, we have to access .result to mimic # the old behavior (that directly returns results). return caller(hookfuncs, kwargs, firstresult=firstresult).result
from napari_plugin_engine import HookImplementation from napari._tests.utils import layer_test_data from napari.components.viewer_model import ViewerModel from napari.layers._source import Source img = np.random.rand(10, 10) layer_data = [(lay[1], {}, lay[0].__name__.lower()) for lay in layer_test_data] def _impl(path): """just a dummy Hookimpl object to return from mocks""" pass _testimpl = HookImplementation(_impl, plugin_name='testimpl') @pytest.mark.parametrize("layer_datum", layer_data) def test_add_layers_with_plugins(layer_datum): """Test that add_layers_with_plugins adds the expected layer types.""" with patch( "napari.plugins.io.read_data_with_plugins", MagicMock(return_value=([layer_datum], _testimpl)), ): v = ViewerModel() v._add_layers_with_plugins('mock_path') layertypes = [layer._type_string for layer in v.layers] assert layertypes == [layer_datum[2]] expected_source = Source(path='mock_path', reader_plugin='testimpl')
def test_legacy_specimpl_opt(): impl = HookImplementation(lambda x: x) assert impl.opts spec = HookSpecification(type("Hook", (), {'x': lambda x: x}), 'x') assert spec.opts