Ejemplo n.º 1
0
        public void AutoResolveConstructor_ValueTypeWithNoConstructors()
        {
            Type       type       = typeof(int);
            Expression?expression = new DefaultConstructorResolver().ResolveConstructorExpression(type);

            Assert.IsType <DefaultExpression>(expression);
        }
        public void CreateReturnsInstanceCreatedWithCreatedParameters()
        {
            var constructorResolver = new DefaultConstructorResolver(CacheLevel.PerInstance);
            var constructorInfo     = constructorResolver.Resolve(typeof(SimpleConstructor), null) !;
            var args = new object[]
            {
                new SlimModel()
            };

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var typeResolver    = Substitute.For <ITypeResolver>();
            var configuration   = Substitute.For <IBuildConfiguration>();

            executeStrategy.CreateParameters(constructorInfo).Returns(args);
            configuration.ConstructorResolver.Returns(constructorResolver);
            configuration.TypeResolver.Returns(typeResolver);
            typeResolver.GetBuildType(configuration, Arg.Any <Type>()).Returns(x => x.Arg <Type>());
            executeStrategy.Configuration.Returns(configuration);

            var sut = new DefaultTypeCreator();

            var actual = sut.Create(executeStrategy, typeof(SimpleConstructor), null);

            actual.Should().NotBeNull();
        }
Ejemplo n.º 3
0
        public void ResolveReturnsConstructorThatMatchesAgainstNullParameters()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var actual = sut.Resolve(typeof(Other), Guid.NewGuid(), null !, 123, null !) !;

            actual.GetParameters().Length.Should().Be(4);
        }
Ejemplo n.º 4
0
        public void ResolveThrowsExceptionWhenArgsContainsNullAndHasArgParamTypeMismatch()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            Action action = () => sut.Resolve(typeof(Optionals), "first", "stuff", null !);

            _output.WriteLine(action.Should().Throw <MissingMemberException>().And.Message);
        }
Ejemplo n.º 5
0
        public void ResolveThrowsExceptionWhenArgsContainsNullNoOptionalParamsAndArgumentCountMismatch()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            Action action = () => sut.Resolve(typeof(WithMixedValueParameters), "first", null !, "this doesn't exist");

            _output.WriteLine(action.Should().Throw <MissingMemberException>().And.Message);
        }
Ejemplo n.º 6
0
        public void ResolveReturnsConstructorWhenArgsContainsNullAndNullArgIsCopyConstructor()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var actual = sut.Resolve(typeof(Clone), null !, "second") !;

            actual.GetParameters().Length.Should().Be(2);
        }
Ejemplo n.º 7
0
        public void ResolveReturnsParameterConstructor()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var constructor = sut.Resolve(typeof(WithValueParameters)) !;

            constructor.GetParameters().Should().NotBeEmpty();
        }
Ejemplo n.º 8
0
        public void ResolveReturnsNullForTypesWithoutPublicConstructors(Type targetType)
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var constructor = sut.Resolve(targetType);

            constructor.Should().BeNull();
        }
        public void ResolveReturnsDefaultConstructorWhenManyConstructorsAvailableTest()
        {
            var target = new DefaultConstructorResolver();

            var constructor = target.Resolve(typeof(WithMixedValueParameters));

            constructor.GetParameters().Should().BeEmpty();
        }
        public void ResolveReturnsDefaultConstructorOnSimpleModelTest()
        {
            var target = new DefaultConstructorResolver();

            var constructor = target.Resolve(typeof(Simple));

            constructor.GetParameters().Should().BeEmpty();
        }
        public void ResolveReturnsConstructorWithLeastParametersTest()
        {
            var target = new DefaultConstructorResolver();

            var constructor = target.Resolve(typeof(WithInterfaceAndAbstractParameters));

            constructor.GetParameters().Should().HaveCount(1);
        }
Ejemplo n.º 12
0
        public void ResolveThrowsExceptionWithNullType()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            Action action = () => sut.Resolve(null !);

            action.Should().Throw <ArgumentNullException>();
        }
Ejemplo n.º 13
0
        public void ResolveThrowsExceptionWhenNoConstructorMatchingSpecifiedParameters()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            Action action = () => sut.Resolve(typeof(Simple), Guid.NewGuid().ToString(), true, 123);

            _output.WriteLine(action.Should().Throw <MissingMemberException>().And.Message);
        }
Ejemplo n.º 14
0
        public void ResolveThrowsExceptionWhenArgsContainsNullMatchingValueTypeParameter()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            Action action = () => sut.Resolve(typeof(Other), Guid.NewGuid(), "NextParameterIsInt", null !, null !);

            _output.WriteLine(action.Should().Throw <MissingMemberException>().And.Message);
        }
Ejemplo n.º 15
0
        public void ResolveReturnsConstructorWithLeastParametersWhenArgsIsNull()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var constructor = sut.Resolve(typeof(WithInterfaceAndAbstractParameters), null !) !;

            constructor.GetParameters().Should().HaveCount(1);
        }
Ejemplo n.º 16
0
        public void ResolveMatchesConstructorMatchingParameterLengthWhereParameterValueIsNull()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var constructor = sut.Resolve(typeof(Person), (Person)null !) !;

            constructor.GetParameters().Length.Should().Be(1);
        }
Ejemplo n.º 17
0
        public void ResolveReturnsConstructorWhenArgsContainsNullableParameterWithNullValue()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var actual = sut.Resolve(typeof(WithConstructorParameters), null !, Guid.NewGuid(), null !, 123, true) !;

            actual.GetParameters().Length.Should().Be(5);
        }
Ejemplo n.º 18
0
        public void ResolveReturnsConstructorWhenArgsContainsNullAndArgCountFillsNonOptionalParam()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var actual = sut.Resolve(typeof(Optionals), null !, 10, "third") !;

            actual.GetParameters().Length.Should().Be(5);
        }
Ejemplo n.º 19
0
        public void ResolveReturnsDefaultConstructorOnSimpleModel()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var constructor = sut.Resolve(typeof(Simple)) !;

            constructor.GetParameters().Should().BeEmpty();
        }
Ejemplo n.º 20
0
        public void ResolveReturnsDefaultConstructorWhenManyConstructorsAvailable()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var constructor = sut.Resolve(typeof(WithMixedValueParameters)) !;

            constructor.GetParameters().Should().BeEmpty();
        }
        public void ResolveReturnsConstructorWithLeastParametersExcludingConstructorsWithSameTypeTest()
        {
            var target = new DefaultConstructorResolver();

            var constructor = target.Resolve(typeof(Other));

            constructor.GetParameters().Should().HaveCount(3);
            constructor.GetParameters().Should().NotContain(x => x.ParameterType == typeof(Other));
        }
Ejemplo n.º 22
0
        public void ResolveReturnsConstructorForStructThatHasConstructors()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var constructor = sut.Resolve(typeof(KeyValuePair <Guid, Person>)) !;

            constructor.Should().NotBeNull();
            constructor.GetParameters().Should().HaveCount(2);
        }
Ejemplo n.º 23
0
        public void ResolveReturnsConstructorWithLeastParametersExcludingConstructorsWithSameType()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var constructor = sut.Resolve(typeof(Other)) !;

            constructor.GetParameters().Should().HaveCount(3);
            constructor.GetParameters().Should().NotContain(x => x.ParameterType == typeof(Other));
        }
Ejemplo n.º 24
0
        public void ResolveReturnsConstructorBasedOnCacheLevel(CacheLevel cacheLevel)
        {
            var sut = new DefaultConstructorResolver(cacheLevel);

            var first  = sut.Resolve(typeof(Other));
            var second = sut.Resolve(typeof(Other));

            first.Should().BeSameAs(second);
        }
Ejemplo n.º 25
0
        public void ResolveThrowsExceptionWhenArgsContainsNullAndParamIsSubtype()
        {
            var company = new Company();

            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            Action action = () => sut.Resolve(typeof(Derived), null !, company, "third");

            _output.WriteLine(action.Should().Throw <MissingMemberException>().And.Message);
        }
Ejemplo n.º 26
0
        public void ResolveReturnsConstructorWhenArgsContainsNullAndMatchesDerivedType()
        {
            var company = new SpecificCompany();

            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var actual = sut.Resolve(typeof(Derived), null !, company) !;

            actual.GetParameters().Length.Should().Be(2);
        }
Ejemplo n.º 27
0
        public void ResolveMatchesConstructorWithMatchingParametersTypes()
        {
            var args = new object[]
            {
                "first", "last", DateTime.UtcNow, true, Guid.NewGuid(), Environment.TickCount
            };

            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var constructor = sut.Resolve(typeof(WithValueParameters), args) !;

            constructor.GetParameters().Length.Should().Be(6);
        }
Ejemplo n.º 28
0
        public void ResolveMatchesConstructorWithDerivedParameterTypes()
        {
            var person = new Person
            {
                Id = Guid.NewGuid()
            };

            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var constructor = sut.Resolve(typeof(Person), person) !;

            constructor.GetParameters().Length.Should().Be(1);
        }
        public void ResolveMatchesConstructorWithDerivedParameterTypesTest()
        {
            var person = new Person
            {
                Id = Guid.NewGuid()
            };

            var target = new DefaultConstructorResolver();

            var constructor = target.Resolve(typeof(Person), person);

            constructor.GetParameters().Length.Should().Be(1);
        }
Ejemplo n.º 30
0
        public void ResolveThrowsExceptionWhenArgsContainsNullAndTooManyArgumentsProvided()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            Action action = () => sut.Resolve(
                typeof(Other),
                Guid.NewGuid(),
                null,
                123,
                null,
                "ThisParamDoesn'tMatch");

            _output.WriteLine(action.Should().Throw <MissingMemberException>().And.Message);
        }
        public void ResolveMatchesConstructorWithMatchingParametersTypesTest()
        {
            var target = new DefaultConstructorResolver();

            var constructor = target.Resolve(
                typeof(WithValueParameters),
                "first",
                "last",
                DateTime.UtcNow,
                true,
                Guid.NewGuid(),
                Environment.TickCount);

            constructor.GetParameters().Length.Should().Be(6);
        }
Ejemplo n.º 32
0
        public void CombinedManual()
        {
            var        resolver = new DefaultConstructorResolver();
            Expression singleton1NewExpression = resolver.TryResolveConstructorExpression(typeof(Singleton1));
            Delegate   action1 = Expression.Lambda(singleton1NewExpression).Compile();
            object     value   = action1.DynamicInvoke();

            singleton1NewExpression = Expression.Constant(value);

            Expression transient1NewExpression = resolver.TryResolveConstructorExpression(typeof(Transient1));

            NewExpression expression = Expression.New(resolver.StaticSelectConstructor(typeof(Combined1)), singleton1NewExpression, transient1NewExpression);
            Delegate      action     = Expression.Lambda(expression).Compile();
            var           func       = (Func <object>)action;
            object        instance   = func.Invoke();
        }
Ejemplo n.º 33
0
        public void ResolveThrowsExceptionWhenParameterValuesDoNotMatchParameterTypes()
        {
            var sut = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var priority = Convert.ToDouble(Environment.TickCount);

            Action action = () => sut.Resolve(
                typeof(WithValueParameters),
                "first",
                "last",
                DateTime.UtcNow,
                true,
                Guid.NewGuid(),
                priority);

            _output.WriteLine(action.Should().Throw <MissingMemberException>().And.Message);
        }
        public void CreateReturnsInstanceCreatedWithResolvedDefaultConstructorWhenArgumentsIsNull()
        {
            var constructorResolver = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var typeResolver    = Substitute.For <ITypeResolver>();
            var configuration   = Substitute.For <IBuildConfiguration>();

            configuration.ConstructorResolver.Returns(constructorResolver);
            configuration.TypeResolver.Returns(typeResolver);
            typeResolver.GetBuildType(configuration, Arg.Any <Type>()).Returns(x => x.Arg <Type>());
            executeStrategy.Configuration.Returns(configuration);

            var sut = new DefaultTypeCreator();

            var actual = sut.Create(executeStrategy, typeof(Person), null);

            actual.Should().NotBeNull();
        }
        public void PopulateReturnsProvidedInstance()
        {
            var expected            = Model.Create <Simple>() !;
            var buildChain          = new BuildHistory();
            var constructorResolver = new DefaultConstructorResolver(CacheLevel.PerInstance);

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var typeResolver    = Substitute.For <ITypeResolver>();
            var configuration   = Substitute.For <IBuildConfiguration>();

            configuration.TypeResolver.Returns(typeResolver);
            typeResolver.GetBuildType(configuration, Arg.Any <Type>()).Returns(x => x.Arg <Type>());
            executeStrategy.BuildChain.Returns(buildChain);
            executeStrategy.Configuration.Returns(configuration);
            configuration.ConstructorResolver.Returns(constructorResolver);

            var sut = new DefaultTypeCreator();

            var actual = sut.Populate(executeStrategy, expected);

            actual.Should().Be(expected);
        }
        public void ResolveThrowsExceptionWhenClassOnlyContainsCopyConstructorTest()
        {
            var target = new DefaultConstructorResolver();

            Action action = () => target.Resolve(typeof(Copy));

            _output.WriteLine(action.ShouldThrow<MissingMemberException>().And.Message);
        }
        public void ResolveThrowsExceptionWhenNoPublicConstructorFoundTest()
        {
            var target = new DefaultConstructorResolver();

            Action action = () => target.Resolve(typeof(Singleton));

            _output.WriteLine(action.ShouldThrow<MissingMemberException>().And.Message);
        }
        public void ResolveThrowsExceptionWhenParameterValuesDoNotMatchParameterTypesTest()
        {
            var target = new DefaultConstructorResolver();

            var priority = Convert.ToDouble(Environment.TickCount);

            Action action =
                () =>
                    target.Resolve(
                        typeof(WithValueParameters),
                        "first",
                        "last",
                        DateTime.UtcNow,
                        true,
                        Guid.NewGuid(),
                        priority);

            _output.WriteLine(action.ShouldThrow<MissingMemberException>().And.Message);
        }
        public void ResolveThrowsExceptionWithNullTypeTest()
        {
            var target = new DefaultConstructorResolver();

            Action action = () => target.Resolve(null);

            action.ShouldThrow<ArgumentNullException>();
        }
        public void ResolveReturnsParameterConstructorTest()
        {
            var target = new DefaultConstructorResolver();

            var constructor = target.Resolve(typeof(WithValueParameters));

            constructor.GetParameters().Should().NotBeEmpty();
        }
        public void CreateWithThrowsExceptionWhenNoGeneratorOrCreatorMatchFoundForConstructorParameterTest()
        {
            var generators = new List<IValueGenerator>();
            var creators = new List<ITypeCreator>();
            var constructorResolver = new DefaultConstructorResolver();

            var valueGenerator = Substitute.For<IValueGenerator>();
            var typeCreator = Substitute.For<ITypeCreator>();
            var buildStrategy = Substitute.For<IBuildStrategy>();

            generators.Add(valueGenerator);
            creators.Add(typeCreator);

            typeCreator.CanCreate(typeof(KeyValuePair<string, Person>), null, Arg.Any<LinkedList<object>>())
                .Returns(true);
            typeCreator.AutoDetectConstructor.Returns(true);
            typeCreator.AutoPopulate.Returns(true);
            valueGenerator.IsSupported(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>()).Returns(false);
            buildStrategy.TypeCreators.Returns(creators.AsReadOnly());
            buildStrategy.ValueGenerators.Returns(generators.AsReadOnly());
            buildStrategy.ConstructorResolver.Returns(constructorResolver);

            var target = new DefaultExecuteStrategy();

            target.Initialize(buildStrategy, buildStrategy.GetBuildLog());

            Action action = () => target.CreateWith(typeof(KeyValuePair<string, Person>));

            action.ShouldThrow<BuildException>();
        }
        public void SetConstructorResolverWithResolverAssignsResolverToCompilerTest()
        {
            var resolver = new DefaultConstructorResolver();

            var target = new BuildStrategyCompiler();

            target.SetConstructorResolver(resolver);

            target.ConstructorResolver.Should().Be(resolver);
        }
        public void SetConstructorResolverWithResolverThrowsExceptionWithNullCompilerTest()
        {
            var resolver = new DefaultConstructorResolver();

            IBuildStrategyCompiler target = null;

            Action action = () => target.SetConstructorResolver(resolver);

            action.ShouldThrow<ArgumentNullException>();
        }
        public void ResolveThrowsExceptionWhenNoConstructorMatchingSpecifiedParametersTest()
        {
            var target = new DefaultConstructorResolver();

            Action action = () => target.Resolve(typeof(Simple), Guid.NewGuid().ToString(), true, 123);

            _output.WriteLine(action.ShouldThrow<MissingMemberException>().And.Message);
        }
Ejemplo n.º 45
0
        public void CacheLevelReturnsConstructorValue(CacheLevel cacheLevel)
        {
            var sut = new DefaultConstructorResolver(cacheLevel);

            sut.CacheLevel.Should().Be(cacheLevel);
        }