Inheritance: Entity
        public void GetSourceValueReturnsNullFromSourcePropertyTest()
        {
            var context = new Person();

            var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.LastName);

            var actual = target.ReadSourceValue(context);

            actual.Should().BeNull();
        }
        public void GetSourceValueThrowsExceptionWhenNoSourceExpressionProvidedTest()
        {
            var context = new Person();

            var target = new GeneratorWrapper(PropertyExpression.FirstName, (Regex)null, (Type)null);

            Action action = () => target.ReadSourceValue(context);

            action.ShouldThrow<InvalidOperationException>();
        }
        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);
        }
        public void GetSourceValueReturnsValueFromSourcePropertyTest(Gender gender, string expected)
        {
            var context = new Person
            {
                Gender = gender
            };

            var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.Gender);

            var actual = target.ReadSourceValue(context);

            actual.Should().Be(expected);
        }
        public void GetSourceValueReturnsValueFromSourceStringPropertyTest()
        {
            var context = new Person
            {
                LastName = Guid.NewGuid().ToString()
            };

            var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.LastName);

            var actual = target.ReadSourceValue(context);

            actual.Should().Be(context.LastName);
        }
        public void GetValueThrowsExceptionWithNullExpressionTest()
        {
            var context = new Person();

            var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.FirstName, (Type)null);

            Action action = () => target.ReadValue(null, context);

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

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

            action.ShouldThrow<ArgumentNullException>();
        }
        public void SetRunsActionAgainstInstanceTest()
        {
            var value = Guid.NewGuid();
            var target = new Person();

            var actual = target.Set(x => x.Id = value);

            actual.Should().BeSameAs(target);
            actual.Id.Should().Be(value);
        }
        public void PopulatingInstanceAppendsLogEntryTest()
        {
            var instance = new Person();

            var target = new DefaultBuildLog();

            target.PopulatingInstance(instance);

            target.Output.Should().NotBeNullOrWhiteSpace();
        }
        public void PopulateInstanceIndentsChildMessagesTest()
        {
            var instance = new Person();

            var target = new DefaultBuildLog();

            target.PopulatingInstance(instance);
            target.CreatingValue(typeof(string), null);
            target.PopulatedInstance(instance);

            var actual = target.Output;

            actual.Should().Contain("    ");
        }
        public void PopulateThrowsExceptionWhenNotInitializedTest()
        {
            var value = new Person();

            var target = new DefaultExecuteStrategy();

            Action action = () => target.Populate(value);

            action.ShouldThrow<InvalidOperationException>();
        }
        public void PopulateThrowsExceptionWithNullBuildStrategyTest()
        {
            var model = new Person();

            IBuildStrategy target = null;

            Action action = () => target.Populate(model);

            action.ShouldThrow<ArgumentNullException>();
        }
        public void PopulateCanBuildExistingInstanceTest()
        {
            var entity = new Person();
            var actual = Model.Populate(entity);

            actual.Should().NotBeNull();
            actual.DOB.Should().NotBe(default(DateTime));
            actual.FirstName.Should().NotBeNullOrWhiteSpace();
            actual.LastName.Should().NotBeNullOrWhiteSpace();
            actual.Id.Should().NotBeEmpty();
            actual.Priority.Should().NotBe(0);
        }
        public void CreateWithThrowsExceptionWhenNoGeneratorOrCreatorMatchFoundForChildPropertyTest()
        {
            var person = new Person();
            var generators = new List<IValueGenerator>();
            var creators = new List<ITypeCreator>();

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

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

            typeCreator.CanCreate(typeof(Person), null, Arg.Any<LinkedList<object>>()).Returns(true);
            typeCreator.Create(typeof(Person), null, Arg.Any<LinkedList<object>>()).Returns(person);
            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());

            var target = new DefaultExecuteStrategy();

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

            Action action = () => target.CreateWith(typeof(Person));

            action.ShouldThrow<BuildException>();
        }
        public void CreateWithThrowsExceptionWhenGeneratingValueFailsTest()
        {
            var person = new Person();
            var generators = new List<IValueGenerator>();
            var creators = new List<ITypeCreator>();

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

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

            typeCreator.CanCreate(typeof(Person), null, Arg.Any<LinkedList<object>>()).Returns(true);
            typeCreator.Create(typeof(Person), null, null, null).Returns(person);
            valueGenerator.IsSupported(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>()).Returns(true);
            valueGenerator.Generate(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>())
                .Throws(new InvalidOperationException());
            buildStrategy.TypeCreators.Returns(creators.AsReadOnly());
            buildStrategy.ValueGenerators.Returns(generators.AsReadOnly());

            var target = new DefaultExecuteStrategy();

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

            Action action = () => target.CreateWith(typeof(Person));

            var exception =
                action.ShouldThrow<BuildException>().Where(x => x.Message != null).Where(x => x.BuildLog != null).Which;

            _output.WriteLine(exception.Message);
        }
        public void CreateWithReturnsValueFromCreatorWithNoArgumentsAndDetectConstructorEnabledTest()
        {
            var expected = new Person();
            var typeCreators = new List<ITypeCreator>();

            var typeCreator = Substitute.For<ITypeCreator>();
            var buildStrategy = Substitute.For<IBuildStrategy>();
            var resolver = Substitute.For<IConstructorResolver>();

            typeCreators.Add(typeCreator);

            buildStrategy.TypeCreators.Returns(typeCreators.AsReadOnly());
            buildStrategy.ConstructorResolver.Returns(resolver);

            var target = new DefaultExecuteStrategy();

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

            resolver.Resolve(typeof(Person))
                .Returns(typeof(Person).GetConstructors().Single(x => x.GetParameters().Length == 0));
            typeCreator.CanCreate(typeof(Person), null, Arg.Any<LinkedList<object>>()).Returns(true);
            typeCreator.Create(typeof(Person), null, Arg.Any<LinkedList<object>>()).Returns(expected);
            typeCreator.Populate(expected, target).Returns(expected);
            typeCreator.AutoPopulate.Returns(false);
            typeCreator.AutoDetectConstructor.Returns(true);

            var actual = target.CreateWith(typeof(Person));

            actual.Should().BeSameAs(expected);
        }
        public void CreateWithReturnsValueCreatedFromProvidedArgumentsTest()
        {
            var expected = new Person();
            var args = new object[]
            {
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                DateTime.UtcNow,
                true,
                Guid.NewGuid(),
                Environment.TickCount
            };
            var typeCreators = new List<ITypeCreator>();

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

            typeCreators.Add(typeCreator);

            buildStrategy.TypeCreators.Returns(typeCreators.AsReadOnly());

            var target = new DefaultExecuteStrategy();

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

            typeCreator.CanCreate(typeof(Person), null, Arg.Any<LinkedList<object>>()).Returns(true);
            typeCreator.Create(typeof(Person), null, Arg.Any<LinkedList<object>>(), args).Returns(expected);
            typeCreator.Populate(expected, target).Returns(expected);
            typeCreator.AutoPopulate.Returns(false);

            var actual = target.CreateWith(typeof(Person), args);

            actual.Should().BeSameAs(expected);
        }
        public void PopulateAssignsValueUsingDefaultExecuteOrderRulesTest()
        {
            var first = SimpleEnum.Seventh;
            var second = Environment.TickCount;
            var third = Guid.NewGuid().ToString();
            var fourth = new Person();
            var expected = new PopulateOrderItem();
            var valueGenerators = new List<IValueGenerator>();
            var typeCreators = new List<ITypeCreator>();
            var executeOrderRules = Model.BuildStrategy.ExecuteOrderRules;

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

            typeCreators.Add(typeCreator);
            valueGenerators.Add(valueGenerator);

            buildStrategy.TypeCreators.Returns(typeCreators.AsReadOnly());
            buildStrategy.ValueGenerators.Returns(valueGenerators.AsReadOnly());
            buildStrategy.ExecuteOrderRules.Returns(executeOrderRules);

            var target = new DefaultExecuteStrategy();

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

            valueGenerator.IsSupported(
                typeof(SimpleEnum),
                "Z",
                Arg.Is<LinkedList<object>>(x => x.Last.Value == expected)).Returns(true);
            valueGenerator.Generate(typeof(SimpleEnum), "Z", Arg.Is<LinkedList<object>>(x => x.Last.Value == expected))
                .Returns(first);
            valueGenerator.IsSupported(typeof(int), "Y", Arg.Is<LinkedList<object>>(x => x.Last.Value == expected))
                .Returns(true);
            valueGenerator.Generate(typeof(int), "Y", Arg.Is<LinkedList<object>>(x => x.Last.Value == expected))
                .Returns(second);
            valueGenerator.IsSupported(typeof(string), "X", Arg.Is<LinkedList<object>>(x => x.Last.Value == expected))
                .Returns(true);
            valueGenerator.Generate(typeof(string), "X", Arg.Is<LinkedList<object>>(x => x.Last.Value == expected))
                .Returns(third);
            typeCreator.CanCreate(typeof(Person), "W", Arg.Is<LinkedList<object>>(x => x.Last.Value == expected))
                .Returns(true);
            typeCreator.Create(typeof(Person), "W", Arg.Is<LinkedList<object>>(x => x.Last.Value == expected))
                .Returns(fourth);
            typeCreator.Populate(fourth, target).Returns(fourth);

            var actual = (PopulateOrderItem)target.Populate(expected);

            actual.Should().BeSameAs(expected);
            actual.Z.Should().Be(first);
            actual.Y.Should().Be(second);
            actual.X.Should().Be(third);
            actual.W.Should().BeSameAs(fourth);
        }
        public void PopulateInferesItemTypeByArrayTypeWhenFirstItemIsNullTest()
        {
            var actual = new Person[15];
            var executeStrategy = Model.BuildStrategy.GetExecuteStrategy<List<int>>();

            var target = new ArrayTypeCreator();

            var result = (Person[]) target.Populate(actual, executeStrategy);

            result.All(x => x != null).Should().BeTrue();
        }
        public void PopulateThrowsExceptionWithNullExecuteStrategyTest()
        {
            var person = new Person();

            var target = new TypeCreatorWrapper();

            Action action = () => target.Populate(person, null);

            action.ShouldThrow<ArgumentNullException>();
        }
        public void CreatePropertyAppendsLogEntryTest()
        {
            var context = new Person();

            var target = new DefaultBuildLog();

            target.CreateProperty(typeof(string), "FirstName", context);

            target.Output.Should().NotBeNullOrWhiteSpace();
        }
        public void PopulateInstanceThrowsExceptionWhenNotInitializedTest()
        {
            var value = new Person();

            var target = new PopulateInstanceWrapper();

            Action action = () => target.RunTest(value);

            action.ShouldThrow<InvalidOperationException>();
        }
        public void CreateChildItemThrowsExceptionWithNullExecuteStrategyTest()
        {
            var person = new Person();

            var target = new ArrayTypeCreatorWrapper();

            Action action = () => target.CreateItem(typeof(Person[]), null, person);

            action.ShouldThrow<ArgumentNullException>();
        }
        public void CreatePropertyValidatesPropertysTest(Type propertyType, string propertyName, bool includeContext)
        {
            Person context = null;

            if (includeContext)
            {
                context = new Person();
            }

            var target = new DefaultBuildLog();

            Action action = () => target.CreateProperty(propertyType, propertyName, context);

            action.ShouldThrow<ArgumentNullException>();
        }