Esempio n. 1
0
        public void Boolean_Validator_Test()
        {
            var sut = new BooleanValidator();

            var col1 = new ColumnTypeInfo()
            {
                AllowNull = true
            };
            var col2 = new ColumnTypeInfo()
            {
                AllowNull = false
            };

            Assert_Valid(sut.Validate(col1, "true"));
            Assert_Valid(sut.Validate(col1, "TRUE"));
            Assert_Valid(sut.Validate(col1, "True"));
            Assert_Valid(sut.Validate(col1, "false"));
            Assert_Valid(sut.Validate(col1, "FALSE"));
            Assert_Valid(sut.Validate(col1, "False"));

            Assert_Invalid(sut.Validate(col1, "Foo"));

            Assert_Invalid_Replaced(sut.Validate(col2, "Foo"), false);
            Assert_Invalid_Replaced(sut.Validate(col2, DBNull.Value), false);
        }
Esempio n. 2
0
        public void BooleanValidator_IsValid_NullPropertyName()
        {
            BooleanValidator validator = new BooleanValidator(false);
            string           errorMessage;

            Assert.Throws <ArgumentException>(() => validator.IsValid(true, null, this, out errorMessage));
            Assert.Throws <ArgumentException>(() => validator.IsValid(true, String.Empty, this, out errorMessage));
        }
Esempio n. 3
0
        public void BooleanValidator_Ctor()
        {
            BooleanValidator validator = new BooleanValidator(false);

            Assert.False(validator.RequiredValue);

            validator = new BooleanValidator(true);
            Assert.True(validator.RequiredValue);
        }
Esempio n. 4
0
        public void BooleanValidator_IsValid()
        {
            BooleanValidator validator = new BooleanValidator(false);
            bool             result    = validator.IsValid(true, "test", this, out string errorMessage);

            Assert.False(result);
            Assert.NotNull(errorMessage);

            result = validator.IsValid("false", "test", this, out errorMessage);
            Assert.False(result);
            Assert.NotNull(errorMessage);

            result = validator.IsValid(false, "test", this, out errorMessage);
            Assert.True(result);
            Assert.Null(errorMessage);
        }
        public void TestTwoEqualInstancesHaveSameHashPasses(BooleanValidator validatorBase, ValidatorLinkage <BooleanValidator> firstLinkage, ValidatorLinkage <BooleanValidator> secondLinkage, bool areEqual)
        {
            "Given a new ValidatorBase instance"
            .x(() => validatorBase = Validate.That("test1", true));

            "And given a first ValidatorBase linkage instance"
            .x(() => firstLinkage = new ValidatorLinkage <BooleanValidator>(validatorBase));

            "And given a secondLinkage ValidatorBase linkage instance"
            .x(() => secondLinkage = new ValidatorLinkage <BooleanValidator>(validatorBase));

            "Testing that both linkage's hashes are equal"
            .x(() => { areEqual = firstLinkage.GetHashCode() == secondLinkage.GetHashCode(); });

            "Should evaluate true"
            .x(() => areEqual.Should().BeTrue());
        }
Esempio n. 6
0
 public static BooleanValidator That(string nameOfParameter, bool parameterValue)
 {
     return(BooleanValidator.GetInstance(nameOfParameter, parameterValue));
 }
        public void Valid_values_for_custom_validator(string value)
        {
            var validator = new BooleanValidator(new[] { "1", "Yes", "T" }, new[] { "0", "No", "F" });

            Should.NotThrow(() => validator.Validate(value));
        }
        public void Invalid_values_for_default_case_sensitive_validator(string value)
        {
            var validator = new BooleanValidator(caseSensitive: true);

            Should.Throw <ValidationException>(() => validator.Validate(value));
        }
        public void TestParameterIsFalseFails(string parameterName, bool parameterValue, BooleanValidator validatorBase, Action act)
        {
            "Given a new ValidatorBase"
            .x(() => validatorBase = Validate.That(parameterName, parameterValue));

            "Testing that the parameter is false"
            .x(() => act = () => validatorBase.IsFalse().OtherwiseThrowException());

            "Should throw an exception"
            .x(() => act.ShouldThrow <ArgumentException>()
               .WithMessage(string.Format(Resources.MustBeFalse + "\r\nParameter name: {0}", parameterName)));
        }
        public void Invalid_values_for_default_validator(string value)
        {
            var validator = new BooleanValidator();

            Should.Throw <ValidationException>(() => validator.Validate(value));
        }
        public void Valid_values_for_default_validator(string value)
        {
            var validator = new BooleanValidator();

            Should.NotThrow(() => validator.Validate(value));
        }
        public void TestParameterEqualToWithCustomExceptionPasses(string parameterName, bool parameterValue, bool valueToCompare, BooleanValidator validatorBase, Action act)
        {
            "Given a new ValidatorBase"
            .x(() => validatorBase = Validate.That(parameterName, parameterValue));

            "Testing that the parameter value is equal to the value to compare against and throwing a custom exception if not"
            .x(() => validatorBase.IsEqualTo(valueToCompare).OtherwiseThrow(new ArgumentException("Some nonsense message", parameterName)));

            "Should not result in an exception"
            .x(() => validatorBase.CurrentException.Should().BeNull());
        }
        public void TestParameterEqualToWithCustomExceptionFails(string parameterName, bool parameterValue, bool valueToCompare, BooleanValidator validatorBase, Action act)
        {
            "Given a new ValidatorBase"
            .x(() => validatorBase = Validate.That(parameterName, parameterValue));

            "Testing that the parameter value is equal to the value to compare against and throwing a custom exception if not"
            .x(() => act = () => validatorBase.IsEqualTo(valueToCompare).OtherwiseThrow(new ArgumentException("Some nonsense message", parameterName)));

            "Should result in that custom exception being thrown"
            .x(() => act.ShouldThrow <ArgumentException>()
               .WithMessage(string.Format("Some nonsense message\r\nParameter name: {0}", parameterName)));
        }
        public void TestParameterEqualToFails(string parameterName, bool parameterValue, bool valueToCompare, BooleanValidator validatorBase, Action act)
        {
            "Given a new ValidatorBase"
            .x(() => validatorBase = Validate.That(parameterName, parameterValue));

            "Testing that the parameter value is equal to the value to compare against"
            .x(() => act = () => validatorBase.IsEqualTo(valueToCompare).OtherwiseThrowException());

            "Should throw an exception"
            .x(() => act.ShouldThrow <ArgumentEqualityException>()
               .WithMessage(string.Format(Resources.MustBeEqualToX + "\r\nParameter name: {1}", valueToCompare, parameterName)));
        }
        public void TestParameterIsFalsePasses(string parameterName, bool parameterValue, BooleanValidator validatorBase)
        {
            "Given a new ValidatorBase"
            .x(() => validatorBase = Validate.That(parameterName, parameterValue));

            "Testing that the parameter is false"
            .x(() => validatorBase.IsFalse().OtherwiseThrowException());

            "Should not result in an exception"
            .x(() => validatorBase.CurrentException.Should().BeNull());
        }
        public void Valid_values_for_default_case_sensitive_validator(string value)
        {
            var validator = new BooleanValidator(caseSensitive: true);

            Should.NotThrow(() => validator.Validate(value));
        }
        public void TestParameterNotEqualToPasses(string parameterName, bool parameterValue, bool valueToCompare, BooleanValidator validatorBase)
        {
            "Given a new ValidatorBase"
            .x(() => validatorBase = Validate.That(parameterName, parameterValue));

            "Testing that the parameter value is equal to the value to compare against"
            .x(() => validatorBase.IsNotEqualTo(valueToCompare).OtherwiseThrowException());

            "Should not result in an exception"
            .x(() => validatorBase.CurrentException.Should().BeNull());
        }