Ejemplo n.º 1
0
        public void InKeyValue_AValueNotInKeyValue_AddXPathInMessage()
        {
            var note   = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Subject</subject>
  <type>ValueNotInKeyValue</type>
  <body>Hi</body>
</note>");
            var values = new Dictionary <string, string> {
                { "Salutation", "Just say hi!" }
            };
            var typeXPath   = "/note/type";
            var validations = MakeValidator(new XPath(typeXPath), new InKeyValue(values));
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            var message = results.Errors.FirstOrDefault()?.ErrorMessage;

            Assert.IsNotNull(message);
            StringAssert.Contains(typeXPath, message);
        }
Ejemplo n.º 2
0
        public void Required_ANonExistingNode_IsInvalid()
        {
            var note        = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Subject</subject>
</note>");
            var validations = MakeValidator(new XPath("/note/body"), new Required());
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            Assert.IsFalse(results.IsValid);
        }
Ejemplo n.º 3
0
        public void Value_AnEmptyNode_IsValid()
        {
            var note        = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <body>Body</body>
</note>");
            var validations = MakeValidator(new XPath("/note/subject"), new Value("Subject"));
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            Assert.IsTrue(results.IsValid);
        }
Ejemplo n.º 4
0
        public void Length_ValueInsideRange_IsValid()
        {
            var note        = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Subject</subject>
  <body>0123456789</body>
</note>");
            var validations = MakeValidator(new XPath("/note/body"), new Length(min: 1, max: 10));
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            Assert.IsTrue(results.IsValid);
        }
Ejemplo n.º 5
0
        public void Length_ValueSmallerThanMinimum_IsInvalid()
        {
            var note        = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Subject</subject>
  <body>ThisValueIsSmallerThanMinimum</body>
</note>");
            var validations = MakeValidator(new XPath("/note/body"), new Length(min: 1000, max: int.MaxValue));
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            Assert.IsFalse(results.IsValid);
        }
Ejemplo n.º 6
0
        public void ValidateIf_NodeIsEmpty_IsValid()
        {
            var note        = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Salutation</subject>
  <body></body>
</note>");
            var validateIf  = new ValidateIf((doc) => doc.Evaluate <string>(new XPath("/note/subject")) == "Salutation", (value) => value.StartsWith("Hi"));
            var validations = MakeValidator(new XPath("/note/body"), validateIf);
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            Assert.IsTrue(results.IsValid);
        }
Ejemplo n.º 7
0
        public void Matches_AValueThatMatchesRegex_IsValid()
        {
            var formattedDate = "2019-01-01";
            var note          = MakeNote($@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <date>{formattedDate}</date>
  <subject>Subject</subject>
  <body>Body</body>
</note>");
            var validations   = MakeValidator(new XPath("/note/date"), new Matches(new Regex(@"\d{4}-\d{2}-\d{2}")));
            var validator     = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            Assert.IsTrue(results.IsValid);
        }
Ejemplo n.º 8
0
        public void Matches_AValueThatDoesNotMatchesRegex_IsInvalid()
        {
            var dateWithoutFormat = "January 1st 2019";
            var note        = MakeNote($@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <date>{dateWithoutFormat}</date>
  <subject>Subject</subject>
  <body>Body</body>
</note>");
            var validations = MakeValidator(new XPath("/note/date"), new Matches(new Regex(@"\d{4}-\d{2}-\d{2}")));
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            Assert.IsFalse(results.IsValid);
        }
Ejemplo n.º 9
0
        public void AreEqual_EmptyExpectedNode_IsValid()
        {
            var note        = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Subject</subject>
  <body>Hi</body>
  <envelope>
    <recipient></recipient>
  </envelope>
</note>");
            var validations = MakeValidator(new XPath("/note/to"), new AreEqual(new XPath("/note/envelope/recipient")));
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            Assert.IsTrue(results.IsValid);
        }
Ejemplo n.º 10
0
        public void Required_AnEmptyNode_AddsXPathInMessage()
        {
            var note        = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Subject</subject>
  <body></body>
</note>");
            var bodyXPath   = "/note/body";
            var validations = MakeValidator(new XPath(bodyXPath), new Required());
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            var message = results.Errors.FirstOrDefault()?.ErrorMessage;

            Assert.IsNotNull(message);
            StringAssert.Contains(bodyXPath, message);
        }
Ejemplo n.º 11
0
        public void Length_ValueGreaterThanMaximum_AddXPathInMessage()
        {
            var note        = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Subject</subject>
  <body>ThisValueIsGreaterThanMaximum</body>
</note>");
            var bodyXPath   = "/note/body";
            var validations = MakeValidator(new XPath("/note/body"), new Length(min: 1, max: 10));
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            var message = results.Errors.FirstOrDefault()?.ErrorMessage;

            Assert.IsNotNull(message);
            StringAssert.Contains(bodyXPath, message);
        }
Ejemplo n.º 12
0
        public void InKeyValue_AValueNotInKeyValue_IsInvalid()
        {
            var note   = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Subject</subject>
  <type>ValueNotInKeyValue</type>
  <body>Hi</body>
</note>");
            var values = new Dictionary <string, string> {
                { "Salutation", "Just say hi!" }
            };
            var validations = MakeValidator(new XPath("/note/type"), new InKeyValue(values));
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            Assert.IsFalse(results.IsValid);
        }
Ejemplo n.º 13
0
        public void ValidateIf_ConditionIsTrueButPredicateIsNot_AddsXPathInMessage()
        {
            var note        = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Salutation</subject>
  <body>ThisValueDoesNotStartWithHiSoPredicateIsNotTrue</body>
</note>");
            var bodyXPath   = "/note/body";
            var validateIf  = new ValidateIf((doc) => doc.Evaluate <string>(new XPath("/note/subject")) == "Salutation", (value) => value.StartsWith("Hi"));
            var validations = MakeValidator(new XPath(bodyXPath), validateIf);
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            var message = results.Errors.FirstOrDefault()?.ErrorMessage;

            Assert.IsNotNull(message);
            StringAssert.Contains(bodyXPath, message);
        }
Ejemplo n.º 14
0
        public void Matches_AValueThatDoesNotMatchesRegex_AddXPathInMessage()
        {
            var dateWithoutFormat = "January 1st 2019";
            var note        = MakeNote($@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <date>{dateWithoutFormat}</date>
  <subject>Subject</subject>
  <body>Body</body>
</note>");
            var dateXPath   = "/note/date";
            var validations = MakeValidator(new XPath(dateXPath), new Matches(new Regex(@"\d{4}-\d{2}-\d{2}")));
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            var message = results.Errors.FirstOrDefault()?.ErrorMessage;

            Assert.IsNotNull(message);
            StringAssert.Contains(dateXPath, message);
        }
Ejemplo n.º 15
0
        public void AreEqual_TwoDifferentValues_AddXPathInMessage()
        {
            var note        = MakeNote(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<note>
  <from>Bob</from>
  <to>Alice</to>
  <subject>Subject</subject>
  <body>Hi</body>
  <envelope>
    <recipient>RecipientDifferentFromAlice</recipient>
  </envelope>
</note>");
            var toXPath     = "/note/to";
            var validations = MakeValidator(new XPath(toXPath), new AreEqual(new XPath("/note/envelope/recipient")));
            var validator   = new CheckDocument(validations);

            ValidationResult results = validator.Validate(note);

            var message = results.Errors.FirstOrDefault()?.ErrorMessage;

            Assert.IsNotNull(message);
            StringAssert.Contains(toXPath, message);
        }