2018-02-14 04:13:34 +02:00
|
|
|
//+build jsoniter_sloppy
|
2017-07-18 03:01:43 +02:00
|
|
|
|
2016-12-06 17:31:11 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2017-07-07 03:13:25 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-06 17:31:11 +02:00
|
|
|
"io"
|
2017-01-05 15:23:08 +02:00
|
|
|
"testing"
|
2016-12-06 17:31:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_string_end(t *testing.T) {
|
2017-06-17 04:21:37 +02:00
|
|
|
end, escaped := ParseString(ConfigDefault, `abc"`).findStringEnd()
|
2016-12-06 17:31:11 +02:00
|
|
|
if end != 4 {
|
|
|
|
t.Fatal(end)
|
|
|
|
}
|
|
|
|
if escaped != false {
|
|
|
|
t.Fatal(escaped)
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
end, escaped = ParseString(ConfigDefault, `abc\\"`).findStringEnd()
|
2016-12-06 17:31:11 +02:00
|
|
|
if end != 6 {
|
|
|
|
t.Fatal(end)
|
|
|
|
}
|
|
|
|
if escaped != true {
|
|
|
|
t.Fatal(escaped)
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
end, escaped = ParseString(ConfigDefault, `abc\\\\"`).findStringEnd()
|
2016-12-06 17:31:11 +02:00
|
|
|
if end != 8 {
|
|
|
|
t.Fatal(end)
|
|
|
|
}
|
|
|
|
if escaped != true {
|
|
|
|
t.Fatal(escaped)
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
end, escaped = ParseString(ConfigDefault, `abc\"`).findStringEnd()
|
2016-12-06 17:31:11 +02:00
|
|
|
if end != -1 {
|
|
|
|
t.Fatal(end)
|
|
|
|
}
|
|
|
|
if escaped != false {
|
|
|
|
t.Fatal(escaped)
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
end, escaped = ParseString(ConfigDefault, `abc\`).findStringEnd()
|
2016-12-06 17:31:11 +02:00
|
|
|
if end != -1 {
|
|
|
|
t.Fatal(end)
|
|
|
|
}
|
|
|
|
if escaped != true {
|
|
|
|
t.Fatal(escaped)
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
end, escaped = ParseString(ConfigDefault, `abc\\`).findStringEnd()
|
2016-12-06 17:31:11 +02:00
|
|
|
if end != -1 {
|
|
|
|
t.Fatal(end)
|
|
|
|
}
|
|
|
|
if escaped != false {
|
|
|
|
t.Fatal(escaped)
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
end, escaped = ParseString(ConfigDefault, `\\`).findStringEnd()
|
2016-12-06 17:31:11 +02:00
|
|
|
if end != -1 {
|
|
|
|
t.Fatal(end)
|
|
|
|
}
|
|
|
|
if escaped != false {
|
|
|
|
t.Fatal(escaped)
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
end, escaped = ParseString(ConfigDefault, `\`).findStringEnd()
|
2016-12-06 17:31:11 +02:00
|
|
|
if end != -1 {
|
|
|
|
t.Fatal(end)
|
|
|
|
}
|
|
|
|
if escaped != true {
|
|
|
|
t.Fatal(escaped)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type StagedReader struct {
|
|
|
|
r1 string
|
|
|
|
r2 string
|
|
|
|
r3 string
|
|
|
|
r int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reader *StagedReader) Read(p []byte) (n int, err error) {
|
|
|
|
reader.r++
|
|
|
|
switch reader.r {
|
|
|
|
case 1:
|
|
|
|
copy(p, []byte(reader.r1))
|
|
|
|
return len(reader.r1), nil
|
|
|
|
case 2:
|
|
|
|
copy(p, []byte(reader.r2))
|
|
|
|
return len(reader.r2), nil
|
|
|
|
case 3:
|
|
|
|
copy(p, []byte(reader.r3))
|
|
|
|
return len(reader.r3), nil
|
|
|
|
default:
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_skip_string(t *testing.T) {
|
2017-02-12 16:49:45 +02:00
|
|
|
should := require.New(t)
|
2017-06-17 04:21:37 +02:00
|
|
|
iter := ParseString(ConfigDefault, `"abc`)
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipString()
|
2017-02-12 16:49:45 +02:00
|
|
|
should.Equal(1, iter.head)
|
2017-06-17 04:21:37 +02:00
|
|
|
iter = ParseString(ConfigDefault, `\""abc`)
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipString()
|
2017-02-12 16:49:45 +02:00
|
|
|
should.Equal(3, iter.head)
|
2016-12-06 17:31:11 +02:00
|
|
|
reader := &StagedReader{
|
|
|
|
r1: `abc`,
|
|
|
|
r2: `"`,
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
iter = Parse(ConfigDefault, reader, 4096)
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipString()
|
2017-02-12 16:49:45 +02:00
|
|
|
should.Equal(1, iter.head)
|
2016-12-06 17:31:11 +02:00
|
|
|
reader = &StagedReader{
|
|
|
|
r1: `abc`,
|
|
|
|
r2: `1"`,
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
iter = Parse(ConfigDefault, reader, 4096)
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipString()
|
2017-02-12 16:49:45 +02:00
|
|
|
should.Equal(2, iter.head)
|
2016-12-06 17:31:11 +02:00
|
|
|
reader = &StagedReader{
|
|
|
|
r1: `abc\`,
|
|
|
|
r2: `"`,
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
iter = Parse(ConfigDefault, reader, 4096)
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipString()
|
2017-02-12 16:49:45 +02:00
|
|
|
should.NotNil(iter.Error)
|
2016-12-06 17:31:11 +02:00
|
|
|
reader = &StagedReader{
|
|
|
|
r1: `abc\`,
|
|
|
|
r2: `""`,
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
iter = Parse(ConfigDefault, reader, 4096)
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipString()
|
2017-02-12 16:49:45 +02:00
|
|
|
should.Equal(2, iter.head)
|
2016-12-06 17:31:11 +02:00
|
|
|
}
|
2016-12-06 17:51:29 +02:00
|
|
|
|
|
|
|
func Test_skip_object(t *testing.T) {
|
2017-06-17 04:21:37 +02:00
|
|
|
iter := ParseString(ConfigDefault, `}`)
|
2016-12-06 17:51:29 +02:00
|
|
|
iter.skipObject()
|
|
|
|
if iter.head != 1 {
|
|
|
|
t.Fatal(iter.head)
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
iter = ParseString(ConfigDefault, `a}`)
|
2016-12-06 17:51:29 +02:00
|
|
|
iter.skipObject()
|
|
|
|
if iter.head != 2 {
|
|
|
|
t.Fatal(iter.head)
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
iter = ParseString(ConfigDefault, `{}}a`)
|
2016-12-06 17:51:29 +02:00
|
|
|
iter.skipObject()
|
|
|
|
if iter.head != 3 {
|
|
|
|
t.Fatal(iter.head)
|
|
|
|
}
|
|
|
|
reader := &StagedReader{
|
|
|
|
r1: `{`,
|
|
|
|
r2: `}}a`,
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
iter = Parse(ConfigDefault, reader, 4096)
|
2016-12-06 17:51:29 +02:00
|
|
|
iter.skipObject()
|
|
|
|
if iter.head != 2 {
|
|
|
|
t.Fatal(iter.head)
|
|
|
|
}
|
2017-06-17 04:21:37 +02:00
|
|
|
iter = ParseString(ConfigDefault, `"}"}a`)
|
2016-12-06 17:51:29 +02:00
|
|
|
iter.skipObject()
|
|
|
|
if iter.head != 4 {
|
|
|
|
t.Fatal(iter.head)
|
|
|
|
}
|
|
|
|
}
|