TST: add test of plugins enable_context
@@ -42,3 +42,31 @@ def test_plugin_registry_extra_options():
plugins.enable('metadata_plugin', p=3)
assert plugins.get()(3) == 27
+
+
+def test_plugin_registry_context():
+ plugins = GeneralCallableRegistry()
+
+ plugins.register('default', lambda x, p=2: x ** p)
+
+ # At first there is no plugin enabled
+ assert plugins.active == ''
+ assert plugins.options == {}
+
+ # Make sure the context is set and reset correctly
+ with plugins.enable_context('default', p=6):
+ assert plugins.active == 'default'
+ assert plugins.options == {'p': 6}
+
+ assert plugins.active == ''
+ assert plugins.options == {}
+
+ # Make sure the context is reset even if there is an error
+ try:
+ with plugins.enable_context('default', p=6):
+ raise ValueError()
+ except ValueError:
+ pass
+
+ assert plugins.active == ''
+ assert plugins.options == {}
