MCPcopy Index your code
hub / github.com/google/go-github / testJSONMarshalOnly

Function testJSONMarshalOnly

github/github_test.go:264–288  ·  view source on GitHub ↗

testJSONMarshalOnly tests JSON marshaling by comparing the marshaled output with the expected JSON string. This function compares JSON by unmarshaling both values into any and using cmp.Diff. This means the comparison ignores: - White space differences - Key ordering in objects - Numeric type diffe

(t *testing.T, v T, want string)

Source from the content-addressed store, hash-verified

262// In most cases, use testJSONMarshal instead.
263// Only use this function in rare cases where you need to test marshaling behavior in isolation.
264func testJSONMarshalOnly[T any](t *testing.T, v T, want string) {
265 t.Helper()
266
267 got, err := json.Marshal(v)
268 if err != nil {
269 t.Fatalf("Unable to marshal got JSON for %#v: %v", v, err)
270 }
271
272 // Unmarshal both the marshaled output and expected JSON into any
273 // to enable semantic comparison that ignores formatting differences
274 var gotAny any
275 if err := json.Unmarshal(got, &gotAny); err != nil {
276 t.Fatalf("Unable to unmarshal got JSON %v: %v", got, err)
277 }
278
279 var wantAny any
280 if err := json.Unmarshal([]byte(want), &wantAny); err != nil {
281 t.Fatalf("Unable to unmarshal want JSON %v: %v", want, err)
282 }
283
284 // Compare the semantic content
285 if diff := cmp.Diff(wantAny, gotAny); diff != "" {
286 t.Errorf("json.Marshal returned:\n%v\nwant:\n%v\ndiff:\n%v", got, want, diff)
287 }
288}
289
290// testJSONUnmarshalOnly tests JSON unmarshaling by parsing the JSON string
291// and comparing the result with the expected value.

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…