Beispiel #1
0
        public void Should_HaveEmptyInitialValues()
        {
            var validator = ValidatorCreator.Create <MemberClass>(b => b);

            Assert.Null(validator.SingleError);
            Assert.Empty(validator.Scopes);
        }
Beispiel #2
0
            public void Should_AddMemberScope_When_Member()
            {
                Predicate <string> isValid = c => true;
                var args = new[] { Arg.Text("test", "test123") };

                MemberSpecification <MemberClass, string> memberSpecification = c => c.Valid(isValid, "message", args);

                var validator = ValidatorCreator.Create <MemberClass>(b => b
                                                                      .Member(m => m.MemberField, memberSpecification)
                                                                      );

                Assert.IsType <MemberScope <MemberClass, string> >(validator.Scopes.Single());

                var memberScope = (MemberScope <MemberClass, string>)validator.Scopes.Single();

                Assert.Null(memberScope.RuleSingleError);

                Assert.Equal("MemberField", memberScope.MemberPropertyInfo.Name);
                Assert.Equal(typeof(string), memberScope.MemberPropertyInfo.PropertyType);

                Assert.IsType <ValidRule <string> >(memberScope.MemberValidator.Rules.Single());

                var memberRule = (ValidRule <string>)memberScope.MemberValidator.Rules.Single();

                Assert.Same(isValid, memberRule.IsValid);
                Assert.Equal("message", memberRule.Error.Message);
                Assert.Same(args, memberRule.Error.Arguments);
                Assert.Null(memberRule.RuleSingleError);
            }
Beispiel #3
0
 public void Should_ThrowException_When_SingleError_Added_With_NullMessage()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         ValidatorCreator.Create <MemberClass>(b => b
                                               .SetSingleError(null));
     });
 }
Beispiel #4
0
 public void Should_ThrowException_When_SingleError_Added_MultipleTimes()
 {
     Assert.Throws <InvalidCommandDuplicationException>(() =>
     {
         ValidatorCreator.Create <MemberClass>(b => b
                                               .SetSingleError("message1")
                                               .SetSingleError("message2"));
     });
 }
Beispiel #5
0
 public void Should_ThrowException_When_Valid_And_NullPredicate()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         ValidatorCreator.Create <MemberClass>(b => b
                                               .Valid(null)
                                               );
     });
 }
Beispiel #6
0
 public void Should_ThrowException_When_WithMessage_AtTheBeginning()
 {
     Assert.Throws <InvalidCommandOrderException>(() =>
     {
         ValidatorCreator.Create <MemberClass>(b => b
                                               .WithMessage("message")
                                               );
     });
 }
Beispiel #7
0
            public void Should_SetSingleError_BeAdded()
            {
                var validator = ValidatorCreator.Create <MemberClass>(b => b
                                                                      .SetSingleError("message")
                                                                      );

                Assert.Equal("message", validator.SingleError.Message);
                Assert.Null(validator.SingleError.Arguments);
            }
Beispiel #8
0
 public void Should_ThrowException_When_Member_And_NullMemberSelector()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         ValidatorCreator.Create <MemberClass>(b => b
                                               .Member <string>(null, c => c)
                                               );
     });
 }
Beispiel #9
0
            public void Should_WithMessage_SetRuleSingleError_In_ModelScope()
            {
                var validator = ValidatorCreator.Create <MemberClass>(b => b
                                                                      .Valid(x => false)
                                                                      .WithMessage("message"),
                                                                      out var commands
                                                                      );

                AssertSingleRuleSet(validator, commands);
            }
Beispiel #10
0
            public void Should_WithMessage_SetRuleSingleError_In_MemberScope()
            {
                var validator = ValidatorCreator.Create <MemberClass>(b => b
                                                                      .Member(m => m.MemberField)
                                                                      .WithMessage("message"),
                                                                      out var commands
                                                                      );

                AssertSingleRuleSet(validator, commands);
            }
Beispiel #11
0
            public void Should_WithMessage_ApplyLastMessage_When_MultipleInRow()
            {
                var validator = ValidatorCreator.Create <MemberClass>(b => b
                                                                      .Member(m => m.MemberField)
                                                                      .WithMessage("message3")
                                                                      .WithMessage("message2")
                                                                      .WithMessage("message1")
                                                                      .WithMessage("message"),
                                                                      out var commands
                                                                      );

                AssertSingleRuleSet(validator, commands, 5);
            }
Beispiel #12
0
        public void Should_OutputCommands()
        {
            ValidatorCreator.Create <MemberClass>(b => b
                                                  .Valid(m => true)
                                                  .WithMessage("message")
                                                  .SetSingleError("single")
                                                  .Member(m => m.MemberField)
                                                  , out var commands);

            Assert.Equal(4, commands.Count);
            Assert.IsType <ModelScope <MemberClass> >(commands.ElementAt(0));
            Assert.IsType <WithMessageCommand>(commands.ElementAt(1));
            Assert.IsType <SetSingleErrorCommand>(commands.ElementAt(2));
            Assert.IsType <MemberScope <MemberClass, string> >(commands.ElementAt(3));
        }
Beispiel #13
0
            public void Should_AddModelScope_When_Valid()
            {
                Predicate <MemberClass> isValid = c => true;

                var validator = ValidatorCreator.Create <MemberClass>(b => b
                                                                      .Valid(isValid)
                                                                      );

                Assert.IsType <ModelScope <MemberClass> >(validator.Scopes.Single());

                var modelScope = (ModelScope <MemberClass>)validator.Scopes.Single();

                Assert.Same(isValid, modelScope.Rule.IsValid);
                Assert.Null(modelScope.RuleSingleError);
                Assert.Null(modelScope.Rule.Error);
            }
Beispiel #14
0
            public void Should_AddMemberScope_When_Member_WithoutSpecification()
            {
                var validator = ValidatorCreator.Create <MemberClass>(b => b
                                                                      .Member(m => m.MemberField)
                                                                      );

                Assert.IsType <MemberScope <MemberClass, string> >(validator.Scopes.Single());

                var memberScope = (MemberScope <MemberClass, string>)validator.Scopes.Single();

                Assert.Null(memberScope.RuleSingleError);

                Assert.Equal("MemberField", memberScope.MemberPropertyInfo.Name);
                Assert.Equal(typeof(string), memberScope.MemberPropertyInfo.PropertyType);

                Assert.False(memberScope.MemberValidator.IsOptional);
                Assert.Null(memberScope.MemberValidator.RequiredError);
                Assert.Empty(memberScope.MemberValidator.Rules);
            }
        /// <summary>
        /// Recupera o criador de validadores.
        /// </summary>
        /// <param name="validationType">Tipo de validação.</param>
        /// <param name="culture"></param>
        /// <returns></returns>
        private IValidatorCreator GetValidatorCreator(ValidationType validationType, System.Globalization.CultureInfo culture)
        {
            IValidatorCreator creator = null;
            bool found = false;

            lock (_validatorCreators)
                found = _validatorCreators.TryGetValue(validationType.ValidationTypeId, out creator);
            if (!found)
            {
                System.Reflection.Assembly assembly = null;
                if (Reflection.AssemblyLoader.Instance.TryGet(validationType.Type.AssemblyName.Name, out assembly))
                {
                    Type validatorType = null;
                    try
                    {
                        validatorType = assembly.GetType(validationType.Type.FullName, true, false);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ResourceMessageFormatter.Create(() => Properties.Resources.GeneralValidationManager_ValidationTypeGetTypeError, validationType.Type.FullName, validationType.Name, Diagnostics.ExceptionFormatter.FormatException(ex, true)), ex);
                    }
                    if (validationType != null)
                    {
                        creator = ValidatorCreator.CreateCreator(validatorType, culture);
                        lock (_validatorCreators)
                            if (!_validatorCreators.ContainsKey(validationType.ValidationTypeId))
                            {
                                _validatorCreators.Add(validationType.ValidationTypeId, creator);
                            }
                    }
                }
                else
                {
                    _logger.Error(ResourceMessageFormatter.Create(() => Properties.Resources.GeneralValidationManager_ValidationTypeAssemblyNotFound, validationType.Type.AssemblyName.Name, validationType.Name));
                }
            }
            return(creator);
        }
Beispiel #16
0
 public void Should_ThrowException_When_ReferenceChanged()
 {
     Assert.Throws <InvalidProcessedReferenceException>(() => { ValidatorCreator.Create <MemberClass>(b => { return(new SpecificationBuilder <MemberClass>()); }); });
 }
Beispiel #17
0
        public static Validator GetValidator(JsonValidationRule rule)
        {
            ValidatorCreator creator = (ValidatorCreator)Validators[rule.ValidationType];

            return((creator != null) ? creator(rule) : null);
        }
Beispiel #18
0
            public void Should_SetSingleError_NotBeAdded_When_NoMethod()
            {
                var validator = ValidatorCreator.Create <MemberClass>(b => b);

                Assert.Null(validator.SingleError);
            }