public static (IReadOnlyCollection <DiffItem> mismatches, JToken expected) CompareAndRebase(this TestCase testCase, JToken actual, JToken @base, IEnumerable <DiffItem> actualDiffs, string resultName, ITokenSelectorProvider idProvider, bool rebaseline) { var expectedDiffs = testCase.GetObjectFromJson <IEnumerable <DiffItem> >(resultName) ?? Enumerable.Empty <DiffItem>(); var expected = @base.DeepClone(); try { expected.ApplyPatch(expectedDiffs); } catch (Exception _) { testCase.RebaseContentOrStoreTmp(resultName, actualDiffs.ToIndentedJson(), rebaseline); throw; } var mismatches = JsonPatchCreator.Compare(expected, actual, idProvider).ToList(); if (mismatches.Count > 0) { testCase.RebaseContentOrStoreTmp(resultName, actualDiffs.ToIndentedJson(), rebaseline); } testCase.AddTmpItem("full_" + resultName, actual.ToIndentedJson()); return(mismatches, expected); }
public async Task ExecuteAsync(TestCaseContext testCaseContext) { var effectiveItemKey = testCaseContext.GetEffectiveItemKey(_itemKey); testCaseContext.DescriptionWriter.AddNote("AssertContentDiff", $"{effectiveItemKey} => file: {_diffName}, base: {_baseFile}"); var actual = testCaseContext.GetRequiredItemAsJToken(effectiveItemKey); if (!string.IsNullOrEmpty(_jsonPath)) { actual = new JArray(actual.SelectTokens(_jsonPath)); } var testCase = testCaseContext.TestCase; var expectedBase = await testCase.GetObjectFromJsonAsync <JToken>(_baseFile); var diffs = JsonPatchCreator.Compare(expectedBase, actual, _tokenSelectorProvider); (var mismatches, var expected) = await testCase.CompareAndRebaseAsync(actual, expectedBase, diffs, _diffName, _tokenSelectorProvider, testCaseContext.Options.IsRebaseline()); if (mismatches.Count > 0) { var matchedDiffCategories = testCaseContext.GetDiffCategories(actual, expected, _categoriesFile); throw new AssertException("Unexpected token changes:" + string.Join("\n", mismatches.Select(d => d.JsonPath)), matchedDiffCategories); } }
public void CompareTest(JToken lhs, JToken rhs, string expectedResult) { var result = JsonPatchCreator.Compare(lhs, rhs, _propertyBasedIdProvider).ToList(); Assert.That(JsonConvert.SerializeObject(result), Is.EqualTo(expectedResult)); lhs.ApplyPatch(result); Assert.That(lhs, Is.EqualTo(rhs)); }
public static void AssertContentAsDiff(this TestCase testCase, JToken actual, JToken expected, string resultName, ITokenSelectorProvider idProvider, bool rebaseline) { var diffs = JsonPatchCreator.Compare(expected, actual, idProvider); (var mismatches, var _) = testCase.CompareAndRebase(actual, expected, diffs, resultName, idProvider, rebaseline); if (mismatches.Count > 0) { throw new AssertException("Unexpected token changes:" + string.Join("\n", mismatches.Select(d => d.JsonPath))); } }