(t *testing.T)
| 386 | } |
| 387 | |
| 388 | func TestMarshalJSONAPI(t *testing.T) { |
| 389 | t.Parallel() |
| 390 | |
| 391 | articleAJSONAPIBody := `{"data":{"id":"1","type":"articles","attributes":{"title":"A"}},"jsonapi":{"version":"1.0"}}` |
| 392 | articleAJSONAPIMetaBody := `{"data":{"id":"1","type":"articles","attributes":{"title":"A"}},"jsonapi":{"version":"1.0","meta":{"foo":"bar"}}}` |
| 393 | errorsObjectMetaBody := `{"jsonapi":{"version":"1.0","meta":{"foo":"bar"}},"errors":[{"title":"T"}]}` |
| 394 | |
| 395 | tests := []struct { |
| 396 | description string |
| 397 | given any |
| 398 | givenJSONAPI any |
| 399 | expect string |
| 400 | expectError error |
| 401 | }{ |
| 402 | { |
| 403 | description: "include jsonapi", |
| 404 | given: &articleA, |
| 405 | givenJSONAPI: nil, |
| 406 | expect: articleAJSONAPIBody, |
| 407 | expectError: nil, |
| 408 | }, { |
| 409 | description: "include jsonapi and meta (map)", |
| 410 | given: &articleA, |
| 411 | givenJSONAPI: map[string]any{"foo": "bar"}, |
| 412 | expect: articleAJSONAPIMetaBody, |
| 413 | expectError: nil, |
| 414 | }, { |
| 415 | description: "include jsonapi and meta (struct)", |
| 416 | given: &articleA, |
| 417 | givenJSONAPI: &struct { |
| 418 | Foo string `json:"foo"` |
| 419 | }{Foo: "bar"}, |
| 420 | expect: articleAJSONAPIMetaBody, |
| 421 | expectError: nil, |
| 422 | }, { |
| 423 | description: "include jsonapi and meta (non-object type)", |
| 424 | given: &articleA, |
| 425 | givenJSONAPI: "foo", |
| 426 | expect: "", |
| 427 | expectError: &TypeError{Actual: "string", Expected: []string{"struct", "map"}}, |
| 428 | }, { |
| 429 | description: "include jsonapi and meta (map) errors object", |
| 430 | given: errorsSimpleSliceSinglePtr, |
| 431 | givenJSONAPI: map[string]any{"foo": "bar"}, |
| 432 | expect: errorsObjectMetaBody, |
| 433 | expectError: nil, |
| 434 | }, |
| 435 | } |
| 436 | |
| 437 | for i, tc := range tests { |
| 438 | tc := tc |
| 439 | t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 440 | t.Parallel() |
| 441 | t.Log(tc.description) |
| 442 | |
| 443 | actual, err := Marshal(tc.given, MarshalJSONAPI(tc.givenJSONAPI)) |
| 444 | if tc.expectError != nil { |
| 445 | is.EqualError(t, tc.expectError, err) |
nothing calls this directly
no test coverage detected