public void SuppliesAppropriateParametersToMessageTemplate()
        {
            TypeConversionValidator validator = new TypeConversionValidator(typeof(int));

            validator.MessageTemplate = "{0}-{1}-{2}-{3}";
            validator.Tag             = "tag";
            object target = "not an int";
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match            match            = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);

            Assert.IsTrue(match.Success);
            Assert.IsTrue(match.Groups["param0"].Success);
            Assert.AreEqual(target.ToString(), match.Groups["param0"].Value);
            Assert.IsTrue(match.Groups["param1"].Success);
            Assert.AreEqual(key, match.Groups["param1"].Value);
            Assert.IsTrue(match.Groups["param2"].Success);
            Assert.AreEqual(validator.Tag, match.Groups["param2"].Value);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("System.Int32", match.Groups["param3"].Value);
        }
Esempio n. 2
0
        public void SuppliesAppropriateParametersToDefaultNegatedMessage()
        {
            TypeConversionValidator validator = new TypeConversionValidator(typeof(int), true);

            validator.Tag = "tag";
            object target = "10";
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];

            System.Text.RegularExpressions.Match match = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);
            Assert.IsTrue(match.Success);
            Assert.IsFalse(match.Groups["param0"].Success);
            Assert.IsFalse(match.Groups["param1"].Success);
            Assert.IsFalse(match.Groups["param2"].Success);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("System.Int32", match.Groups["param3"].Value);
        }
        public void SuppliesAppropriateParametersToDefaultNegatedMessage()
        {
            TypeConversionValidator validator = new TypeConversionValidator(typeof(int), true);
            validator.Tag = "tag";
            object target = "10";
            string key = "key";

            ValidationResults validationResults = new ValidationResults();
            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match match = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);
            Assert.IsTrue(match.Success);
            Assert.IsFalse(match.Groups["param0"].Success);
            Assert.IsFalse(match.Groups["param1"].Success);
            Assert.IsFalse(match.Groups["param2"].Success);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("System.Int32", match.Groups["param3"].Value);
        }