Skip to content

Commit bce7629

Browse files
thedoddaren55555
authored andcommitted
Use subtests (can only test against go >= 1.7 now). (#63)
1 parent 2b01775 commit bce7629

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

errors_test.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,36 @@ func TestErrorObjectWritesExpectedErrorMessage(t *testing.T) {
2222

2323
func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
2424
var marshalErrorsTableTasts = []struct {
25-
In []*ErrorObject
26-
Out map[string]interface{}
25+
Title string
26+
In []*ErrorObject
27+
Out map[string]interface{}
2728
}{
28-
{ // This tests that given fields are turned into the appropriate JSON representation.
29-
In: []*ErrorObject{{ID: "0", Title: "Test title.", Detail: "Test detail", Status: "400", Code: "E1100"}},
29+
{
30+
Title: "TestFieldsAreSerializedAsNeeded",
31+
In: []*ErrorObject{{ID: "0", Title: "Test title.", Detail: "Test detail", Status: "400", Code: "E1100"}},
3032
Out: map[string]interface{}{"errors": []interface{}{
3133
map[string]interface{}{"id": "0", "title": "Test title.", "detail": "Test detail", "status": "400", "code": "E1100"},
3234
}},
3335
},
34-
{ // This tests that the `Meta` field is serialized properly.
35-
In: []*ErrorObject{{Title: "Test title.", Detail: "Test detail", Meta: &map[string]interface{}{"key": "val"}}},
36+
{
37+
Title: "TestMetaFieldIsSerializedProperly",
38+
In: []*ErrorObject{{Title: "Test title.", Detail: "Test detail", Meta: &map[string]interface{}{"key": "val"}}},
3639
Out: map[string]interface{}{"errors": []interface{}{
3740
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "meta": map[string]interface{}{"key": "val"}},
3841
}},
3942
},
4043
}
4144
for _, testRow := range marshalErrorsTableTasts {
42-
buffer, output := bytes.NewBuffer(nil), map[string]interface{}{}
43-
var writer io.Writer = buffer
45+
t.Run(testRow.Title, func(t *testing.T) {
46+
buffer, output := bytes.NewBuffer(nil), map[string]interface{}{}
47+
var writer io.Writer = buffer
4448

45-
_ = MarshalErrors(writer, testRow.In)
46-
json.Unmarshal(buffer.Bytes(), &output)
49+
_ = MarshalErrors(writer, testRow.In)
50+
json.Unmarshal(buffer.Bytes(), &output)
4751

48-
if !reflect.DeepEqual(output, testRow.Out) {
49-
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
50-
}
52+
if !reflect.DeepEqual(output, testRow.Out) {
53+
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
54+
}
55+
})
5156
}
5257
}

request_test.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package jsonapi
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"io"
78
"reflect"
89
"sort"
@@ -201,19 +202,21 @@ func TestUnmarshalInvalidJSON_BadType(t *testing.T) {
201202
{Field: "time_ptr_field", BadValue: "A string.", Error: ErrInvalidTime}, // Expected *time / int64.
202203
}
203204
for _, test := range badTypeTests {
204-
out := new(ModelBadTypes)
205-
in := map[string]interface{}{}
206-
in[test.Field] = test.BadValue
207-
expectedErrorMessage := test.Error.Error()
208-
209-
err := UnmarshalPayload(samplePayloadWithBadTypes(in), out)
210-
211-
if err == nil {
212-
t.Fatalf("Expected error due to invalid type.")
213-
}
214-
if err.Error() != expectedErrorMessage {
215-
t.Fatalf("Unexpected error message: %s", err.Error())
216-
}
205+
t.Run(fmt.Sprintf("Test_%s", test.Field), func(t *testing.T) {
206+
out := new(ModelBadTypes)
207+
in := map[string]interface{}{}
208+
in[test.Field] = test.BadValue
209+
expectedErrorMessage := test.Error.Error()
210+
211+
err := UnmarshalPayload(samplePayloadWithBadTypes(in), out)
212+
213+
if err == nil {
214+
t.Fatalf("Expected error due to invalid type.")
215+
}
216+
if err.Error() != expectedErrorMessage {
217+
t.Fatalf("Unexpected error message: %s", err.Error())
218+
}
219+
})
217220
}
218221
}
219222

0 commit comments

Comments
 (0)