Skip to content

Commit

Permalink
test: improve Capture test
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Nov 23, 2021
1 parent 526d06e commit fd3d7dd
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions dec_capture_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jx

import (
"strings"
"bytes"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -84,6 +84,11 @@ func BenchmarkIterator_Skip(b *testing.B) {
}

func TestDecoder_Capture(t *testing.T) {
strs := []string{
"foo",
"bar",
"baz",
}
test := func(i *Decoder) func(t *testing.T) {
return func(t *testing.T) {
var elems int
Expand All @@ -96,15 +101,39 @@ func TestDecoder_Capture(t *testing.T) {
t.Fatal(err)
}
require.Equal(t, Array, i.Next())
require.Equal(t, 3, elems)
require.Equal(t, 6, elems)
t.Run("Nil", func(t *testing.T) {
require.NoError(t, i.Capture(nil))
require.Equal(t, Array, i.Next())
})

idx := 0
require.NoError(t, i.Arr(func(d *Decoder) error {
v, err := d.Str()
if err != nil {
return err
}
require.Equal(t, strs[idx%len(strs)], v)

idx++
return nil
}))
}
}

testData := `["foo", "bar", "baz"]`
t.Run("Str", test(DecodeStr(testData)))
t.Run("Reader", test(Decode(strings.NewReader(testData), 0)))
var e Encoder
e.ArrStart()
for i := 0; i < 6; i++ {
e.Str(strs[i%len(strs)])
}
e.ArrEnd()
testData := e.Bytes()

t.Run("Str", test(DecodeBytes(testData)))
// Check that we get correct result even if buffer smaller than captured data.
decoder := Decoder{
reader: bytes.NewReader(testData),
buf: make([]byte, 8),
}
t.Run("Reader", test(&decoder))
}

0 comments on commit fd3d7dd

Please sign in to comment.