Skip to content

Commit 65acd1e

Browse files
committed
refactor: use cmp.Or and cmp.Compare
1 parent 9f49ee5 commit 65acd1e

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

internal/json/patch_test.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
package json_test
55

66
import (
7+
"cmp"
78
"slices"
89
"testing"
910

10-
"github.com/google/go-cmp/cmp"
11+
gocmp "github.com/google/go-cmp/cmp"
1112
tfjson "github.com/hashicorp/terraform-provider-aws/internal/json"
1213
mattbairdjsonpatch "github.com/mattbaird/jsonpatch"
1314
)
@@ -73,32 +74,20 @@ func TestCreatePatchFromStrings(t *testing.T) {
7374
t.Parallel()
7475

7576
got, err := tfjson.CreatePatchFromStrings(testCase.a, testCase.b)
76-
if got, want := err != nil, testCase.wantErr; !cmp.Equal(got, want) {
77+
if got, want := err != nil, testCase.wantErr; !gocmp.Equal(got, want) {
7778
t.Errorf("CreatePatchFromStrings(%s, %s) err %t, want %t", testCase.a, testCase.b, got, want)
7879
}
7980
if err == nil {
80-
sortTransformer := cmp.Transformer("SortPatchOps", func(ops []mattbairdjsonpatch.JsonPatchOperation) []mattbairdjsonpatch.JsonPatchOperation {
81+
sortTransformer := gocmp.Transformer("SortPatchOps", func(ops []mattbairdjsonpatch.JsonPatchOperation) []mattbairdjsonpatch.JsonPatchOperation {
8182
sorted := make([]mattbairdjsonpatch.JsonPatchOperation, len(ops))
8283
copy(sorted, ops)
8384
slices.SortFunc(sorted, func(a, b mattbairdjsonpatch.JsonPatchOperation) int {
84-
if a.Operation != b.Operation {
85-
if a.Operation < b.Operation {
86-
return -1
87-
}
88-
return 1
89-
}
90-
if a.Path < b.Path {
91-
return -1
92-
}
93-
if a.Path > b.Path {
94-
return 1
95-
}
96-
return 0
85+
return cmp.Or(cmp.Compare(a.Operation, b.Operation), cmp.Compare(a.Path, b.Path))
9786
})
9887
return sorted
9988
})
10089

101-
if diff := cmp.Diff(got, testCase.wantPatch, sortTransformer); diff != "" {
90+
if diff := gocmp.Diff(got, testCase.wantPatch, sortTransformer); diff != "" {
10291
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
10392
}
10493
}

0 commit comments

Comments
 (0)