Skip to content

Commit e48613b

Browse files
committed
Use transformer for cmp.Diff to sort in comparison
1 parent 4817c6b commit e48613b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

internal/json/patch_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package json_test
55

66
import (
7+
"sort"
78
"testing"
89

910
"github.com/google/go-cmp/cmp"
@@ -76,7 +77,19 @@ func TestCreatePatchFromStrings(t *testing.T) {
7677
t.Errorf("CreatePatchFromStrings(%s, %s) err %t, want %t", testCase.a, testCase.b, got, want)
7778
}
7879
if err == nil {
79-
if diff := cmp.Diff(got, testCase.wantPatch); diff != "" {
80+
sortTransformer := cmp.Transformer("SortPatchOps", func(ops []mattbairdjsonpatch.JsonPatchOperation) []mattbairdjsonpatch.JsonPatchOperation {
81+
sorted := make([]mattbairdjsonpatch.JsonPatchOperation, len(ops))
82+
copy(sorted, ops)
83+
sort.Slice(sorted, func(i, j int) bool {
84+
if sorted[i].Operation != sorted[j].Operation {
85+
return sorted[i].Operation < sorted[j].Operation
86+
}
87+
return sorted[i].Path < sorted[j].Path
88+
})
89+
return sorted
90+
})
91+
92+
if diff := cmp.Diff(got, testCase.wantPatch, sortTransformer); diff != "" {
8093
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
8194
}
8295
}

0 commit comments

Comments
 (0)