Use subtests (can only test against go >= 1.7 now). (#63)

This commit is contained in:
Anthony Dodd 2017-02-17 17:11:39 -06:00 committed by Aren Patel
parent 2b01775d0f
commit bce7629682
2 changed files with 32 additions and 24 deletions

View File

@ -22,16 +22,19 @@ func TestErrorObjectWritesExpectedErrorMessage(t *testing.T) {
func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
var marshalErrorsTableTasts = []struct {
Title string
In []*ErrorObject
Out map[string]interface{}
}{
{ // This tests that given fields are turned into the appropriate JSON representation.
{
Title: "TestFieldsAreSerializedAsNeeded",
In: []*ErrorObject{{ID: "0", Title: "Test title.", Detail: "Test detail", Status: "400", Code: "E1100"}},
Out: map[string]interface{}{"errors": []interface{}{
map[string]interface{}{"id": "0", "title": "Test title.", "detail": "Test detail", "status": "400", "code": "E1100"},
}},
},
{ // This tests that the `Meta` field is serialized properly.
{
Title: "TestMetaFieldIsSerializedProperly",
In: []*ErrorObject{{Title: "Test title.", Detail: "Test detail", Meta: &map[string]interface{}{"key": "val"}}},
Out: map[string]interface{}{"errors": []interface{}{
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "meta": map[string]interface{}{"key": "val"}},
@ -39,6 +42,7 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
},
}
for _, testRow := range marshalErrorsTableTasts {
t.Run(testRow.Title, func(t *testing.T) {
buffer, output := bytes.NewBuffer(nil), map[string]interface{}{}
var writer io.Writer = buffer
@ -48,5 +52,6 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
if !reflect.DeepEqual(output, testRow.Out) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
}
})
}
}

View File

@ -3,6 +3,7 @@ package jsonapi
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"sort"
@ -201,6 +202,7 @@ func TestUnmarshalInvalidJSON_BadType(t *testing.T) {
{Field: "time_ptr_field", BadValue: "A string.", Error: ErrInvalidTime}, // Expected *time / int64.
}
for _, test := range badTypeTests {
t.Run(fmt.Sprintf("Test_%s", test.Field), func(t *testing.T) {
out := new(ModelBadTypes)
in := map[string]interface{}{}
in[test.Field] = test.BadValue
@ -214,6 +216,7 @@ func TestUnmarshalInvalidJSON_BadType(t *testing.T) {
if err.Error() != expectedErrorMessage {
t.Fatalf("Unexpected error message: %s", err.Error())
}
})
}
}