...
1 package misc_tests
2
3 import (
4 "bytes"
5 "io"
6 "testing"
7
8 "github.com/json-iterator/go"
9 "github.com/stretchr/testify/require"
10 )
11
12 func Test_read_null(t *testing.T) {
13 should := require.New(t)
14 iter := jsoniter.ParseString(jsoniter.ConfigDefault, `null`)
15 should.True(iter.ReadNil())
16 iter = jsoniter.ParseString(jsoniter.ConfigDefault, `null`)
17 should.Nil(iter.Read())
18 iter = jsoniter.ParseString(jsoniter.ConfigDefault, `navy`)
19 iter.Read()
20 should.True(iter.Error != nil && iter.Error != io.EOF)
21 iter = jsoniter.ParseString(jsoniter.ConfigDefault, `navy`)
22 iter.ReadNil()
23 should.True(iter.Error != nil && iter.Error != io.EOF)
24 }
25
26 func Test_write_null(t *testing.T) {
27 should := require.New(t)
28 buf := &bytes.Buffer{}
29 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
30 stream.WriteNil()
31 stream.Flush()
32 should.Nil(stream.Error)
33 should.Equal("null", buf.String())
34 }
35
36 func Test_decode_null_object_field(t *testing.T) {
37 should := require.New(t)
38 iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`)
39 iter.ReadArray()
40 if iter.ReadObject() != "" {
41 t.FailNow()
42 }
43 iter.ReadArray()
44 if iter.ReadString() != "a" {
45 t.FailNow()
46 }
47 type TestObject struct {
48 Field string
49 }
50 objs := []TestObject{}
51 should.Nil(jsoniter.UnmarshalFromString("[null]", &objs))
52 should.Len(objs, 1)
53 }
54
55 func Test_decode_null_array_element(t *testing.T) {
56 should := require.New(t)
57 iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`)
58 should.True(iter.ReadArray())
59 should.True(iter.ReadNil())
60 should.True(iter.ReadArray())
61 should.Equal("a", iter.ReadString())
62 }
63
64 func Test_decode_null_string(t *testing.T) {
65 should := require.New(t)
66 iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`)
67 should.True(iter.ReadArray())
68 should.Equal("", iter.ReadString())
69 should.True(iter.ReadArray())
70 should.Equal("a", iter.ReadString())
71 }
72
73 func Test_decode_null_skip(t *testing.T) {
74 iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`)
75 iter.ReadArray()
76 iter.Skip()
77 iter.ReadArray()
78 if iter.ReadString() != "a" {
79 t.FailNow()
80 }
81 }
82
View as plain text