1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-30 08:36:43 +02:00
json-iterator/jsoniter_null_test.go

83 lines
1.7 KiB
Go
Raw Normal View History

2016-12-02 05:22:20 +02:00
package jsoniter
import (
"testing"
2017-01-07 06:28:16 +02:00
"github.com/json-iterator/go/require"
"bytes"
2016-12-02 05:22:20 +02:00
)
2017-01-21 10:09:38 +02:00
func Test_read_null(t *testing.T) {
should := require.New(t)
2016-12-02 05:22:20 +02:00
iter := ParseString(`null`)
2017-01-21 10:09:38 +02:00
should.True(iter.ReadNil())
iter = ParseString(`null`)
should.Nil(iter.Read())
2017-01-22 17:29:48 +02:00
iter = ParseString(`null`)
any, err := UnmarshalAnyFromString(`null`)
should.Nil(err)
should.Equal(0, any.ToInt())
should.Equal(float64(0), any.ToFloat64())
should.Equal("", any.ToString())
should.False(any.ToBool())
2016-12-02 05:22:20 +02:00
}
2017-01-07 06:28:16 +02:00
func Test_write_null(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
2017-01-18 17:33:40 +02:00
stream.WriteNil()
2017-01-07 06:28:16 +02:00
stream.Flush()
should.Nil(stream.Error)
should.Equal("null", buf.String())
}
func Test_encode_null(t *testing.T) {
should := require.New(t)
str, err := MarshalToString(nil)
should.Nil(err)
should.Equal("null", str)
}
func Test_decode_null_object(t *testing.T) {
2016-12-02 05:22:20 +02:00
iter := ParseString(`[null,"a"]`)
iter.ReadArray()
if iter.ReadObject() != "" {
t.FailNow()
}
iter.ReadArray()
if iter.ReadString() != "a" {
t.FailNow()
}
}
2017-01-07 06:28:16 +02:00
func Test_decode_null_array(t *testing.T) {
2016-12-02 05:22:20 +02:00
iter := ParseString(`[null,"a"]`)
iter.ReadArray()
if iter.ReadArray() != false {
t.FailNow()
}
iter.ReadArray()
if iter.ReadString() != "a" {
t.FailNow()
}
}
2017-01-07 06:28:16 +02:00
func Test_decode_null_string(t *testing.T) {
2017-01-18 17:33:40 +02:00
should := require.New(t)
2016-12-02 05:22:20 +02:00
iter := ParseString(`[null,"a"]`)
2017-01-18 17:33:40 +02:00
should.True(iter.ReadArray())
should.True(iter.ReadNil())
should.True(iter.ReadArray())
should.Equal("a", iter.ReadString())
2016-12-02 05:22:20 +02:00
}
2017-01-07 06:28:16 +02:00
func Test_decode_null_skip(t *testing.T) {
2016-12-02 05:22:20 +02:00
iter := ParseString(`[null,"a"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
if iter.ReadString() != "a" {
t.FailNow()
}
}