go-json/decode_test.go

121 lines
2.9 KiB
Go
Raw Normal View History

2020-04-22 07:06:52 +03:00
package json_test
import (
"testing"
"github.com/goccy/go-json"
)
func Test_Decoder(t *testing.T) {
2020-04-24 10:10:07 +03:00
t.Run("int", func(t *testing.T) {
var v int
assertErr(t, json.Unmarshal([]byte(`-1`), &v))
assertEq(t, "int", int(-1), v)
})
t.Run("int8", func(t *testing.T) {
var v int8
assertErr(t, json.Unmarshal([]byte(`-2`), &v))
assertEq(t, "int8", int8(-2), v)
})
t.Run("int16", func(t *testing.T) {
var v int16
assertErr(t, json.Unmarshal([]byte(`-3`), &v))
assertEq(t, "int16", int16(-3), v)
})
t.Run("int32", func(t *testing.T) {
var v int32
assertErr(t, json.Unmarshal([]byte(`-4`), &v))
assertEq(t, "int32", int32(-4), v)
})
t.Run("int64", func(t *testing.T) {
var v int64
assertErr(t, json.Unmarshal([]byte(`-5`), &v))
assertEq(t, "int64", int64(-5), v)
})
t.Run("uint", func(t *testing.T) {
var v uint
assertErr(t, json.Unmarshal([]byte(`1`), &v))
assertEq(t, "uint", uint(1), v)
})
t.Run("uint8", func(t *testing.T) {
var v uint8
assertErr(t, json.Unmarshal([]byte(`2`), &v))
assertEq(t, "uint8", uint8(2), v)
})
t.Run("uint16", func(t *testing.T) {
var v uint16
assertErr(t, json.Unmarshal([]byte(`3`), &v))
assertEq(t, "uint16", uint16(3), v)
})
t.Run("uint32", func(t *testing.T) {
var v uint32
assertErr(t, json.Unmarshal([]byte(`4`), &v))
assertEq(t, "uint32", uint32(4), v)
})
t.Run("uint64", func(t *testing.T) {
var v uint64
assertErr(t, json.Unmarshal([]byte(`5`), &v))
assertEq(t, "uint64", uint64(5), v)
})
t.Run("bool", func(t *testing.T) {
t.Run("true", func(t *testing.T) {
var v bool
assertErr(t, json.Unmarshal([]byte(`true`), &v))
assertEq(t, "bool", true, v)
})
t.Run("false", func(t *testing.T) {
v := true
assertErr(t, json.Unmarshal([]byte(`false`), &v))
assertEq(t, "bool", false, v)
})
})
t.Run("string", func(t *testing.T) {
var v string
assertErr(t, json.Unmarshal([]byte(`"hello"`), &v))
assertEq(t, "string", "hello", v)
})
2020-04-24 10:46:12 +03:00
t.Run("float32", func(t *testing.T) {
2020-04-24 14:55:21 +03:00
var v struct {
F float32
}
assertErr(t, json.Unmarshal([]byte(`{"f": 3.14}`), &v))
assertEq(t, "float32", float32(3.14), v.F)
2020-04-24 10:46:12 +03:00
})
t.Run("float64", func(t *testing.T) {
var v float64
assertErr(t, json.Unmarshal([]byte(`3.14`), &v))
assertEq(t, "float64", float64(3.14), v)
})
2020-04-22 07:06:52 +03:00
t.Run("struct", func(t *testing.T) {
2020-04-23 19:39:20 +03:00
type T struct {
AA int `json:"aa"`
BB string `json:"bb"`
CC bool `json:"cc"`
}
2020-04-22 07:06:52 +03:00
var v struct {
2020-04-22 11:59:01 +03:00
A int `json:"abcd"`
B string `json:"str"`
2020-04-22 12:51:42 +03:00
C bool
2020-04-23 19:39:20 +03:00
D *T
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
content := []byte(`
{
"abcd": 123,
"str" : "hello",
"c" : true,
"d" : {
"aa": 2,
"bb": "world",
"cc": true
}
}`)
assertErr(t, json.Unmarshal(content, &v))
2020-04-22 11:59:01 +03:00
assertEq(t, "struct.A", 123, v.A)
assertEq(t, "struct.B", "hello", v.B)
2020-04-22 12:51:42 +03:00
assertEq(t, "struct.C", true, v.C)
2020-04-23 19:39:20 +03:00
assertEq(t, "struct.D.AA", 2, v.D.AA)
2020-04-24 10:10:07 +03:00
assertEq(t, "struct.D.BB", "world", v.D.BB)
assertEq(t, "struct.D.CC", true, v.D.CC)
2020-04-22 07:06:52 +03:00
})
}