UnmarshalJSON returns an error for invalid zero.* types, tests

This commit is contained in:
Greg 2014-09-09 12:31:58 +09:00
parent b550c62e10
commit 214012c56c
13 changed files with 72 additions and 0 deletions

View file

@ -46,6 +46,13 @@ func TestUnmarshalBool(t *testing.T) {
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullBool(t, null, "null json")
var badType Bool
err = json.Unmarshal(intJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullBool(t, badType, "wrong type json")
}
func TestTextUnmarshalBool(t *testing.T) {

View file

@ -45,6 +45,13 @@ func TestUnmarshalFloat(t *testing.T) {
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullFloat(t, null, "null json")
var badType Float
err = json.Unmarshal(boolJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullFloat(t, badType, "wrong type json")
}
func TestTextUnmarshalFloat(t *testing.T) {

BIN
gin-bin

Binary file not shown.

View file

@ -45,6 +45,13 @@ func TestUnmarshalInt(t *testing.T) {
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullInt(t, null, "null json")
var badType Int
err = json.Unmarshal(boolJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullInt(t, badType, "wrong type json")
}
func TestTextUnmarshalInt(t *testing.T) {

View file

@ -56,6 +56,13 @@ func TestUnmarshalString(t *testing.T) {
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullStr(t, null, "null json")
var badType String
err = json.Unmarshal(boolJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullStr(t, badType, "wrong type json")
}
func TestTextUnmarshalString(t *testing.T) {

View file

@ -4,6 +4,8 @@ import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"reflect"
)
// Bool is a nullable bool.
@ -49,6 +51,8 @@ func (b *Bool) UnmarshalJSON(data []byte) error {
case nil:
b.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Bool", reflect.TypeOf(v).Name())
}
b.Valid = (err == nil) && b.Bool
return err

View file

@ -59,6 +59,13 @@ func TestUnmarshalBool(t *testing.T) {
panic("err should not be nil")
}
assertNullBool(t, invalid, "invalid json")
var badType Bool
err = json.Unmarshal(intJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullBool(t, badType, "wrong type json")
}
func TestTextUnmarshalBool(t *testing.T) {

View file

@ -3,6 +3,8 @@ package zero
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
"strconv"
)
@ -50,6 +52,8 @@ func (f *Float) UnmarshalJSON(data []byte) error {
case nil:
f.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Float", reflect.TypeOf(v).Name())
}
f.Valid = (err == nil) && (f.Float64 != 0)
return err

View file

@ -50,6 +50,13 @@ func TestUnmarshalFloat(t *testing.T) {
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullFloat(t, null, "null json")
var badType Float
err = json.Unmarshal(boolJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullFloat(t, badType, "wrong type json")
}
func TestTextUnmarshalFloat(t *testing.T) {

View file

@ -3,6 +3,8 @@ package zero
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
"strconv"
)
@ -51,6 +53,8 @@ func (i *Int) UnmarshalJSON(data []byte) error {
case nil:
i.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Int", reflect.TypeOf(v).Name())
}
i.Valid = (err == nil) && (i.Int64 != 0)
return err

View file

@ -51,6 +51,13 @@ func TestUnmarshalInt(t *testing.T) {
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullInt(t, null, "null json")
var badType Int
err = json.Unmarshal(boolJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullInt(t, badType, "wrong type json")
}
func TestTextUnmarshalInt(t *testing.T) {

View file

@ -7,6 +7,8 @@ package zero
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
)
// String is a nullable string.
@ -53,6 +55,8 @@ func (s *String) UnmarshalJSON(data []byte) error {
case nil:
s.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.String", reflect.TypeOf(v).Name())
}
s.Valid = (err == nil) && (s.String != "")
return err

View file

@ -44,6 +44,13 @@ func TestUnmarshalString(t *testing.T) {
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullStr(t, null, "null json")
var badType String
err = json.Unmarshal(boolJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullStr(t, badType, "wrong type json")
}
func TestTextUnmarshalString(t *testing.T) {