Add additional test cases for json diffing.

This commit is contained in:
Nick Tobey
2025-11-22 15:53:48 -08:00
parent c7773d1762
commit c43c31b477
@@ -252,6 +252,58 @@ var SimpleJsonDiffTests = []JsonDiffTest{
},
},
},
{
// Currently, inserting or removing elements from an array is seen as a modification at each subsequent index.
// This may change in the future.
Name: "array insert in middle",
From: types.JSONDocument{Val: types.JsonArray{1, 2, 3}},
To: types.JSONDocument{Val: types.JsonArray{1, "inserted", 2, 3}},
ExpectedDiffs: []JsonDiff{
{
Key: makeJsonPathKey(1),
From: &types.JSONDocument{Val: 2},
To: &types.JSONDocument{Val: "inserted"},
Type: ModifiedDiff,
},
{
Key: makeJsonPathKey(2),
From: &types.JSONDocument{Val: 3},
To: &types.JSONDocument{Val: 2},
Type: ModifiedDiff,
},
{
Key: makeJsonPathKey(3),
To: &types.JSONDocument{Val: 3},
Type: AddedDiff,
},
},
},
{
// Currently, inserting or removing elements from an array is seen as a modification at each subsequent index.
// This may change in the future.
Name: "array removal in middle",
From: types.JSONDocument{Val: types.JsonArray{1, "removed", 2, 3}},
To: types.JSONDocument{Val: types.JsonArray{1, 2, 3}},
ExpectedDiffs: []JsonDiff{
{
Key: makeJsonPathKey(1),
From: &types.JSONDocument{Val: "removed"},
To: &types.JSONDocument{Val: 2},
Type: ModifiedDiff,
},
{
Key: makeJsonPathKey(2),
From: &types.JSONDocument{Val: 2},
To: &types.JSONDocument{Val: 3},
Type: ModifiedDiff,
},
{
Key: makeJsonPathKey(3),
From: &types.JSONDocument{Val: 3},
Type: RemovedDiff,
},
},
},
{
Name: "array modification in object",
From: types.JSONDocument{Val: types.JsonObject{"a": types.JsonArray{1, 2}}},
@@ -265,6 +317,31 @@ var SimpleJsonDiffTests = []JsonDiffTest{
},
},
},
{
Name: "object modification in array",
From: types.JSONDocument{Val: types.JsonArray{types.JsonObject{"a": "b", "c": "d"}, types.JsonObject{"e": "f"}, types.JsonObject{"i": "j"}}},
To: types.JSONDocument{Val: types.JsonArray{types.JsonObject{"a": "b"}, types.JsonObject{"e": "f", "g": "h"}, types.JsonObject{"i": "k"}}},
ExpectedDiffs: []JsonDiff{
{
Key: makeJsonPathKey(0, "c"),
From: &types.JSONDocument{Val: "d"},
To: nil,
Type: RemovedDiff,
},
{
Key: makeJsonPathKey(1, "g"),
From: nil,
To: &types.JSONDocument{Val: "h"},
Type: AddedDiff,
},
{
Key: makeJsonPathKey(2, "i"),
From: &types.JSONDocument{Val: "j"},
To: &types.JSONDocument{Val: "k"},
Type: ModifiedDiff,
},
},
},
{
Name: "primitive modification",
From: types.JSONDocument{Val: "before"},