Skip to content

Commit 6a4ac46

Browse files
geraldo-castroexageraldo
authored andcommitted
Add @WillAbides suggestion
1 parent bcfcb89 commit 6a4ac46

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

github/github_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,32 @@ func testBody(t *testing.T, r *http.Request, want string) {
144144
// to the want string.
145145
func testJSONMarshal(t *testing.T, v interface{}, want string) {
146146
t.Helper()
147-
// Unmarshal the wanted JSON, to verify its correctness, and marshal it back
148-
// to sort the keys.
149-
u := reflect.New(reflect.TypeOf(v)).Interface()
150-
if err := json.Unmarshal([]byte(want), &u); err != nil {
151-
t.Errorf("Unable to unmarshal JSON for %v: %v", want, err)
152-
}
153-
w, err := json.MarshalIndent(u, "", " ")
147+
got, err := json.Marshal(v)
154148
if err != nil {
155-
t.Errorf("Unable to marshal JSON for %#v", u)
149+
t.Errorf("Unable to marshal JSON for %#v", v)
150+
}
151+
got = normalizeJSON(t, got)
152+
wantBytes := normalizeJSON(t, []byte(want))
153+
diff := cmp.Diff(string(wantBytes), string(got))
154+
if diff != "" {
155+
t.Errorf("json.Marshal returned:\n%s\nwant:\n%s\ndiff:\n%v", string(got), string(wantBytes), diff)
156156
}
157+
}
157158

158-
// Marshal the target value.
159-
got, err := json.MarshalIndent(v, "", " ")
159+
// normalizeJSON normalizes the JSON in b by unmarshaling and marshaling it
160+
// again.
161+
func normalizeJSON(t *testing.T, b []byte) []byte {
162+
t.Helper()
163+
var v interface{}
164+
err := json.Unmarshal(b, &v)
160165
if err != nil {
161-
t.Errorf("Unable to marshal JSON for %#v", v)
166+
t.Errorf("Unable to unmarshal JSON for %v: %v", string(b), err)
162167
}
163-
164-
if diff := cmp.Diff(string(w), string(got)); diff != "" {
165-
t.Errorf("json.Marshal returned:\n%s\nwant:\n%s\ndiff:\n%v", got, w, diff)
168+
w, err := json.MarshalIndent(v, "", " ")
169+
if err != nil {
170+
t.Errorf("Unable to marshal JSON for %#v", v)
166171
}
172+
return w
167173
}
168174

169175
// Test whether the v fields have the url tag and the parsing of v

0 commit comments

Comments
 (0)