public void SetUp() { _jsonComparer = Substitute.For <IJsonComparer>(); _jsonArrayComparer = new JsonArrayComparer(); _jsonComparer.Compare(Arg.Any <JToken>(), Arg.Any <JToken>(), Arg.Any <string>()) .Returns(new List <JsonCompareError>()); }
public IEnumerable <IJsonCompareError <JToken> > Compare(JObject expected, JObject actual, IJsonComparer jsonComparer, string path = "") { foreach (var actualProperty in actual.Properties()) { var expectedProperty = expected.Property(actualProperty.Name); if (expectedProperty == null) { yield return(new UnexpectedPropertyJsonComparerError(path, expected, actual, actualProperty)); } } foreach (var expectedProperty in expected.Properties()) { var actualProperty = actual.Property(expectedProperty.Name); var expectedJToken = expectedProperty.Value; if (actualProperty == null) { yield return(new MissingPropertyJsonComparerError(path, expected, actual, expectedProperty)); continue; } var elementPath = JsonPathUtils.Combine(path, actualProperty.Name); var errors = jsonComparer.Compare(expectedJToken, actualProperty.Value, elementPath); foreach (var jsonCompareError in errors) { yield return(jsonCompareError); } } }
public IEnumerable <IJsonCompareError <JToken> > Compare(JArray expected, JArray actual, IJsonComparer jsonComparer, string path = "") { if (expected.Count != actual.Count) { yield return(new InvalidSizeJsonCompareError(path, expected, actual)); yield break; } for (var i = 0; i < expected.Count; i++) { var expectedElement = expected[i]; var actualElement = actual[i]; var errors = jsonComparer.Compare(expectedElement, actualElement, JsonPathUtils.Combine(path, $"[{i}]")); foreach (var error in errors) { yield return(error); } } }
public HttpSteps(ScenarioContext scenarioContext, NaheulbookHttpClient naheulbookHttpClient, IJsonComparer jsonComparer) { _scenarioContext = scenarioContext; _naheulbookHttpClient = naheulbookHttpClient; _jsonComparer = jsonComparer; }
public JsonEquivalentConstraint WithComparer(IJsonComparer jsonComparer) { _jsonComparer = jsonComparer; return(this); }
public (bool success, IList <IJsonCompareError <JToken> > errors) HandleSpecialObject(JToken expected, JToken actual, string path, IJsonComparer jsonComparer) { if (IsCaptureObject(expected)) { return(HandleCaptureObject(expected, actual, path)); } if (IsMatchObject(expected)) { return(HandleMatchObject(expected, actual, path)); } if (IsPartialObject(expected)) { return(HandlePartialObject(expected, actual, path, jsonComparer)); } return(false, null); }
private (bool success, IList <IJsonCompareError <JToken> > errors) HandlePartialObject(JToken expected, JToken actual, string path, IJsonComparer jsonComparer) { var jPartialObject = expected.Value <JToken>("__partial"); if (jPartialObject.Type != JTokenType.Object) { return(false, new List <IJsonCompareError <JToken> > { new InvalidPartialObjectCompareError(path, expected, actual, $"Invalid `type` of __partial object. Partial comparison is only supported for JSON Object yet") }); } if (actual.Type != jPartialObject.Type) { return(false, new List <IJsonCompareError <JToken> > { new InvalidTypeJsonCompareError(path, jPartialObject, actual) }); } var errors = _jsonObjectPartialComparer.Compare(jPartialObject as JObject, actual as JObject, jsonComparer).ToList(); if (expected.Parent is JProperty parentProperty) { parentProperty.Value = jPartialObject; } if (errors.Count > 0) { return(false, errors); } return(true, null); }