Skip to content

Commit

Permalink
test: improve integer test table, add more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Mar 21, 2022
1 parent eb5ab64 commit 1535c41
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 43 deletions.
2 changes: 1 addition & 1 deletion dec_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestDecoder_Int(t *testing.T) {
return &Decoder{
buf: []byte{'1', '2'},
tail: 2,
reader: errReader{},
reader: r,
}
}
t.Run("32", func(t *testing.T) {
Expand Down
31 changes: 18 additions & 13 deletions dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/stretchr/testify/require"
)

func runTestCases(t *testing.T, cases []string, cb func(t *testing.T, d *Decoder) error) {
testCase := func(d *Decoder, input string, valid bool) func(t *testing.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() {
Expand All @@ -29,21 +29,26 @@ func runTestCases(t *testing.T, cases []string, cb func(t *testing.T, d *Decoder
}
}
}
for i, input := range cases {

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

t.Run(fmt.Sprintf("Test%d", i), func(t *testing.T) {
t.Run("Buffer", testCase(DecodeStr(input), input, valid))
t.Run("Buffer", run(DecodeStr(input), input, valid))

r := strings.NewReader(input)
d := Decode(r, 512)
t.Run("Reader", testCase(d, 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", testCase(d, input, valid))
})
r.Reset(input)
obr := iotest.OneByteReader(r)
d.Reset(obr)
t.Run("OneByteReader", run(d, input, valid))
}
}

func runTestCases(t *testing.T, cases []string, cb func(t *testing.T, d *Decoder) error) {
for i, input := range cases {
t.Run(fmt.Sprintf("Test%d", i), createTestCase(input, cb))
}
}

Expand Down
119 changes: 90 additions & 29 deletions int_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package jx

import (
"bytes"
"fmt"
"io"
"math"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -73,26 +73,56 @@ func TestDecoderIntNumbers(t *testing.T) {
}

func TestReadInt32(t *testing.T) {
inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `2147483647`, `-2147483648`}
for _, input := range inputs {
t.Run(input, func(t *testing.T) {
inputs := []string{
`-12`,
`-1`,
`0`,
`1`,
`12`,
`123`,
`1234`,
`12345`,
`123456`,
`1234567`,
`12345678`,
`123456789`,
`1234567890`,
`2147483647`,
`-2147483648`,
}
for i, input := range inputs {
input := input
t.Run(fmt.Sprintf("Test%d", i+1), createTestCase(input, func(t *testing.T, d *Decoder) error {
should := require.New(t)
iter := DecodeStr(input)
expected, err := strconv.ParseInt(input, 10, 32)
should.NoError(err)
v, err := iter.Int32()
v, err := d.Int32()
should.NoError(err)
should.Equal(int32(expected), v)
})
t.Run(input, func(t *testing.T) {
return nil
}))
}

{
input := "[" + strings.Join(inputs, ",") + "]"
t.Run("Array", createTestCase(input, func(t *testing.T, d *Decoder) error {
should := require.New(t)
iter := Decode(bytes.NewBufferString(input), 2)
expected, err := strconv.ParseInt(input, 10, 32)
should.NoError(err)
v, err := iter.Int32()
should.NoError(err)
should.Equal(int32(expected), v)
})
i := 0

return d.Arr(func(d *Decoder) error {
expected, err := strconv.ParseInt(inputs[i], 10, 32)
should.NoError(err)

v, err := d.Int32()
if err != nil {
return err
}
should.Equal(int32(expected), v)

i++
return nil
})
}))
}
}

Expand Down Expand Up @@ -175,26 +205,57 @@ func TestReadInt64Overflow(t *testing.T) {
}

func TestReadInt64(t *testing.T) {
inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `9223372036854775807`, `-9223372036854775808`}
for _, input := range inputs {
t.Run(input, func(t *testing.T) {
inputs := []string{
`-12`,
`-1`,
`0`,
`1`,
`12`,
`123`,
`1234`,
`12345`,
`123456`,
`1234567`,
`12345678`,
`123456789`,
`1234567890`,
`12345678901`,
`9223372036854775807`,
`-9223372036854775808`,
}
for i, input := range inputs {
input := input
t.Run(fmt.Sprintf("Test%d", i+1), createTestCase(input, func(t *testing.T, d *Decoder) error {
should := require.New(t)
iter := DecodeStr(input)
expected, err := strconv.ParseInt(input, 10, 64)
should.NoError(err)
v, err := iter.Int64()
v, err := d.Int64()
should.NoError(err)
should.Equal(expected, v)
})
t.Run(input, func(t *testing.T) {
return nil
}))
}

{
input := "[" + strings.Join(inputs, ",") + "]"
t.Run("Array", createTestCase(input, func(t *testing.T, d *Decoder) error {
should := require.New(t)
iter := Decode(bytes.NewBufferString(input), 2)
expected, err := strconv.ParseInt(input, 10, 64)
should.NoError(err)
v, err := iter.Int64()
should.NoError(err)
should.Equal(expected, v)
})
i := 0

return d.Arr(func(d *Decoder) error {
expected, err := strconv.ParseInt(inputs[i], 10, 64)
should.NoError(err)

v, err := d.Int64()
if err != nil {
return err
}
should.Equal(expected, v)

i++
return nil
})
}))
}
}

Expand Down

0 comments on commit 1535c41

Please sign in to comment.