public void InvalidObjectWhenEmpty()
        {
            var validator = new Validator();

            var invalidObject = new RequiredObjectWithGenericMessage()
            {
                Name = string.Empty
            };
            Assert.IsFalse(validator.IsValid(invalidObject));
        }
        public void CanRetriveGenericValiationResults()
        {
            var validator = new Validator();

            var invalidObject = new RequiredObjectWithGenericMessage();
            ICollection<IValidationResult> results = validator.ValidationResultsFor(invalidObject);

            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("Name", results.First().PropertyName);
            Assert.AreEqual(typeof(RequiredObjectWithGenericMessage), results.First().ClassContext);
            Assert.AreEqual("may not be null or empty", results.First().Message);
        }
Exemple #3
0
        public void CanRetriveValiationResults()
        {
            var validator = new Validator();

            var invalidObject = new SomeObject();
            ICollection<IValidationResult> results = validator.ValidationResultsFor(invalidObject);

            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("Name", results.First().PropertyName);
            Assert.AreEqual(typeof (SomeObject), results.First().ClassContext);
            Assert.AreEqual("Dude...the name please!!", results.First().Message);
        }
Exemple #4
0
        public void CanValidateCompleteObject()
        {
            var validator = new Validator();

            var invalidObject = CreateValidCompleteObject();
            invalidObject.RequiredValidatorString = null;

            Assert.IsFalse(validator.IsValid(invalidObject));

            var validObject = CreateValidCompleteObject();

            Assert.IsTrue(validator.IsValid(validObject));
        }
Exemple #5
0
        public void CanValidateObject()
        {
            var validator = new Validator();

            var invalidObject = new SomeObject();
            Assert.IsFalse(validator.IsValid(invalidObject));

            var validObject = new SomeObject
                                  {
                                      Name = ""
                                  };
            Assert.IsTrue(validator.IsValid(validObject));
        }
        public void CanTransferValidationResultsToModelState()
        {
            //RegisterAdditionalServices(container);

            var validator = new Validator();

            var invalidObject = new SomeObject
            {
                LastName = null,
                FirstName = "ThisFirstNameIsTooLong",
                Street = " ",
                MiddleName = "valid"
            };
            Assert.IsFalse(validator.IsValid(invalidObject));

            var results = validator.ValidationResultsFor(invalidObject);
            Assert.IsNotNull(results);
            Assert.AreEqual(3, results.Count, "Wrong number of validation messages encountered.");

            ModelStateDictionary modelState = new ModelStateDictionary();
            Assert.IsNotNull(modelState);
            Assert.AreEqual(0, modelState.Values.Count);

            invalidObject.TransferValidationMessagesTo(modelState);

            Assert.AreEqual(3, modelState.Values.Count);

            var resultsList = new List<string>();
            foreach (var result in modelState.Values)
            {
                foreach (var errs in result.Errors)
                {
                    resultsList.Add(errs.ErrorMessage);
                }
            }
            var errors = new[]
                             {
                                 "LastName: Dude...the name please!!",
                                 "Street: may not be null or empty",
                                 "FirstName: length must be between 0 and 10"
                             };

            Assert.AreEqual(resultsList.Count, errors.Length, "Number of error messages do not match");
            foreach (var error in errors)
            {
                Assert.IsTrue(resultsList.Contains(error), "Expected error \"" + error + "\" not found");
            }
        }
        public void CanValidateObject()
        {
            var validator = new Validator();

            var invalidObject = new RequiredObjectWithSpecificMessage
                                    {
                                        Name = " "
                                    };
            Assert.IsFalse(validator.IsValid(invalidObject));

            var validObject = new RequiredObjectWithSpecificMessage
                                  {
                                      Name = "x"
                                  };
            Assert.IsTrue(validator.IsValid(validObject));
        }
Exemple #8
0
 public void ValidateRangeDoubleDecimalIsNotValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<decimal> { (decimal)-3.01, -1, 0, 1, 3, (decimal)10.9999, 12 };
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidatorDecimal = list;
         ICollection<IValidationResult> results = validator.ValidationResultsFor(objectTotest);
         Assert.IsNotNull(results, "Failed on: \"" + list + "\"");
         Assert.AreEqual(1, results.Count, "Failed on: \"" + list + "\"");
         Assert.AreEqual("RangeDoubleValidatorDecimal", results.First().PropertyName, "Failed on: \"" + list + "\"");
         Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext, "Failed on: \"" + list + "\"");
         Assert.AreEqual("must be between 3.01 and 10.999", results.First().Message, "Failed on: \"" + list + "\"");
     }
 }
        public void CanValidateObject()
        {
            var validator = new Validator();

            var invalidObject = new SomeObject
            {
                LastName = "",
                FirstName = "ThisFirstNameIsTooLong",
                Street = null
            };
            Assert.IsFalse(validator.IsValid(invalidObject));

            var validObject = new SomeObject
            {
                LastName = "Last",
                FirstName = "First",
                Street = "SomeStreet"
            };
            Assert.IsTrue(validator.IsValid(validObject));
        }
Exemple #10
0
 public void ValidateRequiredAttributeSpacesOnlyTest()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     objectTotest.RequiredValidatorString = "    ";
     ICollection<IValidationResult> results = validator.ValidationResultsFor(objectTotest);
     Assert.IsNotNull(results);
     Assert.AreEqual(1, results.Count);
     Assert.AreEqual("RequiredValidatorString", results.First().PropertyName);
     Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext);
     Assert.AreEqual("may not be null or empty", results.First().Message);
 }
Exemple #11
0
 public void ValidateRangeDoubleMinOnlyIsValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<double> { 3, 3.000001, 3.1, 4, 20000000000.0, 3.0E300 };
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidatorMinOnly = list;
         Assert.IsTrue(validator.IsValid(objectTotest), "Failed on: \"" + list + "\"");
     }
 }
Exemple #12
0
 public void ValidateRangeDoubleStringIsValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<string> { null, "3", "3.5", "4", "3.0E2" };
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidatorString = list;
         Assert.IsTrue(validator.IsValid(objectTotest), "Failed on: \"" + list + "\"");
     }
 }
Exemple #13
0
 public void ValidateRequiredAttributeOverideMessageTest()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     objectTotest.RequiredValidatorSpecialMessage = "    ";
     ICollection<IValidationResult> results = validator.ValidationResultsFor(objectTotest);
     Assert.IsNotNull(results);
     Assert.AreEqual(1, results.Count);
     Assert.AreEqual("RequiredValidatorSpecialMessage", results.First().PropertyName);
     Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext);
     Assert.AreEqual("Special Error message here!", results.First().Message);
 }
Exemple #14
0
 public void ValidateRangeDoubleStringIsNotValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<string> { "-3", "-2", "0", "2.999", "1001", "ABC", "2.99E5" };
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidatorString = list;
         ICollection<IValidationResult> results = validator.ValidationResultsFor(objectTotest);
         Assert.IsNotNull(results, "Failed on: \"" + list + "\"");
         Assert.AreEqual(1, results.Count, "Failed on: \"" + list + "\"");
         Assert.AreEqual("RangeDoubleValidatorString", results.First().PropertyName, "Failed on: \"" + list + "\"");
         Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext, "Failed on: \"" + list + "\"");
         Assert.AreEqual("must be between 3 and 1000", results.First().Message, "Failed on: \"" + list + "\"");
     }
 }
Exemple #15
0
 public void ValidateRangeDoubleFloatIsValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<float> { 3, (float)3.01, (float)3.5, 4, (float)9.999, 10 };
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidatorFloat = list;
         Assert.IsTrue(validator.IsValid(objectTotest), "Failed on: \"" + list + "\"");
     }
 }
Exemple #16
0
 public void ValidateRangeDoubleIsValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<double>{1.01, 1.010001, 1.1, 1.999, 2, 2.0001, 2.4999999, 2.5};
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidator = list;
         Assert.IsTrue(validator.IsValid(objectTotest), "Failed on: \"" + list + "\"");
     }
 }
Exemple #17
0
 public void ValidateRangeDoubleLongIsValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<long> { 3, 4, 5, 6, 7, 8, 9, 10 };
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidatorLong = list;
         Assert.IsTrue(validator.IsValid(objectTotest), "Failed on: \"" + list + "\"");
     }
 }
Exemple #18
0
 public void ValidateRangeDoubleMinOnlyIsNotValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<double> { -1.0E200, -1.1, -1, 0, 0.0001, 1, 1.0000001, 2.5000001, 2.9999999 };
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidatorMinOnly = list;
         ICollection<IValidationResult> results = validator.ValidationResultsFor(objectTotest);
         Assert.IsNotNull(results, "Failed on: \"" + list + "\"");
         Assert.AreEqual(1, results.Count, "Failed on: \"" + list + "\"");
         Assert.AreEqual("RangeDoubleValidatorMinOnly", results.First().PropertyName, "Failed on: \"" + list + "\"");
         Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext, "Failed on: \"" + list + "\"");
         Assert.AreEqual("must be between 3 and 1.79769313486232E+308", results.First().Message, "Failed on: \"" + list + "\"");
     }
 }
Exemple #19
0
 public void ValidateRangeDoubleDecimalIsValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<decimal> { (decimal)3.01, (decimal)3.5, 4, (decimal)9.999, 10 };
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidatorDecimal = list;
         Assert.IsTrue(validator.IsValid(objectTotest), "Failed on: \"" + list + "\"");
     }
 }
Exemple #20
0
 public void ValidateRangeDoubleNullableIsNotValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<double?> { -3, -2, 0, 2.999, 4.0001, 5 };
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidatorNullable = list;
         ICollection<IValidationResult> results = validator.ValidationResultsFor(objectTotest);
         Assert.IsNotNull(results, "Failed on: \"" + list + "\"");
         Assert.AreEqual(1, results.Count, "Failed on: \"" + list + "\"");
         Assert.AreEqual("RangeDoubleValidatorNullable", results.First().PropertyName, "Failed on: \"" + list + "\"");
         Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext, "Failed on: \"" + list + "\"");
         Assert.AreEqual("must be between 3 and 4", results.First().Message, "Failed on: \"" + list + "\"");
     }
 }
Exemple #21
0
 public void ValidateRangeDoubleNullableIsValidTests()
 {
     var validator = new Validator();
     var objectTotest = CreateValidCompleteObject();
     var doubleList = new List<double?> { null, 3, 3.5, 4 };
     foreach (var list in doubleList)
     {
         objectTotest.RangeDoubleValidatorNullable = list;
         Assert.IsTrue(validator.IsValid(objectTotest), "Failed on: \"" + list + "\"");
     }
 }
        public void InvalidObjectWhenSpacesOnly()
        {
            var validator = new Validator();

            var invalidObject = new RequiredObjectWithGenericMessage()
            {
                Name = "   "
            };
            Assert.IsFalse(validator.IsValid(invalidObject));
        }