public void TestContainsInstance()
        {
            SimpleIoc.Default.Reset();
            const string key1 = "My key";
            var instance = new TestClass1();
            SimpleIoc.Default.Register(() => instance, key1);
            SimpleIoc.Default.Register<TestClass2>();

            Assert.False(SimpleIoc.Default.ContainsCreated<TestClass1>());
            Assert.False(SimpleIoc.Default.ContainsCreated<TestClass1>(key1));
            Assert.False(SimpleIoc.Default.ContainsCreated<TestClass2>());
            Assert.False(SimpleIoc.Default.ContainsCreated<TestClass3>());

            SimpleIoc.Default.GetInstance<TestClass1>(key1);

            Assert.True(SimpleIoc.Default.ContainsCreated<TestClass1>());
            Assert.True(SimpleIoc.Default.ContainsCreated<TestClass1>(key1));
            Assert.False(SimpleIoc.Default.ContainsCreated<TestClass2>());
            Assert.False(SimpleIoc.Default.ContainsCreated<TestClass3>());

            SimpleIoc.Default.GetInstance<TestClass2>();

            Assert.True(SimpleIoc.Default.ContainsCreated<TestClass1>());
            Assert.True(SimpleIoc.Default.ContainsCreated<TestClass1>(key1));
            Assert.True(SimpleIoc.Default.ContainsCreated<TestClass2>());
            Assert.False(SimpleIoc.Default.ContainsCreated<TestClass3>());
        }
        public void TestUnregisterFactoryInstance()
        {
            SimpleIoc.Default.Reset();

            var instance0 = new TestClass1();

            SimpleIoc.Default.Register(() => instance0);

            Assert.True(SimpleIoc.Default.IsRegistered<TestClass1>());
            Assert.False(SimpleIoc.Default.ContainsCreated<TestClass1>());

            var instance1 = SimpleIoc.Default.GetInstance<TestClass1>();
            Assert.Same(instance0, instance1);

            Assert.True(SimpleIoc.Default.IsRegistered<TestClass1>());
            Assert.True(SimpleIoc.Default.ContainsCreated<TestClass1>());

            var instance2 = SimpleIoc.Default.GetInstance<TestClass1>();
            Assert.Same(instance0, instance2);

            SimpleIoc.Default.Unregister(instance0);

            Assert.True(SimpleIoc.Default.IsRegistered<TestClass1>());
            Assert.False(SimpleIoc.Default.ContainsCreated<TestClass1>());

            var instance3 = SimpleIoc.Default.GetInstance<TestClass1>();
            Assert.Same(instance0, instance3);
        }
        public void TestIsClassRegisteredWithFactory()
        {
            SimpleIoc.Default.Reset();

            var instance = new TestClass1();
            Assert.False(SimpleIoc.Default.IsRegistered<TestClass1>());
            SimpleIoc.Default.Register(() => instance);
            Assert.True(SimpleIoc.Default.IsRegistered<TestClass1>());

            SimpleIoc.Default.GetInstance<TestClass1>();
            Assert.True(SimpleIoc.Default.IsRegistered<TestClass1>());

            SimpleIoc.Default.Unregister<TestClass1>();
            Assert.False(SimpleIoc.Default.IsRegistered<TestClass1>());
        }
        public void TestConstructWithProperty()
        {
            var property = new TestClass1();

            SimpleIoc.Default.Reset();
            SimpleIoc.Default.Register(() => new TestClass6 { MyProperty = property });

            var instance1 = new TestClass6();
            Assert.NotNull(instance1);
            Assert.Null(instance1.MyProperty);

            var instance2 = SimpleIoc.Default.GetInstance<TestClass6>();
            Assert.NotNull(instance2);
            Assert.NotNull(instance2.MyProperty);
            Assert.Same(property, instance2.MyProperty);
        }
        public void TestAddingFactoryAndKeyForClassRegisteredWithDefault()
        {
            SimpleIoc.Default.Reset();

            SimpleIoc.Default.Register<TestClass1>();

            const string key1 = "key1";
            var instance1 = new TestClass1();
            SimpleIoc.Default.Register(() => instance1, key1);

            var defaultInstance = SimpleIoc.Default.GetInstance<TestClass1>();
            var foundInstance1 = SimpleIoc.Default.GetInstance<TestClass1>(key1);

            Assert.Same(instance1, foundInstance1);
            Assert.NotSame(defaultInstance, foundInstance1);
        }
        public void TestBuildInstanceWithMultipleConstructorsNotMarkedWithAttribute()
        {
            var property = new TestClass1();

            SimpleIoc.Default.Reset();
            SimpleIoc.Default.Register(() => new TestClass6(property));

            var instance1 = new TestClass6();
            Assert.NotNull(instance1);
            Assert.Null(instance1.MyProperty);

            var instance2 = SimpleIoc.Default.GetInstance<TestClass6>();
            Assert.NotNull(instance2);
            Assert.NotNull(instance2.MyProperty);
            Assert.Same(property, instance2.MyProperty);
        }
        public void TestBuildWithMultipleConstructorsNotMarkedWithAttribute()
        {
            var property = new TestClass1();

            SimpleIoc.Default.Reset();
            SimpleIoc.Default.Register<ITestClass1>(() => property);

            try
            {
                SimpleIoc.Default.Register<TestClass6>();
                Assert.True(false, "ActivationException was expected");
            }
            catch (ActivationException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
        public void TestBuildWithMultipleConstructors()
        {
            var property = new TestClass1();

            SimpleIoc.Default.Reset();
            SimpleIoc.Default.Register<ITestClass1>(() => property);
            SimpleIoc.Default.Register<TestClass5>();

            var instance1 = new TestClass5();
            Assert.NotNull(instance1);
            Assert.Null(instance1.MyProperty);

            var instance2 = SimpleIoc.Default.GetInstance<TestClass5>();
            Assert.NotNull(instance2);
            Assert.NotNull(instance2.MyProperty);
            Assert.Same(property, instance2.MyProperty);
        }
        public void TestAddingFactoryAndKeyForClassRegisteredWithFactoryAndDifferentKey()
        {
            SimpleIoc.Default.Reset();

            const string key1 = "key1";
            var instance1 = new TestClass1();
            SimpleIoc.Default.Register(() => instance1, key1);

            const string key2 = "key2";
            var instance2 = new TestClass1();
            SimpleIoc.Default.Register(() => instance2, key2);

            var foundInstance1 = SimpleIoc.Default.GetInstance<TestClass1>(key1);
            var foundInstance2 = SimpleIoc.Default.GetInstance<TestClass1>(key2);

            Assert.Same(instance1, foundInstance1);
            Assert.Same(instance2, foundInstance2);
            Assert.NotSame(foundInstance1, foundInstance2);
        }
        public void TestIsClassRegisteredWithFactoryAndKey()
        {
            SimpleIoc.Default.Reset();
            const string key1 = "My key 1";
            const string key2 = "My key 2";

            var instance = new TestClass1();
            Assert.False(SimpleIoc.Default.IsRegistered<TestClass1>(key1));
            Assert.False(SimpleIoc.Default.IsRegistered<TestClass1>(key2));

            SimpleIoc.Default.Register(() => instance, key1);
            Assert.True(SimpleIoc.Default.IsRegistered<TestClass1>(key1));
            Assert.False(SimpleIoc.Default.IsRegistered<TestClass1>(key2));

            SimpleIoc.Default.GetInstance<TestClass1>(key1);
            Assert.True(SimpleIoc.Default.IsRegistered<TestClass1>(key1));

            SimpleIoc.Default.Unregister<TestClass1>(key1);
            Assert.False(SimpleIoc.Default.IsRegistered<TestClass1>(key1));
            Assert.False(SimpleIoc.Default.IsRegistered<TestClass1>(key2));
        }
        public void TestResolvedParameter()
        {
            // Arrange
            var instanceName = "namedInstance";
            var testObject1 = new TestClass1();
            SimpleIoc.Default.Register<ITestClass1>(() => testObject1, instanceName);
            SimpleIoc.Default.Register<ITestClass10, TestClass10>(new ResolvedParameter<ITestClass1>(instanceName));

            // Act
            var testObject10 = SimpleIoc.Default.GetInstance<ITestClass10>();

            // Assert
            testObject10.Should().NotBeNull();
            testObject10.TestClass1.Should().NotBeNull();
            testObject10.TestClass1.Should().BeOfType<TestClass1>();
            testObject10.TestClass1.As<TestClass1>().Should().Be(testObject1);
            SimpleIoc.Default.IsRegistered<ITestClass1>().Should().BeTrue();
            SimpleIoc.Default.IsRegistered<ITestClass10>().Should().BeTrue();
            SimpleIoc.Default.ContainsCreated<ITestClass1>(instanceName).Should().BeTrue();
            SimpleIoc.Default.ContainsCreated<ITestClass10>().Should().BeTrue();
        }
        public void TestAddingFactoryAndKeyForClassRegisteredWithFactoryAndSameKey()
        {
            SimpleIoc.Default.Reset();

            const string key1 = "key1";
            var instance1 = new TestClass1();
            SimpleIoc.Default.Register(() => instance1, key1);

            var instance2 = new TestClass1();

            try
            {
                SimpleIoc.Default.Register(() => instance2, key1);
                Assert.True(false, "InvalidOperationException was expected");
            }
            catch (InvalidOperationException)
            {
            }
        }
        public void TestGetInstanceWithGenericInterface()
        {
            SimpleIoc.Default.Reset();
            var instanceOriginal = new TestClass1();
            SimpleIoc.Default.Register<ITestClass1>(() => instanceOriginal);

            var instance = SimpleIoc.Default.GetInstance<ITestClass1>();

            Assert.NotNull(instance);
            Assert.IsType<TestClass1>(instance);
            Assert.Same(instanceOriginal, instance);
        }
        public void TestUnregisterInstance()
        {
            var instanceOriginal1 = new TestClass1();

            SimpleIoc.Default.Reset();
            SimpleIoc.Default.Register(() => instanceOriginal1);

            var instance1 = SimpleIoc.Default.GetInstance<TestClass1>();
            Assert.Same(instanceOriginal1, instance1);

            SimpleIoc.Default.Unregister(instanceOriginal1);

            try
            {
                var instance2 = SimpleIoc.Default.GetInstance<TestClass1>();
                Assert.Same(instance1, instance2);
            }
            catch (ActivationException)
            {
                Assert.True(false, "ActivationException was thrown");
            }
        }
        public void TestGetInstancesWithInstanceGeneric()
        {
            const string key1 = "MyKey1";
            const string key2 = "MyKey2";

            var instanceOriginal1 = new TestClass1();
            var instanceOriginal2 = new TestClass1();

            SimpleIoc.Default.Reset();
            SimpleIoc.Default.Register<ITestClass1>(() => instanceOriginal1, key1);
            SimpleIoc.Default.Register<ITestClass1>(() => instanceOriginal2, key2);

            var instance1 = SimpleIoc.Default.GetInstance<ITestClass1>(key1);
            var instance2 = SimpleIoc.Default.GetInstance<ITestClass1>(key1);
            var instance3 = SimpleIoc.Default.GetInstance<ITestClass1>(key2);

            Assert.NotNull(instance1);
            Assert.NotNull(instance2);
            Assert.NotNull(instance3);

            Assert.IsType<TestClass1>(instance1);
            Assert.IsType<TestClass1>(instance2);
            Assert.IsType<TestClass1>(instance3);

            Assert.Same(instance1, instance2);
            Assert.NotSame(instance1, instance3);
        }
        public void TestUnregisterInstanceWithKey()
        {
            var instanceOriginal1 = new TestClass1();
            var instanceOriginal2 = new TestClass1();
            const string key1 = "My key 1";
            const string key2 = "My key 2";

            SimpleIoc.Default.Reset();
            SimpleIoc.Default.Register(() => instanceOriginal1, key1);
            SimpleIoc.Default.Register(() => instanceOriginal2, key2);

            var instance1 = SimpleIoc.Default.GetInstance<TestClass1>(key1);
            Assert.Same(instanceOriginal1, instance1);
            var instance2 = SimpleIoc.Default.GetInstance<TestClass1>(key2);
            Assert.Same(instanceOriginal2, instance2);

            SimpleIoc.Default.Unregister<TestClass1>(key1);

            try
            {
                SimpleIoc.Default.GetInstance<TestClass1>(key1);
                Assert.True(false, "ActivationException was expected");
            }
            catch (ActivationException)
            {
            }
        }
        public void TestGetAllInstancesWithInstanceGeneric()
        {
            const string key1 = "MyKey1";
            const string key2 = "MyKey2";
            const string key3 = "MyKey3";
            const string key4 = "MyKey4";
            const string key5 = "MyKey5";
            const string key6 = "MyKey6";

            var instanceOriginal1 = new TestClass1();
            var instanceOriginal2 = new TestClass1();
            var instanceOriginal3 = new TestClass1();
            var instanceOriginal4 = new TestClass1();
            var instanceOriginal5 = new TestClass4();
            var instanceOriginal6 = new TestClass4();

            SimpleIoc.Default.Reset();
            SimpleIoc.Default.Register(() => instanceOriginal1, key1);
            SimpleIoc.Default.Register(() => instanceOriginal2, key2);
            SimpleIoc.Default.Register(() => instanceOriginal3, key3);
            SimpleIoc.Default.Register(() => instanceOriginal4, key4);
            SimpleIoc.Default.Register(() => instanceOriginal5, key5);
            SimpleIoc.Default.Register(() => instanceOriginal6, key6);

            var instance1 = SimpleIoc.Default.GetInstance<TestClass1>(key1);
            var instance2 = SimpleIoc.Default.GetInstance<TestClass1>(key2);
            var instance3 = SimpleIoc.Default.GetInstance<TestClass1>(key3);
            var instance4 = SimpleIoc.Default.GetInstance<TestClass1>(key4);
            var instance5 = SimpleIoc.Default.GetInstance<TestClass4>(key5);
            var instance6 = SimpleIoc.Default.GetInstance<TestClass4>(key6);

            Assert.NotNull(instance1);
            Assert.NotNull(instance2);
            Assert.NotNull(instance3);
            Assert.NotNull(instance4);
            Assert.NotNull(instance5);
            Assert.NotNull(instance6);

            var allInstances = SimpleIoc.Default.GetAllInstances<TestClass1>();
            Assert.Equal(4, allInstances.Count());

            foreach (var instance in allInstances)
            {
                Assert.NotNull(instance);

                if (instance.Equals(instance1))
                {
                    instance1 = null;
                }

                if (instance.Equals(instance2))
                {
                    instance2 = null;
                }

                if (instance.Equals(instance3))
                {
                    instance3 = null;
                }

                if (instance.Equals(instance4))
                {
                    instance4 = null;
                }

                if (instance.Equals(instance5))
                {
                    instance5 = null;
                }

                if (instance.Equals(instance6))
                {
                    instance6 = null;
                }
            }

            Assert.Null(instance1);
            Assert.Null(instance2);
            Assert.Null(instance3);
            Assert.Null(instance4);
            Assert.NotNull(instance5);
            Assert.NotNull(instance6);
        }
        public void TestGetAllInstancesOfClassWithCreation()
        {
            SimpleIoc.Default.Reset();
            TestClass1.Reset();

            var instance0 = new TestClass1();
            SimpleIoc.Default.Register(() => instance0, true);

            const string key1 = "key1";
            const string key2 = "key2";
            const string key3 = "key3";
            var instance1 = new TestClass1();
            var instance2 = new TestClass1();
            var instance3 = new TestClass1();
            SimpleIoc.Default.Register(() => instance1, key1, true);
            SimpleIoc.Default.Register(() => instance2, key2, true);
            SimpleIoc.Default.Register(() => instance3, key3);

            Assert.Equal(4, TestClass1.InstancesCount);

            var instances = SimpleIoc.Default.GetAllCreatedInstances<TestClass1>();
            instances.Should().HaveCount(3);

            instances = SimpleIoc.Default.GetAllCreatedInstances<TestClass1>();
            SimpleIoc.Default.GetInstance<TestClass1>(key3);

            instances.Should().HaveCount(4);

            var list = instances.ToList();

            foreach (var instance in instances)
            {
                if (instance == instance0
                    || instance == instance1
                    || instance == instance2
                    || instance == instance3)
                {
                    list.Remove(instance);
                }
                else
                {
                    Assert.True(false);
                }
            }

            Assert.Equal(0, list.Count);
        }
        public void TestGetAllInstancesGeneric2()
        {
            SimpleIoc.Default.Reset();

            var instance = new TestClass1();
            SimpleIoc.Default.Register(() => instance);

            var instances = SimpleIoc.Default.GetAllInstances<TestClass1>();
            instances.Should().HaveCount(1);

            SimpleIoc.Default.GetInstance<TestClass1>("key1");

            instances = SimpleIoc.Default.GetAllInstances<TestClass1>();
            instances.Should().HaveCount(2);
        }
        public void TestGetAllInstancesGeneric1()
        {
            SimpleIoc.Default.Reset();

            const string key1 = "key1";
            var instance1 = new TestClass1();
            SimpleIoc.Default.Register(() => instance1, key1);

            SimpleIoc.Default.Register<TestClass1>();

            var instances = SimpleIoc.Default.GetAllInstances<TestClass1>().ToList();
            Assert.Equal(2, instances.Count);

            var getInstance1 = SimpleIoc.Default.GetInstance<TestClass1>(key1);
            Assert.Same(instance1, getInstance1);

            Assert.True(instances.Contains(instance1));

            instances.Remove(instance1);
            Assert.Equal(1, instances.Count);

            var getInstance2 = SimpleIoc.Default.GetInstance<TestClass1>();
            Assert.Same(instances[0], getInstance2);

            SimpleIoc.Default.GetInstance<TestClass1>("key2");

            instances = SimpleIoc.Default.GetAllInstances<TestClass1>().ToList();
            Assert.Equal(3, instances.Count);
        }
        public void TestGettingDefaultInstanceAfterRegisteringFactoryAndKey()
        {
            SimpleIoc.Default.Reset();

            const string key1 = "key1";
            var instance = new TestClass1();
            SimpleIoc.Default.Register(() => instance, key1);

            try
            {
                SimpleIoc.Default.GetInstance<TestClass1>();
                Assert.True(false, "ActivationException was expected");
            }
            catch (ActivationException)
            {
            }
        }
        public void TestReset()
        {
            SimpleIoc.Default.Reset();
            var instanceOriginal = new TestClass1();
            SimpleIoc.Default.Register<ITestClass1>(() => instanceOriginal);
            var instance = SimpleIoc.Default.GetInstance<ITestClass1>();
            Assert.NotNull(instance);

            SimpleIoc.Default.Reset();

            try
            {
                SimpleIoc.Default.GetInstance<ITestClass1>();
                Assert.True(false, "ActivationException was expected");
            }
            catch (ActivationException)
            {
            }
        }