public void CreateEvaluatesPostBuildActionsInOrderOfDescendingPriorityTest()
        {
            var firstAction = Substitute.For<IPostBuildAction>();
            var secondAction = Substitute.For<IPostBuildAction>();
            var buildStrategy = new DefaultBuildStrategyCompiler().Add(firstAction).Add(secondAction).Compile();
            var executeCount = 0;

            firstAction.IsSupported(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>()).Returns(true);
            secondAction.IsSupported(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>()).Returns(true);
            firstAction.WhenForAnyArgs(x => x.Execute(null, null, null)).Do(
                x =>
                {
                    executeCount++;

                    executeCount.Should().Be(1);
                });
            secondAction.WhenForAnyArgs(x => x.Execute(null, null, null)).Do(
                x =>
                {
                    executeCount++;

                    executeCount.Should().Be(2);
                });

            var target = new DefaultExecuteStrategy();

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

            target.Create(typeof(Simple));

            firstAction.Received().Execute(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>());
            secondAction.Received().Execute(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>());
        }
        public void CreatesWithDefaultConfigurationTest()
        {
            var target = new DefaultBuildStrategyCompiler();

            target.IgnoreRules.Should().BeEmpty();
            target.ConstructorResolver.Should().BeOfType<DefaultConstructorResolver>();
            target.CreationRules.Should().BeEmpty();
            target.TypeCreators.Should().NotBeEmpty();
            target.ValueGenerators.Should().NotBeEmpty();
            target.ExecuteOrderRules.Should().NotBeEmpty();
            target.PostBuildActions.Should().BeEmpty();
        }
        public void CloneReturnsCompilerWithBuildStrategyConfigurationTest()
        {
            var target = new DefaultBuildStrategyCompiler().AddIgnoreRule<Person>(x => x.Address).Compile();

            var actual = target.Clone();

            actual.ConstructorResolver.Should().Be(target.ConstructorResolver);
            actual.TypeCreators.ShouldBeEquivalentTo(target.TypeCreators);
            actual.ValueGenerators.ShouldBeEquivalentTo(target.ValueGenerators);
            actual.IgnoreRules.ShouldBeEquivalentTo(target.IgnoreRules);
            actual.ExecuteOrderRules.ShouldBeEquivalentTo(target.ExecuteOrderRules);
        }
        public void TypeCreatorsIncludesAllAvailableTypeCreatorsTest()
        {
            var types = from x in typeof(DefaultBuildStrategyCompiler).Assembly.GetTypes()
                where typeof(ITypeCreator).IsAssignableFrom(x) && x.IsAbstract == false && x.IsInterface == false
                select x;

            var target = new DefaultBuildStrategyCompiler();

            foreach (var type in types)
            {
                target.TypeCreators.Should().Contain(x => x.GetType() == type);
            }
        }
        public void DefaultExecuteOrderRulesReturnsExecutableRulesTest()
        {
            var rules = new DefaultBuildStrategyCompiler().ExecuteOrderRules.ToList();

            rules.Should().NotBeEmpty();

            foreach (var rule in rules)
            {
                Action action = () => rule.IsMatch(typeof(string), "Stuff");

                action.ShouldNotThrow();
            }
        }
        public void ValueGeneratorsIncludesAllAvailableValueGeneratorsExceptMailinatorTest()
        {
            var types = from x in typeof(DefaultBuildStrategyCompiler).Assembly.GetTypes()
                where
                    typeof(IValueGenerator).IsAssignableFrom(x) && x.IsAbstract == false && x.IsInterface == false &&
                    x != typeof(MailinatorEmailValueGenerator)
                select x;

            var target = new DefaultBuildStrategyCompiler();

            foreach (var type in types)
            {
                target.ValueGenerators.Should().Contain(x => x.GetType() == type);
            }
        }
        public void CreateEvaluatesPostBuildActionsThatSupportTheBuildScenarioTest()
        {
            var firstAction = Substitute.For<IPostBuildAction>();
            var secondAction = Substitute.For<IPostBuildAction>();
            var buildStrategy = new DefaultBuildStrategyCompiler().Add(firstAction).Add(secondAction).Compile();

            firstAction.IsSupported(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>()).Returns(false);
            secondAction.IsSupported(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>()).Returns(true);

            var target = new DefaultExecuteStrategy();

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

            target.Create(typeof(Simple));

            firstAction.DidNotReceive().Execute(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>());
            secondAction.Received().Execute(Arg.Any<Type>(), Arg.Any<string>(), Arg.Any<LinkedList<object>>());
        }
        private static IBuildStrategy CreateBuildStrategy()
        {
            var compiler = new DefaultBuildStrategyCompiler();

            compiler.AddExecuteOrderRule <Skill>(x => x.YearStarted, 30000);
            compiler.AddExecuteOrderRule <Skill>(x => x.YearLastUsed, 20000);
            compiler.AddCreationRule <UpdatableProfile>(x => x.AcceptCoC, 5000, true);
            compiler.AddCreationRule <UpdatableProfile>(x => x.AcceptToS, 5000, true);
            compiler.AddCreationRule <UpdatableProfile>(x => x.Status, 5000, ProfileStatus.Available);
            compiler.AddCreationRule <Profile>(x => x.AcceptCoC, 5000, true);
            compiler.AddCreationRule <Profile>(x => x.AcceptToS, 5000, true);
            compiler.AddCreationRule <Profile>(x => x.BannedAt, 5000, null);
            compiler.AddCreationRule <Profile>(x => x.AcceptedCoCAt, 5000, null);
            compiler.AddCreationRule <Profile>(x => x.AcceptedToSAt, 5000, null);
            compiler.AddCreationRule <Profile>(x => x.Status, 5000, ProfileStatus.Available);

            compiler.AddValueGenerator <BirthYearValueGenerator>();
            compiler.AddValueGenerator <YearInPastValueGenerator>();

            return(compiler.Compile());
        }
        public void MailinatorEmailGeneratorIsAssignedAgainstAllInstancesTest()
        {
            var strategy =
                new DefaultBuildStrategyCompiler().RemoveValueGenerator<EmailValueGenerator>()
                    .AddValueGenerator<MailinatorEmailValueGenerator>()
                    .Compile();

            var actual = strategy.Create<List<Person>>();

            actual.All(x => x.PersonalEmail.EndsWith("@mailinator.com")).Should().BeTrue();
            actual.All(x => x.WorkEmail.EndsWith("@mailinator.com")).Should().BeTrue();
        }
        public void CreateThrowsExceptionWhenPropertyCannotBeCreatedTest()
        {
            var typeCreator = Substitute.For<ITypeCreator>();

            typeCreator.CanCreate(typeof(Address), "Address", Arg.Any<LinkedList<object>>()).Returns(true);
            typeCreator.Priority.Returns(int.MaxValue);
            typeCreator.AutoDetectConstructor.Returns(true);
            typeCreator.AutoPopulate.Returns(true);
            typeCreator.Create(typeof(Address), "Address", Arg.Any<LinkedList<object>>())
                .Throws(new InvalidOperationException());

            var buildStrategy = new DefaultBuildStrategyCompiler().Add(typeCreator).Compile();

            var target = new DefaultExecuteStrategy<Company>();

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

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

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

            _output.WriteLine(exception.Message);
        }
        public void ValueGeneratorsDoesNotIncludeMailinatorEmailValueGeneratorTest()
        {
            var target = new DefaultBuildStrategyCompiler();

            target.ValueGenerators.Should().NotContain(x => x.GetType() == typeof(MailinatorEmailValueGenerator));
        }