1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-23 18:54:21 +02:00
json-iterator/jsoniter_null_test.go

78 lines
1.4 KiB
Go
Raw Normal View History

2016-12-02 11:22:20 +08:00
package jsoniter
import (
"testing"
2017-01-07 12:28:16 +08:00
"github.com/json-iterator/go/require"
"bytes"
2016-12-02 11:22:20 +08:00
)
2017-01-07 12:28:16 +08:00
func Test_decode_null(t *testing.T) {
2016-12-02 11:22:20 +08:00
iter := ParseString(`null`)
if iter.ReadNil() != true {
2016-12-02 11:22:20 +08:00
t.FailNow()
}
}
2017-01-07 12:28:16 +08:00
func Test_write_null(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream.WriteNull()
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 11:22:20 +08:00
iter := ParseString(`[null,"a"]`)
iter.ReadArray()
if iter.ReadObject() != "" {
t.FailNow()
}
iter.ReadArray()
if iter.ReadString() != "a" {
t.FailNow()
}
}
2017-01-07 12:28:16 +08:00
func Test_decode_null_array(t *testing.T) {
2016-12-02 11:22:20 +08:00
iter := ParseString(`[null,"a"]`)
iter.ReadArray()
if iter.ReadArray() != false {
t.FailNow()
}
iter.ReadArray()
if iter.ReadString() != "a" {
t.FailNow()
}
}
2017-01-07 12:28:16 +08:00
func Test_decode_null_string(t *testing.T) {
2016-12-02 11:22:20 +08:00
iter := ParseString(`[null,"a"]`)
iter.ReadArray()
if iter.ReadString() != "" {
t.FailNow()
}
iter.ReadArray()
if iter.ReadString() != "a" {
t.FailNow()
}
}
2017-01-07 12:28:16 +08:00
func Test_decode_null_skip(t *testing.T) {
2016-12-02 11:22:20 +08:00
iter := ParseString(`[null,"a"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
if iter.ReadString() != "a" {
t.FailNow()
}
}