public void BuildForParameterInfoReturnsValueFromRuleWithHighestPriority()
        {
            var parameterInfo = typeof(Person).GetConstructors()
                                .First(x => x.GetParameters().FirstOrDefault()?.Name == "firstName").GetParameters().First();
            var buildConfiguration = new BuildConfiguration();
            var expected           = new Person();

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var firstRule       = Substitute.For <ICreationRule>();
            var secondRule      = Substitute.For <ICreationRule>();

            buildConfiguration.CreationRules.Add(firstRule);
            buildConfiguration.CreationRules.Add(secondRule);

            executeStrategy.Configuration.Returns(buildConfiguration);
            executeStrategy.Log.Returns(_buildLog);
            firstRule.Priority.Returns(10);
            firstRule.IsMatch(parameterInfo).Returns(true);
            secondRule.Priority.Returns(20);
            secondRule.IsMatch(parameterInfo).Returns(true);
            secondRule.Create(executeStrategy, parameterInfo).Returns(expected);

            var sut = new CreationRuleBuildAction();

            var actual = sut.Build(executeStrategy, parameterInfo);

            actual.Should().Be(expected);
        }
        public void BuildForTypeReturnsValueFromRuleWithHighestPriority()
        {
            var type = typeof(Person);
            var buildConfiguration = new BuildConfiguration();
            var expected           = new Person();

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var firstRule       = Substitute.For <ICreationRule>();
            var secondRule      = Substitute.For <ICreationRule>();

            buildConfiguration.CreationRules.Add(firstRule);
            buildConfiguration.CreationRules.Add(secondRule);

            executeStrategy.Configuration.Returns(buildConfiguration);
            executeStrategy.Log.Returns(_buildLog);
            firstRule.Priority.Returns(10);
            firstRule.IsMatch(type).Returns(true);
            secondRule.Priority.Returns(20);
            secondRule.IsMatch(type).Returns(true);
            secondRule.Create(executeStrategy, type).Returns(expected);

            var sut = new CreationRuleBuildAction();

            var actual = sut.Build(executeStrategy, type);

            actual.Should().Be(expected);
        }
        public void BuildThrowsExceptionWhenRuleFails()
        {
            var propertyInfo       = typeof(Person).GetProperty(nameof(Person.FirstName)) !;
            var buildConfiguration = new BuildConfiguration();

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var rule            = Substitute.For <ICreationRule>();

            buildConfiguration.CreationRules.Add(rule);

            executeStrategy.Configuration.Returns(buildConfiguration);
            executeStrategy.Log.Returns(_buildLog);
            rule.IsMatch(propertyInfo).Returns(true);
            rule.Create(executeStrategy, propertyInfo).Throws <TimeoutException>();

            var sut = new CreationRuleBuildAction();

            Action action = () => sut.Build(executeStrategy, propertyInfo);

            var exception = action.Should().Throw <BuildException>().Which;

            exception.InnerException.Should().BeOfType <TimeoutException>();

            _output.WriteLine(exception.Message);
        }
        public void BuildForTypeThrowsExceptionWithNullType()
        {
            var executeStrategy = Substitute.For <IExecuteStrategy>();

            var sut = new CreationRuleBuildAction();

            Action action = () => sut.Build(executeStrategy, (Type)null !);

            action.Should().Throw <ArgumentNullException>();
        }
        public void BuildForTypeReturnsNullWhenNoRulesExist()
        {
            var type = typeof(Person);
            var buildConfiguration = new BuildConfiguration();

            var executeStrategy = Substitute.For <IExecuteStrategy>();

            executeStrategy.Configuration.Returns(buildConfiguration);
            executeStrategy.Log.Returns(_buildLog);

            var sut = new CreationRuleBuildAction();

            var actual = sut.Build(executeStrategy, type);

            actual.Should().BeNull();
        }
        public void BuildForParameterInfoReturnsNullWhenNoRulesExist()
        {
            var parameterInfo = typeof(Person).GetConstructors()
                                .First(x => x.GetParameters().FirstOrDefault()?.Name == "firstName").GetParameters().First();
            var buildConfiguration = new BuildConfiguration();

            var executeStrategy = Substitute.For <IExecuteStrategy>();

            executeStrategy.Configuration.Returns(buildConfiguration);
            executeStrategy.Log.Returns(_buildLog);

            var sut = new CreationRuleBuildAction();

            var actual = sut.Build(executeStrategy, parameterInfo);

            actual.Should().BeNull();
        }
        public void BuildForTypeReturnsRuleValueWhenMatchingRuleFound()
        {
            var type = typeof(Person);
            var buildConfiguration = new BuildConfiguration();
            var expected           = new Person();

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var rule            = Substitute.For <ICreationRule>();

            buildConfiguration.CreationRules.Add(rule);

            executeStrategy.Configuration.Returns(buildConfiguration);
            executeStrategy.Log.Returns(_buildLog);
            rule.IsMatch(type).Returns(true);
            rule.Create(executeStrategy, type).Returns(expected);

            var sut = new CreationRuleBuildAction();

            var actual = sut.Build(executeStrategy, type);

            actual.Should().Be(expected);
        }
        public void BuildForParameterInfoReturnsRuleValueWhenMatchingRuleFound()
        {
            var parameterInfo = typeof(Person).GetConstructors()
                                .First(x => x.GetParameters().FirstOrDefault()?.Name == "firstName").GetParameters().First();
            var buildConfiguration = new BuildConfiguration();
            var expected           = new Person();

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var rule            = Substitute.For <ICreationRule>();

            buildConfiguration.CreationRules.Add(rule);

            executeStrategy.Configuration.Returns(buildConfiguration);
            executeStrategy.Log.Returns(_buildLog);
            rule.IsMatch(parameterInfo).Returns(true);
            rule.Create(executeStrategy, parameterInfo).Returns(expected);

            var sut = new CreationRuleBuildAction();

            var actual = sut.Build(executeStrategy, parameterInfo);

            actual.Should().Be(expected);
        }
        public void BuildLogsValueCreation()
        {
            var type = typeof(Person);
            var buildConfiguration = new BuildConfiguration();
            var expected           = new Person();

            var buildLog        = Substitute.For <IBuildLog>();
            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var rule            = Substitute.For <ICreationRule>();

            buildConfiguration.CreationRules.Add(rule);

            executeStrategy.Configuration.Returns(buildConfiguration);
            executeStrategy.Log.Returns(buildLog);
            rule.IsMatch(type).Returns(true);
            rule.Create(executeStrategy, type).Returns(expected);

            var sut = new CreationRuleBuildAction();

            sut.Build(executeStrategy, type);

            buildLog.Received().CreatingValue(type, rule.GetType(), null !);
        }
        public void BuildRethrowsBuildException()
        {
            var parameterInfo = typeof(Person).GetConstructors()
                                .First(x => x.GetParameters().FirstOrDefault()?.Name == "firstName").GetParameters().First();
            var buildConfiguration = new BuildConfiguration();

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var rule            = Substitute.For <ICreationRule>();

            buildConfiguration.CreationRules.Add(rule);

            executeStrategy.Configuration.Returns(buildConfiguration);
            executeStrategy.Log.Returns(_buildLog);
            rule.IsMatch(parameterInfo).Returns(true);
            rule.Create(executeStrategy, parameterInfo).Throws <BuildException>();

            var sut = new CreationRuleBuildAction();

            Action action = () => sut.Build(executeStrategy, parameterInfo);

            var exception = action.Should().Throw <BuildException>().Which;

            _output.WriteLine(exception.Message);
        }