Skip to content

Commit

Permalink
test: use testBufferReader helper everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Jun 14, 2022
1 parent 7d58fbb commit 08e64ce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 69 deletions.
66 changes: 26 additions & 40 deletions dec_skip_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"reflect"
"strings"
"testing"
"testing/iotest"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -477,49 +476,36 @@ func TestDecoder_Skip(t *testing.T) {
inputs: testObjs,
})

testDecode := func(iter *Decoder, input string, stdErr error) func(t *testing.T) {
return func(t *testing.T) {
t.Cleanup(func() {
if t.Failed() {
t.Logf("Input: %q", input)
}
})

should := require.New(t)
if stdErr == nil {
should.NoError(iter.Skip())
should.ErrorIs(iter.Null(), io.ErrUnexpectedEOF)
} else {
should.Error(func() error {
if err := iter.Skip(); err != nil {
return err
}
if err := iter.Skip(); err != io.EOF {
return err
}
return nil
}())
}
}
}
for _, testCase := range testCases {
valType := reflect.TypeOf(testCase.ptr).Elem()
t.Run(valType.Kind().String(), func(t *testing.T) {
for inputIdx, input := range testCase.inputs {
t.Run(fmt.Sprintf("Test%d", inputIdx), func(t *testing.T) {
ptrVal := reflect.New(valType)
stdErr := json.Unmarshal([]byte(input), ptrVal.Interface())

t.Run("Buffer", testDecode(DecodeStr(input), input, stdErr))

r := strings.NewReader(input)
d := Decode(r, 512)
t.Run("Reader", testDecode(d, input, stdErr))

r.Reset(input)
obr := iotest.OneByteReader(r)
t.Run("OneByteReader", testDecode(Decode(obr, 512), input, stdErr))
})
input := input
stdErr := json.Unmarshal([]byte(input), reflect.New(valType).Interface())
cb := func(t *testing.T, iter *Decoder) {
t.Cleanup(func() {
if t.Failed() {
t.Logf("Input: %q", input)
}
})

should := require.New(t)
if stdErr == nil {
should.NoError(iter.Skip())
should.ErrorIs(iter.Null(), io.ErrUnexpectedEOF)
} else {
should.Error(func() error {
if err := iter.Skip(); err != nil {
return err
}
if err := iter.Skip(); err != io.EOF {
return err
}
return nil
}())
}
}
t.Run(fmt.Sprintf("Test%d", inputIdx), testBufferReader(input, cb))
}
})
}
Expand Down
42 changes: 13 additions & 29 deletions dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,21 @@ func testBufferReader(input string, cb func(t *testing.T, d *Decoder)) func(t *t
}

func createTestCase(input string, cb func(t *testing.T, d *Decoder) error) func(t *testing.T) {
run := func(d *Decoder, input string, valid bool) func(t *testing.T) {
return func(t *testing.T) {
t.Cleanup(func() {
if t.Failed() {
t.Logf("Input: %q", input)
}
})

err := cb(t, d)
if valid {
require.NoError(t, err)
} else {
require.Error(t, err)
valid := json.Valid([]byte(input))
return testBufferReader(input, func(t *testing.T, d *Decoder) {
t.Cleanup(func() {
if t.Failed() {
t.Logf("Input: %q", input)
}
}
}

return func(t *testing.T) {
valid := json.Valid([]byte(input))

t.Run("Buffer", run(DecodeStr(input), input, valid))

r := strings.NewReader(input)
d := Decode(r, 512)
t.Run("Reader", run(d, input, valid))
})

r.Reset(input)
obr := iotest.OneByteReader(r)
d.Reset(obr)
t.Run("OneByteReader", run(d, input, valid))
}
err := cb(t, d)
if valid {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}

func runTestCases(t *testing.T, cases []string, cb func(t *testing.T, d *Decoder) error) {
Expand Down

0 comments on commit 08e64ce

Please sign in to comment.