Example #1
0
        public void Should_return_plugin_instances_that_apply_to_context()
        {
            var plugin1a = new Plugin1a();
            var plugin1b = new Plugin1b();
            var plugin2  = new Plugin2();
            var plugin3  = new Plugin3();
            var plugin4  = new Plugin4();

            var instances = new IPluginType[] { plugin1a, plugin1b, plugin2, plugin3, plugin4 };

            var plugins = new ConditionalPlugins <IPluginType, Context>(false)
                          .Configure(x => x
                                     .Append <Plugin1a>(c => c.Value == 1)
                                     .Append <Plugin1b>(c => c.Value == 1)
                                     .Append <Plugin2>(c => c.Value == 2)
                                     .Append <Plugin3>());

            var apply = plugins.ThatApplyTo(instances, new Context {
                Value = 1
            }).ToList();

            apply.Count.ShouldEqual(3);
            apply[0].Instance.ShouldEqual(plugin1a);
            apply[1].Instance.ShouldEqual(plugin1b);
            apply[2].Instance.ShouldEqual(plugin3);

            apply = plugins.ThatApplyTo(instances, new Context {
                Value = 2
            }).ToList();

            apply.Count.ShouldEqual(2);
            apply[0].Instance.ShouldEqual(plugin2);
            apply[1].Instance.ShouldEqual(plugin3);
        }
Example #2
0
        public void Should_prepend_instance_plugin_if_it_cannot_prepend_before_another(
            [Values(true, false)] bool @default,
            [Values(true, false)] bool dispose,
            [Values(true, false)] bool when)
        {
            var instance2 = new Plugin2();

            _pluginsDsl.Append <Plugin1>();

            RunInScope(when, (dsl, p) => dsl
                       .Prepend(instance2, p, dispose, @default)
                       .BeforeOrPrepend <Plugin3>());

            _plugins.Count().ShouldEqual(2);

            var prepended = _plugins.First();

            prepended.Type.ShouldEqual(typeof(Plugin2));
            prepended.Instance.ShouldEqual(instance2);
            prepended.Dispose.ShouldEqual(dispose);
            prepended.AppliesTo.ShouldEqual(_predicate);

            _plugins.Second().Type.ShouldEqual(typeof(Plugin1));

            _plugins.IsDefault(ConditionalPlugin <IPluginType, Context>
                               .Create(instance2, x => false))
            .ShouldEqual(@default);
        }
Example #3
0
        public void Should_get_the_default_instance()
        {
            var plugin1 = new Plugin1();
            var plugin2 = new Plugin2();
            var plugin3 = new Plugin3();
            var plugin4 = new Plugin4();

            var instances = new IPluginType[] { plugin1, plugin2, plugin3, plugin4 };

            var plugins = new ConditionalPlugins <IPluginType, Context>(false)
                          .Configure(x => x
                                     .Append <Plugin1>()
                                     .Append <Plugin2>(@default: true)
                                     .Append <Plugin3>(@default: true)
                                     .Append <Plugin4>());

            plugins.GetDefaultInstance(instances).ShouldEqual(plugin3);
        }
Example #4
0
        public void Contains_ValueNotPartOfTheCollection_ReturnsFalse()
        {
            // Arrange
            var container = ContainerFactory.New();

            container.Collection.Append <IPlugin, Plugin0>(Lifestyle.Singleton);
            container.Collection.Append <IPlugin, Plugin1>(Lifestyle.Singleton);
            container.Collection.Append <IPlugin, Plugin2>(Lifestyle.Singleton);

            var plugins = container.GetAllInstances <IPlugin>() as ICollection <IPlugin>;

            var differentInstance = new Plugin2();

            // Act
            var result = plugins.Contains(differentInstance);

            // Assert
            Assert.IsFalse(result);
        }
Example #5
0
        public void Should_set_type()
        {
            var instance = new Plugin2();
            var plugin   = Plugin <IPluginType> .Create(instance, true);

            plugin.Singleton.ShouldBeFalse();
            plugin.HasInstance.ShouldBeTrue();
            plugin.Type.ShouldEqual(typeof(Plugin2));
            plugin.Instance.ShouldEqual(instance);
            plugin.Dispose.ShouldBeTrue();

            plugin.Set <Plugin1>(true);

            plugin.Singleton.ShouldBeTrue();
            plugin.HasInstance.ShouldBeFalse();
            plugin.Type.ShouldEqual(typeof(Plugin1));
            plugin.Instance.ShouldBeNull();
            plugin.Dispose.ShouldBeFalse();
        }
Example #6
0
        public void Should_prepend_instance_plugin_if_it_cannot_prepend_before_another(
            [Values(true, false)] bool @default,
            [Values(true, false)] bool dispose)
        {
            var instance2 = new Plugin2();

            _pluginsDsl.Append <Plugin1>();
            _pluginsDsl.Prepend(instance2, dispose, @default).BeforeOrPrepend <Plugin3>();

            _plugins.Count().ShouldEqual(2);

            var prepended = _plugins.First();

            prepended.Type.ShouldEqual(typeof(Plugin2));
            prepended.Instance.ShouldEqual(instance2);
            prepended.Dispose.ShouldEqual(dispose);

            _plugins.Second().Type.ShouldEqual(typeof(Plugin1));

            _plugins.IsDefault(Plugin <IPluginType> .Create(instance2))
            .ShouldEqual(@default);
        }
Example #7
0
        public void Should_append_instance_plugin(
            [Values(true, false)] bool @default,
            [Values(true, false)] bool dispose)
        {
            var instance2 = new Plugin2();

            _pluginsDsl.Append <Plugin1>();
            _pluginsDsl.Append(instance2, dispose, @default);

            _plugins.Count().ShouldEqual(2);

            _plugins.First().Type.ShouldEqual(typeof(Plugin1));

            var appended = _plugins.Second();

            appended.Type.ShouldEqual(typeof(Plugin2));
            appended.Instance.ShouldEqual(instance2);
            appended.Dispose.ShouldEqual(dispose);

            _plugins.IsDefault(Plugin <IPluginType> .Create(instance2))
            .ShouldEqual(@default);
        }