public void RegisterSingleConcrete_WithAllValidParameters_Succeeds()
        {
            // Arrange
            var container = new Container();

            var convention = new ParameterConvention();

            container.Options.RegisterParameterConvention(convention);

            // Act
            // ctor: ClassWithPrimiveConstructorParams(string someValue, DateTime now, string name)
            container.RegisterSingleton<ClassWithOnlyPrimitiveConstructorParams>(
                convention.WithParameter("someValue", "foo"),
                convention.WithParameter(() => DateTime.MinValue),
                convention.WithParameter("name", () => "bar"));

            container.GetInstance<ClassWithOnlyPrimitiveConstructorParams>();
        }
        public void RegisterSingleConcrete_WithAllValidParameters_Succeeds()
        {
            // Arrange
            var container = new Container();

            var convention = new ParameterConvention();

            container.Options.RegisterParameterConvention(convention);

            // Act
            // ctor: ClassWithPrimiveConstructorParams(string someValue, DateTime now, string name)
            container.RegisterSingleton <ClassWithOnlyPrimitiveConstructorParams>(
                convention.WithParameter("someValue", "foo"),
                convention.WithParameter(() => DateTime.MinValue),
                convention.WithParameter("name", () => "bar"));

            container.GetInstance <ClassWithOnlyPrimitiveConstructorParams>();
        }
        public void Register_TypeWithMixedParameters_Succeeds()
        {
            // Arrange
            var container = new Container();

            var convention = new ParameterConvention();

            container.Options.RegisterParameterConvention(convention);

            container.RegisterSingleton <IDependency>(new Dependency());

            // Act
            // ctor: ClassWithAPrimitiveConstructorParam(IDependency dependency, Decimal someDecimal)
            container.RegisterSingleton <ClassWithAPrimitiveConstructorParam>(
                convention.WithParameter("someDecimal", decimal.MinValue));
        }
        public void GetInstance_Type_TypeWithMixedParameters_InjectsExpectedValues()
        {
            // Arrange
            var     expectedDependency = new Dependency();
            decimal expectedDecimal    = decimal.MaxValue;

            var container  = new Container();
            var convention = new ParameterConvention();

            container.Options.RegisterParameterConvention(convention);
            container.RegisterSingleton <IDependency>(expectedDependency);

            // ctor: ClassWithAPrimitiveConstructorParam(IDependency dependency, Decimal someDecimal)
            container.RegisterSingleton <ClassWithAPrimitiveConstructorParam>(
                convention.WithParameter("someDecimal", expectedDecimal));

            // Act
            var instance = container.GetInstance <ClassWithAPrimitiveConstructorParam>();

            // Assert
            Assert.IsTrue(object.ReferenceEquals(expectedDependency, instance.Dependency));
            Assert.AreEqual(expectedDecimal, instance.SomeDecimal);
        }
        public void GetInstance_TypeWithAllValidParametersRegistered_InjectsExpectedValues()
        {
            // Arrange
            string someValue = "foo";
            DateTime now = new DateTime(2012, 03, 4);
            string name = "bar";

            var container = new Container();

            var convention = new ParameterConvention();

            container.Options.RegisterParameterConvention(convention);

            // ctor: ClassWithPrimiveConstructorParams(string someValue, DateTime now, string name)
            container.Register<ClassWithOnlyPrimitiveConstructorParams>(
                convention.WithParameter("someValue", someValue),
                convention.WithParameter(() => now),
                convention.WithParameter("name", () => name));

            // Act
            var instance = container.GetInstance<ClassWithOnlyPrimitiveConstructorParams>();

            // Assert
            Assert.AreEqual(now, instance.Now);
            Assert.AreEqual(someValue, instance.SomeValue);
            Assert.AreEqual(name, instance.Name);
        }
        public void Register_AmbiguousWithParameterRegistrations_FailsWithExpectedException()
        {
            // Arrange
            var container = new Container();

            var convention = new ParameterConvention();

            container.Options.RegisterParameterConvention(convention);

            try
            {
                // Act
                // ctor: ClassWithPrimiveConstructorParams(string someValue, DateTime now, string name)
                container.Register<ClassWithOnlyPrimitiveConstructorParams>(
                    convention.WithParameter<string>("foo"),
                    convention.WithParameter(() => DateTime.MinValue),
                    convention.WithParameter("name", () => "bar"));

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ArgumentException ex)
            {
                AssertThat.StringContains(
                    "Multiple parameter registrations found for type ClassWithOnlyPrimitiveConstructorParams " +
                    "that match to parameter with name 'name' of type String.",
                    ex.Message);
            }
        }
        public void Register_WithParameterWithNonExistingParamName_FailsWithExpectedException()
        {
            // Arrange
            var container = new Container();

            var convention = new ParameterConvention();

            container.Options.RegisterParameterConvention(convention);

            try
            {
                // Act
                // ctor: ClassWithPrimiveConstructorParams(string someValue, DateTime now, string name)
                container.Register<ClassWithOnlyPrimitiveConstructorParams>(
                    convention.WithParameter("notExistingParamName", "foo"),
                    convention.WithParameter(() => DateTime.MinValue),
                    convention.WithParameter("name", () => "bar"));

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ArgumentException ex)
            {
                AssertThat.StringContains(
                    "Parameter with name 'notExistingParamName' of type String is not a parameter of " +
                    "the constructor of type ClassWithOnlyPrimitiveConstructorParams",
                    ex.Message);
            }
        }
        public void GetInstance_Type_TypeWithMixedParameters_InjectsExpectedValues()
        {
            // Arrange
            var expectedDependency = new Dependency();
            decimal expectedDecimal = decimal.MaxValue;

            var container = new Container();
            var convention = new ParameterConvention();
            container.Options.RegisterParameterConvention(convention);
            container.RegisterSingleton<IDependency>(expectedDependency);

            // ctor: ClassWithAPrimitiveConstructorParam(IDependency dependency, Decimal someDecimal)
            container.RegisterSingleton<ClassWithAPrimitiveConstructorParam>(
                convention.WithParameter("someDecimal", expectedDecimal));

            // Act
            var instance = container.GetInstance<ClassWithAPrimitiveConstructorParam>();

            // Assert
            Assert.IsTrue(object.ReferenceEquals(expectedDependency, instance.Dependency));
            Assert.AreEqual(expectedDecimal, instance.SomeDecimal);
        }
        public void Register_TypeWithMixedParameters_Succeeds()
        {
            // Arrange
            var container = new Container();

            var convention = new ParameterConvention();

            container.Options.RegisterParameterConvention(convention);

            container.RegisterSingleton<IDependency>(new Dependency());

            // Act
            // ctor: ClassWithAPrimitiveConstructorParam(IDependency dependency, Decimal someDecimal)
            container.RegisterSingleton<ClassWithAPrimitiveConstructorParam>(
                convention.WithParameter("someDecimal", decimal.MinValue));
        }