Skip to content

Commit

Permalink
tests: add missing cases for unmarshal error
Browse files Browse the repository at this point in the history
  • Loading branch information
kernle32dll committed May 4, 2019
1 parent c016143 commit c6a345d
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 49 deletions.
42 changes: 35 additions & 7 deletions bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ package nullable
import (
"bytes"
"encoding/json"
"reflect"
"testing"
)

func TestBool_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
buf *bytes.Buffer
expect Bool
name string
buf *bytes.Buffer
expect Bool
expectErr error
}{
{
name: "null value",
buf: bytes.NewBufferString(`{"value":null}`),
expect: Bool{
Present: true,
},
expectErr: nil,
},
{
name: "valid value",
Expand All @@ -27,11 +30,21 @@ func TestBool_UnmarshalJSON(t *testing.T) {
Valid: true,
Value: true,
},
expectErr: nil,
},
{
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Bool{},
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Bool{},
expectErr: nil,
},
{
name: "unmarshallable",
buf: bytes.NewBufferString(`{"value":42}`),
expect: Bool{
Present: true,
},
expectErr: &json.UnmarshalTypeError{},
},
}
for _, tt := range tests {
Expand All @@ -40,7 +53,7 @@ func TestBool_UnmarshalJSON(t *testing.T) {
Value Bool `json:"value"`
}{}

if err := json.Unmarshal(tt.buf.Bytes(), &str); err != nil {
if err := json.Unmarshal(tt.buf.Bytes(), &str); !typeMatch(tt.expectErr, err) {
t.Fatalf("unexpected unmarshaling error: %s", err)
}

Expand All @@ -51,3 +64,18 @@ func TestBool_UnmarshalJSON(t *testing.T) {
})
}
}

func typeMatch(expected, actual interface{}) bool {
if expected != nil && actual != nil {
expectedType := reflect.TypeOf(expected)
actualType := reflect.TypeOf(actual)

return actualType.AssignableTo(expectedType)
} else if expected == nil && actual == nil {
return true
} else {
// One of them was nil, while the other was not
// -> never matches
return false
}
}
52 changes: 38 additions & 14 deletions float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import (

func TestFloat_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
buf *bytes.Buffer
expect Float
name string
buf *bytes.Buffer
expect Float
expectErr error
}{
{
name: "null value",
buf: bytes.NewBufferString(`{"value":null}`),
expect: Float{
Present: true,
},
expectErr: nil,
},
{
name: "valid value",
Expand All @@ -28,11 +30,21 @@ func TestFloat_UnmarshalJSON(t *testing.T) {
Valid: true,
Value: 1.1,
},
expectErr: nil,
},
{
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Float{},
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Float{},
expectErr: nil,
},
{
name: "unmarshallable",
buf: bytes.NewBufferString(`{"value":"wat"}`),
expect: Float{
Present: true,
},
expectErr: &json.UnmarshalTypeError{},
},
}
for _, tt := range tests {
Expand All @@ -41,7 +53,7 @@ func TestFloat_UnmarshalJSON(t *testing.T) {
Value Float `json:"value"`
}{}

if err := json.Unmarshal(tt.buf.Bytes(), &str); err != nil {
if err := json.Unmarshal(tt.buf.Bytes(), &str); !typeMatch(tt.expectErr, err) {
t.Fatalf("unexpected unmarshaling error: %s", err)
}

Expand All @@ -55,16 +67,18 @@ func TestFloat_UnmarshalJSON(t *testing.T) {

func TestFloatSlice_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
buf *bytes.Buffer
expect FloatSlice
name string
buf *bytes.Buffer
expect FloatSlice
expectErr error
}{
{
name: "null value",
buf: bytes.NewBufferString(`{"value":null}`),
expect: FloatSlice{
Present: true,
},
expectErr: nil,
},
{
name: "valid value",
Expand All @@ -74,11 +88,21 @@ func TestFloatSlice_UnmarshalJSON(t *testing.T) {
Valid: true,
Value: []float64{1.1, 1.2},
},
expectErr: nil,
},
{
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: FloatSlice{},
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: FloatSlice{},
expectErr: nil,
},
{
name: "unmarshallable",
buf: bytes.NewBufferString(`{"value":"wat"}`),
expect: FloatSlice{
Present: true,
},
expectErr: &json.UnmarshalTypeError{},
},
}
for _, tt := range tests {
Expand All @@ -87,7 +111,7 @@ func TestFloatSlice_UnmarshalJSON(t *testing.T) {
Value FloatSlice `json:"value"`
}{}

if err := json.Unmarshal(tt.buf.Bytes(), &str); err != nil {
if err := json.Unmarshal(tt.buf.Bytes(), &str); !typeMatch(tt.expectErr, err) {
t.Fatalf("unexpected unmarshaling error: %s", err)
}

Expand Down
52 changes: 38 additions & 14 deletions int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import (

func TestInt_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
buf *bytes.Buffer
expect Int
name string
buf *bytes.Buffer
expect Int
expectErr error
}{
{
name: "null value",
buf: bytes.NewBufferString(`{"value":null}`),
expect: Int{
Present: true,
},
expectErr: nil,
},
{
name: "valid value",
Expand All @@ -28,11 +30,21 @@ func TestInt_UnmarshalJSON(t *testing.T) {
Valid: true,
Value: 1,
},
expectErr: nil,
},
{
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Int{},
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Int{},
expectErr: nil,
},
{
name: "unmarshallable",
buf: bytes.NewBufferString(`{"value":"wat"}`),
expect: Int{
Present: true,
},
expectErr: &json.UnmarshalTypeError{},
},
}
for _, tt := range tests {
Expand All @@ -41,7 +53,7 @@ func TestInt_UnmarshalJSON(t *testing.T) {
Value Int `json:"value"`
}{}

if err := json.Unmarshal(tt.buf.Bytes(), &str); err != nil {
if err := json.Unmarshal(tt.buf.Bytes(), &str); !typeMatch(tt.expectErr, err) {
t.Fatalf("unexpected unmarshaling error: %s", err)
}

Expand All @@ -55,16 +67,18 @@ func TestInt_UnmarshalJSON(t *testing.T) {

func TestIntSlice_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
buf *bytes.Buffer
expect IntSlice
name string
buf *bytes.Buffer
expect IntSlice
expectErr error
}{
{
name: "null value",
buf: bytes.NewBufferString(`{"value":null}`),
expect: IntSlice{
Present: true,
},
expectErr: nil,
},
{
name: "valid value",
Expand All @@ -74,11 +88,21 @@ func TestIntSlice_UnmarshalJSON(t *testing.T) {
Valid: true,
Value: []int64{1, 2},
},
expectErr: nil,
},
{
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: IntSlice{},
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: IntSlice{},
expectErr: nil,
},
{
name: "unmarshallable",
buf: bytes.NewBufferString(`{"value":"wat"}`),
expect: IntSlice{
Present: true,
},
expectErr: &json.UnmarshalTypeError{},
},
}
for _, tt := range tests {
Expand All @@ -87,7 +111,7 @@ func TestIntSlice_UnmarshalJSON(t *testing.T) {
Value IntSlice `json:"value"`
}{}

if err := json.Unmarshal(tt.buf.Bytes(), &str); err != nil {
if err := json.Unmarshal(tt.buf.Bytes(), &str); !typeMatch(tt.expectErr, err) {
t.Fatalf("unexpected unmarshaling error: %s", err)
}

Expand Down
Loading

0 comments on commit c6a345d

Please sign in to comment.