1
0
mirror of https://github.com/json-iterator/go.git synced 2025-02-01 19:14:29 +02:00
json-iterator/jsoniter_io_test.go

66 lines
1.2 KiB
Go
Raw Normal View History

2016-12-01 10:35:38 +08:00
package jsoniter
import (
"bytes"
2017-07-09 16:09:23 +08:00
"github.com/stretchr/testify/require"
2016-12-01 10:35:38 +08:00
"io"
"testing"
2016-12-01 10:35:38 +08:00
)
func Test_read_by_one(t *testing.T) {
2017-06-17 10:21:37 +08:00
iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 1)
2016-12-01 10:35:38 +08:00
b := iter.readByte()
if iter.Error != nil {
t.Fatal(iter.Error)
}
if b != 'a' {
t.Fatal(b)
}
iter.unreadByte()
if iter.Error != nil {
t.Fatal(iter.Error)
}
b = iter.readByte()
if iter.Error != nil {
t.Fatal(iter.Error)
}
if b != 'a' {
t.Fatal(b)
}
}
func Test_read_by_two(t *testing.T) {
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 2)
2016-12-01 10:35:38 +08:00
b := iter.readByte()
should.Nil(iter.Error)
should.Equal(byte('a'), b)
2016-12-01 10:35:38 +08:00
b = iter.readByte()
should.Nil(iter.Error)
should.Equal(byte('b'), b)
2016-12-01 10:35:38 +08:00
iter.unreadByte()
should.Nil(iter.Error)
2016-12-01 10:35:38 +08:00
iter.unreadByte()
should.Nil(iter.Error)
2016-12-01 10:35:38 +08:00
b = iter.readByte()
should.Nil(iter.Error)
should.Equal(byte('a'), b)
2016-12-01 10:35:38 +08:00
}
func Test_read_until_eof(t *testing.T) {
2017-06-17 10:21:37 +08:00
iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 2)
2016-12-01 10:35:38 +08:00
iter.readByte()
iter.readByte()
b := iter.readByte()
if iter.Error != nil {
t.Fatal(iter.Error)
}
if b != 'c' {
t.Fatal(b)
}
iter.readByte()
if iter.Error != io.EOF {
t.Fatal(iter.Error)
}
}