protected void ShouldFailRule(Action <ValidationTestConfig> configure) { var config = new ValidationTestConfig(); config.Rule(Rule); configure(config); config.Rules.Any().ShouldBeTrue("Must provide at least one rule to validate against."); var result = Validate(config.Query, Schema, config.Rules); result.IsValid.ShouldBeFalse("Expected validation errors though there were none."); result.Errors.Count.ShouldEqual( config.Assertions.Count(), $"The number of errors found ({result.Errors.Count}) does not match the number of errors expected ({config.Assertions.Count()})."); config.Assertions.Apply((assert, idx) => { var error = result.Errors.Skip(idx).First(); error.Message.ShouldEqual(assert.Message); assert.Locations.Apply((assertLoc, locIdx) => { var errorLoc = error.Locations.Skip(locIdx).First(); errorLoc.Line.ShouldEqual( assertLoc.Line, $"Expected line {assertLoc.Line} but was {errorLoc.Line} - {error.Message} ({errorLoc.Line},{errorLoc.Column})"); errorLoc.Column.ShouldEqual( assertLoc.Column, $"Expected column {assertLoc.Column} but was {errorLoc.Column} - {error.Message} ({errorLoc.Line},{errorLoc.Column})"); }); error.Locations.Count().ShouldEqual(assert.Locations.Count()); }); }
protected void ShouldPassRule(Action <ValidationTestConfig> configure) { var config = new ValidationTestConfig(); config.Rule(Rule); configure(config); config.Rules.Any().ShouldBeTrue("Must provide at least one rule to validate against."); var result = Validate(config.Query, config.Schema ?? Schema, config.Rules); var message = ""; if (result.Errors?.Any() == true) { message = string.Join(", ", result.Errors.Select(x => x.Message)); } result.IsValid.ShouldBeTrue(message); }
protected void ShouldFailRule(Action <ValidationTestConfig> configure) { var config = new ValidationTestConfig(); config.Rule(Rule); configure(config); config.Rules.Any().ShouldBeTrue("Must provide at least one rule to validate against."); var result = Validate(config.Query, config.Schema ?? Schema, config.Rules); result.IsValid.ShouldBeFalse("Expected validation errors though there were none."); result.Errors.Count.ShouldBe( config.Assertions.Count, $"The number of errors found ({result.Errors.Count}) does not match the number of errors expected ({config.Assertions.Count})."); for (int i = 0; i < config.Assertions.Count; i++) { var assert = config.Assertions[i]; var error = result.Errors[i]; error.Message.ShouldBe(assert.Message); var allLocations = string.Concat(error.Locations.Select(l => $"({l.Line},{l.Column})")); var locations = error.Locations.ToList(); for (int j = 0; j < assert.Locations.Count; j++) { var assertLoc = assert.Locations[j]; var errorLoc = locations[j]; errorLoc.Line.ShouldBe( assertLoc.Line, $"Expected line {assertLoc.Line} but was {errorLoc.Line} - {error.Message} {allLocations}"); errorLoc.Column.ShouldBe( assertLoc.Column, $"Expected column {assertLoc.Column} but was {errorLoc.Column} - {error.Message} {allLocations}"); } locations.Count.ShouldBe(assert.Locations.Count); } }