Пример #1
0
        public void ItShouldConstruct()
        {
            //Arrange
            Mock <IValidationRuleProvider <int> > mockRuleProvider = new Mock <IValidationRuleProvider <int> >();

            //Act
            DigitsValidator sut = new DigitsValidator(mockRuleProvider.Object);

            //Assert
            Assert.NotNull(sut);
        }
Пример #2
0
        public void ItShouldAcceptValidNumeral(int input)
        {
            //Arrange
            Mock <IValidationRuleProvider <int> > mockRuleProvider = new Mock <IValidationRuleProvider <int> >();
            DigitsValidator sut = new DigitsValidator(mockRuleProvider.Object);

            //Act
            bool result = sut.Validate(input);

            //Assert
            Assert.True(result);
        }
        public void IsValid()
        {
            DigitsValidator v = new DigitsValidator();
            v.Initialize(new DigitsAttribute(3));
            Assert.IsTrue(v.IsValid(0, null));
            Assert.IsTrue(v.IsValid(9, null));
            Assert.IsTrue(v.IsValid(99, null));
            Assert.IsTrue(v.IsValid(99.0, null));
            Assert.IsTrue(v.IsValid(null, null));
            Assert.IsTrue(v.IsValid("22", null));
            Assert.IsTrue(v.IsValid(103, null));
            Assert.IsTrue(v.IsValid(01, null));

            Assert.IsFalse(v.IsValid(1000, null));
            Assert.IsFalse(v.IsValid(10.1, null));
            Assert.IsFalse(v.IsValid(new object(), null));
            Assert.IsFalse(v.IsValid("aa.bb", null));

            v.Initialize(new DigitsAttribute(3, 2));
            Assert.IsTrue(v.IsValid(0, null));
            Assert.IsTrue(v.IsValid(1, null));
            Assert.IsTrue(v.IsValid(100.100, null));
            Assert.IsTrue(v.IsValid(99.99, null));

            Assert.IsFalse(v.IsValid(1000.0, null));
            Assert.IsFalse(v.IsValid(9.233, null));
            Assert.IsFalse(v.IsValid("1233", null));

            v.Initialize(new DigitsAttribute(0, 2));
            Assert.IsTrue(v.IsValid(0, null));
            Assert.IsTrue(v.IsValid(0.12, null));
            Assert.IsTrue(v.IsValid(0.1, null));
            Assert.IsTrue(v.IsValid(0.00000000000, null));

            Assert.IsFalse(v.IsValid(1.12, null));
            Assert.IsFalse(v.IsValid(0.123, null));
        }
Пример #4
0
        public void ItShouldReturnTrueIfAllValidationSucceeds(
            bool expected,
            int input)
        {
            //Arrange

            Mock <IValidationRule <int> > mockRule = new Mock <IValidationRule <int> >();

            mockRule.Setup(mockRule => mockRule.IsSatisfiedBy(input))
            .Returns(expected);

            Mock <IValidationRuleProvider <int> > mockRuleProvider = new Mock <IValidationRuleProvider <int> >();

            mockRuleProvider.Setup(mockRuleProvider => mockRuleProvider.GetRules())
            .Returns(new IValidationRule <int>[] { mockRule.Object });

            DigitsValidator sut = new DigitsValidator(mockRuleProvider.Object);

            //Act
            bool result = sut.Validate(input);

            //Assert
            Assert.True(result == expected);
        }
        public string ValidationMsg()
        {
            // If we have no text to validate...
            if (String.IsNullOrEmpty(_attachedTextControl.Text.Trim()))
            {
                if (_required)
                {
                    return("Please enter a value into " + _nomenclature);
                }
                return(String.Empty);
            }
            // If we got here then there is text to validate
            if (_attachedTextControl.Text == _originalText)
            {
                // Assume the original text is in the proper validation style
                if (_validationStyle == ValidationStyle.NoOriginalValue)
                {
                    return("Please enter a different value into " + _nomenclature);
                }
                return(String.Empty);
            }
            // Okay, we have something to validate and its not the original value...
            string        returnMsg = String.Empty;
            TextValidator validator;
            string        errSuffix = " into " + _nomenclature;

            switch (_validationStyle)
            {
            case ValidationStyle.NoValidation:
                returnMsg = String.Empty;
                break;

            case ValidationStyle.DateAny:
                validator = new DateValidator(DateStyle.DontCare);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.DateFuture:
                validator = new DateValidator(DateStyle.Future);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.DatePast:
                validator = new DateValidator(DateStyle.Past);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.DigitsOnly:
                validator = new DigitsValidator(DigitStyle.DigitsOnly);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.DigitsNotZero:
                validator = new DigitsValidator(DigitStyle.DigitsNotZero);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.Money:
                validator = new DigitsValidator(DigitStyle.Money);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.MoneyNotZero:
                validator = new DigitsValidator(DigitStyle.MoneyNotZero);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.Percentage:
                validator = new DigitsValidator(DigitStyle.Percentage);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.PercentageNotZero:
                validator = new DigitsValidator(DigitStyle.PercentageNotZero);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.PhoneNumber:
                validator = new PhoneNumberValidator();
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.EmailAddr:
                validator = new PureTextValidator(PureTextStyle.EmailAddress);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.StateAbbreviation:
                validator = new PureTextValidator(PureTextStyle.StateAbbreviation);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.NoWhiteSpace:
                validator = new PureTextValidator(PureTextStyle.NoWhiteSpace);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.NoPunctuation:
                validator = new PureTextValidator(PureTextStyle.NoPunctuation);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.NoWhiteSpaceAndNoPunct:
                validator = new PureTextValidator(PureTextStyle.NoWhiteSpaceAndNoPunct);
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.SSN:
                validator = new SSNValidator();
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.Time:
                validator = new TimeValidator();
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;

            case ValidationStyle.ZipPlus4:
                validator = new ZipCodeValidator();
                returnMsg = validator.Validate(_attachedTextControl.Text);
                if (returnMsg.Length > 0)
                {
                    returnMsg = returnMsg + errSuffix;
                }
                break;
            }

            return(returnMsg);
        }