Skip to content

Commit

Permalink
add reader test for struct
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosnake0 committed Mar 22, 2021
1 parent 385ec80 commit e7412dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
19 changes: 19 additions & 0 deletions iterator_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ import (
"github.com/stretchr/testify/require"
)

func TestIterator_readObjectFieldAsSlice(t *testing.T) {
t.Run("stream reading", func(t *testing.T) {
withIterator("", func(it *Iterator) {
for step := 1; step < 8; step++ {
it.Reset(&stepByteReader{
b: `{"a":1}`,
step: step, // consume the entire field A
})
var s struct {
A int `json:"a"`
}
err := it.ReadVal(&s)
require.NoError(t, err)
require.Equal(t, 1, s.A)
}
})
})
}

func TestIterator_Object_ReadObjectBegin(t *testing.T) {
t.Run("eof", func(t *testing.T) {
withIterator("", func(it *Iterator) {
Expand Down
22 changes: 12 additions & 10 deletions iterator_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,19 @@ func TestIterator_Str_readStringAsSlice(t *testing.T) {
})
t.Run("reader 2", func(t *testing.T) {
withIterator("", func(it *Iterator) {
it.Reset(&stepByteReader{
b: `"abc"`,
step: 2,
})
vt, err := it.NextValueType()
require.NoError(t, err)
require.Equal(t, StringValue, vt)
for step := 2; step < 6; step++ {
it.Reset(&stepByteReader{
b: `"abc"`,
step: step,
})
vt, err := it.NextValueType()
require.NoError(t, err)
require.Equal(t, StringValue, vt)

s, err := it.ReadString()
require.NoError(t, err)
require.Equal(t, "abc", s)
s, err := it.ReadString()
require.NoError(t, err)
require.Equal(t, "abc", s)
}
})
})
}
Expand Down

0 comments on commit e7412dd

Please sign in to comment.