Пример #1
0
 public void Should_ThrowException_When_SingleError_Added_With_NullMessage()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         MemberValidatorCreator.Create <object, object>(b => b
                                                        .SetSingleError(null));
     });
 }
Пример #2
0
            public void Should_SetSetOptional()
            {
                var validator = MemberValidatorCreator.Create <object, IEnumerable <MemberClass> >(b => b
                                                                                                   .SetOptional()
                                                                                                   );

                Assert.True(validator.IsOptional);
            }
Пример #3
0
 public void Should_ThrowException_When_Valid_WithParameters_And_NullPredicate()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         MemberValidatorCreator.Create <object, object>(b => b
                                                        .Valid(null, "test")
                                                        );
     });
 }
Пример #4
0
 public void Should_ThrowException_When_Valid_And_ArgsWithoutMessage()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         MemberValidatorCreator.Create <object, object>(b => b
                                                        .Valid(c => true, null, new[] { Arg.Number("test", 1) })
                                                        );
     });
 }
Пример #5
0
            public void Should_SetRequired_BeAdded()
            {
                var validator = MemberValidatorCreator.Create <object, object>(b => b
                                                                               .SetRequired("message")
                                                                               );

                Assert.Equal("message", validator.RequiredError.Message);
                Assert.Null(validator.RequiredError.Arguments);
            }
Пример #6
0
 public void Should_ThrowException_When_DuplicateSetOptional()
 {
     Assert.Throws <InvalidCommandDuplicationException>(() =>
     {
         MemberValidatorCreator.Create <object, IEnumerable <MemberClass> >(b => b
                                                                            .SetOptional().SetOptional()
                                                                            );
     });
 }
Пример #7
0
 public void Should_ThrowException_When_AsRelative_And_NullPredicate()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         MemberValidatorCreator.Create <object, object>(b => b
                                                        .AsRelative(null)
                                                        );
     });
 }
Пример #8
0
 public void Should_ThrowException_When_AsNullable_And_NullMemberSpecification()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         MemberValidatorCreator.Create <object, int?>(b => b
                                                      .AsNullable(null)
                                                      );
     });
 }
Пример #9
0
        public void Should_HaveEmptyInitialValues()
        {
            var memberValidator = MemberValidatorCreator.Create <object, object>(b => b);

            Assert.False(memberValidator.IsOptional);
            Assert.Null(memberValidator.RequiredError);
            Assert.Null(memberValidator.SingleError);
            Assert.Empty(memberValidator.Rules);
        }
Пример #10
0
 public void Should_ThrowException_When_SingleError_Added_MultipleTimes()
 {
     Assert.Throws <InvalidCommandDuplicationException>(() =>
     {
         MemberValidatorCreator.Create <object, object>(b => b
                                                        .SetSingleError("message1")
                                                        .SetSingleError("message2"));
     });
 }
Пример #11
0
 public void Should_ThrowException_When_WithMessage_AtTheBeginning()
 {
     Assert.Throws <InvalidCommandOrderException>(() =>
     {
         MemberValidatorCreator.Create <object, object>(b => b
                                                        .WithMessage("message")
                                                        );
     });
 }
Пример #12
0
            public void Should_WithMessage_SetRuleSingleError_In_Valid()
            {
                var validator = MemberValidatorCreator.Create <object, MemberClass>(b => b
                                                                                    .Valid(x => false)
                                                                                    .WithMessage("message"),
                                                                                    out var commands
                                                                                    );

                AssertSingleRuleSet(validator, commands);
            }
Пример #13
0
            public void Should_WithMessage_SetRuleSingleError_In_AsModelsCollection()
            {
                var validator = MemberValidatorCreator.Create <object, IEnumerable <MemberClass> >(b => b
                                                                                                   .AsModelsCollection <object, IEnumerable <MemberClass>, MemberClass>(x => x)
                                                                                                   .WithMessage("message"),
                                                                                                   out var commands
                                                                                                   );

                AssertSingleRuleSet(validator, commands);
            }
Пример #14
0
            public void Should_AddAsModelRule_WithoutSpecification_When_AsModel_And_NullSpecification()
            {
                var validator = MemberValidatorCreator.Create <object, MemberClass>(b => b
                                                                                    .AsModel()
                                                                                    );

                Assert.IsType <AsModelRule <MemberClass> >(validator.Rules.Single());

                var asModelRule = (AsModelRule <MemberClass>)validator.Rules.Single();

                Assert.Null(asModelRule.Specification);
            }
Пример #15
0
            public void Should_WithMessage_ApplyLastMessage_When_MultipleInRow()
            {
                var validator = MemberValidatorCreator.Create <object, MemberClass>(b => b
                                                                                    .Valid(x => false)
                                                                                    .WithMessage("message3")
                                                                                    .WithMessage("message2")
                                                                                    .WithMessage("message1")
                                                                                    .WithMessage("message"),
                                                                                    out var commands
                                                                                    );

                AssertSingleRuleSet(validator, commands, 5);
            }
Пример #16
0
            public void Should_AddAsCollectionRule_When_AsCollection()
            {
                MemberSpecification <object, MemberClass> itemSpecification = c => c;

                var validator = MemberValidatorCreator.Create <object, IEnumerable <MemberClass> >(b => b
                                                                                                   .AsCollection <object, IEnumerable <MemberClass>, MemberClass>(itemSpecification)
                                                                                                   );

                Assert.IsType <AsCollectionRule <object, MemberClass> >(validator.Rules.Single());

                var validCollectionRule = (AsCollectionRule <object, MemberClass>)validator.Rules.Single();

                Assert.Equal(itemSpecification, validCollectionRule.ItemSpecification);
            }
Пример #17
0
            public void Should_AddAsModelRule_WithSpecification_When_AsModel()
            {
                Specification <MemberClass> specification = c => c;

                var validator = MemberValidatorCreator.Create <object, MemberClass>(b => b
                                                                                    .AsModel(specification)
                                                                                    );

                Assert.IsType <AsModelRule <MemberClass> >(validator.Rules.Single());

                var asModelRule = (AsModelRule <MemberClass>)validator.Rules.Single();

                Assert.Same(specification, asModelRule.Specification);
            }
Пример #18
0
            public void Should_AddAsNullableRule_When_AsNullable()
            {
                MemberSpecification <object, int> memberSpecification = c => c;

                var validator = MemberValidatorCreator.Create <object, int?>(b => b
                                                                             .AsNullable(memberSpecification)
                                                                             );

                Assert.IsType <AsNullableRule <object, int> >(validator.Rules.Single());

                var asNullableRule = (AsNullableRule <object, int>)validator.Rules.Single();

                Assert.Same(memberSpecification, asNullableRule.MemberSpecification);
            }
Пример #19
0
            public void Should_AddAsRelativeRule()
            {
                Predicate <object> isValid = c => true;

                var validator = MemberValidatorCreator.Create <object, object>(b => b
                                                                               .AsRelative(isValid)
                                                                               );

                Assert.IsType <AsRelativeRule <object> >(validator.Rules.Single());

                var memberRule = (AsRelativeRule <object>)validator.Rules.Single();

                Assert.Same(isValid, memberRule.IsValid);
                Assert.Null(memberRule.Error);
            }
Пример #20
0
        public static Exception TestMemberRuleException <TMember>(MemberSpecification <object, TMember> specification, Type expectedException)
        {
            try
            {
                MemberValidatorCreator.Create(specification);
            }
            catch (Exception exception)
            {
                if (!expectedException.IsInstanceOfType(exception))
                {
                    throw new TesterException($"Invalid exception (`{exception.GetType().FullName}`) thrown when `{expectedException.FullName}` was expected");
                }

                return(exception);
            }

            throw new TesterException($"No exception thrown when `{expectedException.FullName}` was expected");
        }
Пример #21
0
        public void Should_OutputCommands()
        {
            MemberValidatorCreator.Create <object, MemberClass>(b => b
                                                                .SetOptional()
                                                                .SetRequired("required")
                                                                .Valid(x => false)
                                                                .WithMessage("message")
                                                                .AsRelative(x => true)
                                                                .AsModel()
                                                                .SetSingleError("single")
                                                                , out var commands);

            Assert.Equal(7, commands.Count);
            Assert.IsType <SetOptionalCommand>(commands.ElementAt(0));
            Assert.IsType <SetRequiredCommand>(commands.ElementAt(1));
            Assert.IsType <ValidRule <MemberClass> >(commands.ElementAt(2));
            Assert.IsType <WithMessageCommand>(commands.ElementAt(3));
            Assert.IsType <AsRelativeRule <object> >(commands.ElementAt(4));
            Assert.IsType <AsModelRule <MemberClass> >(commands.ElementAt(5));
            Assert.IsType <SetSingleErrorCommand>(commands.ElementAt(6));
        }
Пример #22
0
            public void Should_AddAsCollectionRule_When_AsModelsCollection(Specification <MemberClass> specification)
            {
                var validator = MemberValidatorCreator.Create <object, IEnumerable <MemberClass> >(b => b
                                                                                                   .AsModelsCollection <object, IEnumerable <MemberClass>, MemberClass>(specification)
                                                                                                   );

                Assert.IsType <AsCollectionRule <object, MemberClass> >(validator.Rules.Single());

                var validCollectionRule = (AsCollectionRule <object, MemberClass>)validator.Rules.Single();

                Assert.NotNull(validCollectionRule.ItemSpecification);

                var itemValidator = MemberValidatorCreator.Create(validCollectionRule.ItemSpecification);

                Assert.IsType <AsModelRule <MemberClass> >(itemValidator.Rules.Single());
                Assert.False(itemValidator.IsOptional);

                var asModelRule = (AsModelRule <MemberClass>)itemValidator.Rules.Single();

                Assert.Same(specification, asModelRule.Specification);
            }
Пример #23
0
        public MemberScope(PropertyInfo memberPropertyInfo, MemberSpecification <TModel, TMember> memberSpecification)
        {
            MemberPropertyInfo = memberPropertyInfo ?? throw new ArgumentNullException(nameof(memberPropertyInfo));
            MemberValidator    = MemberValidatorCreator.Create(memberSpecification ?? (m => m));
            Name = memberPropertyInfo.Name;

            _getErrors = (executableRule, model, executionContext, validationStrategy, depth) =>
            {
                var memberValue = model != null
                    ? (TMember)executableRule.MemberPropertyInfo.GetValue(model)
                    : default;

                return(ValidatorExecutor.ExecuteMember(
                           executableRule.MemberValidator,
                           model,
                           memberValue,
                           executionContext,
                           validationStrategy,
                           depth
                           ));
            };
        }
Пример #24
0
            public void Should_AddValidRule_WithParameters(string message, IMessageArg[] args)
            {
                Predicate <object> isValid = c => true;

                var validator = MemberValidatorCreator.Create <object, object>(b => b
                                                                               .Valid(isValid, message, args)
                                                                               );

                Assert.IsType <ValidRule <object> >(validator.Rules.Single());

                var memberRule = (ValidRule <object>)validator.Rules.Single();

                Assert.Same(isValid, memberRule.IsValid);

                if (message != null)
                {
                    Assert.Equal(message, memberRule.Error.Message);
                    Assert.Same(args, memberRule.Error.Arguments);
                }
                else
                {
                    Assert.Null(memberRule.Error);
                }
            }
Пример #25
0
            public void Should_RequiredError_NotBeAdded_When_NoMethod()
            {
                var validator = MemberValidatorCreator.Create <object, object>(b => b);

                Assert.Null(validator.RequiredError);
            }
Пример #26
0
        public static void TestSingleMemberRule <TMember>(MemberSpecification <object, TMember> specification, TMember member, bool expectedIsValid, string expectedErrorMessage = null, IReadOnlyCollection <IMessageArg> expectedArgs = null)
        {
            var validator = MemberValidatorCreator.Create(specification);

            var rule = validator.Rules.Single();

            var executionContext = new ExecutionContextStub();

            IErrorsCollection errorsCollection;

            if (rule is ValidRule <TMember> validateRule)
            {
                validateRule.TryGetErrors(member, executionContext, ValidationStrategy.Complete, out errorsCollection);
            }
            else if (rule is AsNullableRule validateNullableRule)
            {
                validateNullableRule.TryGetErrors(new object(), member, executionContext, ValidationStrategy.Complete, out errorsCollection);
            }
            else
            {
                throw new UntestableException($"Only Valid and AsNullable can be tested in {nameof(TestSingleMemberRule)}");
            }

            if (expectedIsValid)
            {
                if (!errorsCollection.IsEmpty)
                {
                    throw new TesterException("Error collection isn't empty, but no error is expected");
                }
            }
            else
            {
                if (errorsCollection.IsEmpty)
                {
                    throw new TesterException("Error collection is empty, but error is expected");
                }

                if ((errorsCollection.Errors.Count != 1) || (errorsCollection.Members.Count > 0))
                {
                    throw new TesterException("Only one error is expected");
                }

                var error = errorsCollection.Errors.Single();

                if (expectedErrorMessage != null)
                {
                    if (!string.Equals(error.Message, expectedErrorMessage))
                    {
                        throw new TesterException($"Message `{error.Message}` is not as expected: `{expectedErrorMessage}`");
                    }
                }

                if (expectedArgs != null)
                {
                    if (error.Arguments == null)
                    {
                        throw new TesterException($"No arguments while expected amount is `{expectedArgs.Count}`");
                    }

                    if (error.Arguments.Count != expectedArgs.Count)
                    {
                        throw new TesterException($"Arguments count `{error.Arguments.Count}` is not as expected `{expectedArgs.Count}`");
                    }

                    for (var i = 0; i < expectedArgs.Count; ++i)
                    {
                        var expected = expectedArgs.ElementAt(i);

                        var actual = error.Arguments.ElementAt(i);

                        if (!string.Equals(expected.Name, actual.Name))
                        {
                            throw new TesterException($"Argument at position `{i}` - name `{actual.Name}` is not as expected `{expected.Name}`");
                        }

                        var actualType   = actual.GetType();
                        var expectedType = expected.GetType();

                        if (!expectedType.IsAssignableFrom(actualType))
                        {
                            throw new TesterException($"Argument at position `{i}` - type `{actualType.FullName}` cannot be used as `{expectedType.FullName}`");
                        }

                        var actualValue   = actualType.GetProperties().Single(p => p.Name == "Value").GetValue(actual);
                        var expectedValue = actualType.GetProperties().Single(p => p.Name == "Value").GetValue(expected);

                        if (actualValue is double d)
                        {
                            if (Math.Abs(d - (double)expectedValue) > 0.0000001d)
                            {
                                throw new TesterException($"Argument at position `{i}` - value (double) `{actualValue}` is not as expected `{expectedValue}`");
                            }
                        }
                        else if (!expectedValue.Equals(actualValue))
                        {
                            throw new TesterException($"Argument at position `{i}` - value `{actualValue}` is not as expected `{expectedValue}`");
                        }
                    }
                }
                else if (error.Arguments != null)
                {
                    throw new TesterException("Arguments are not expected in the error");
                }
            }
        }