From 1535c41c06f57af3dc3f2f8e1649dfed5a13f438 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 21 Mar 2022 15:26:02 +0300 Subject: [PATCH] test: improve integer test table, add more cases --- dec_int_test.go | 2 +- dec_test.go | 31 +++++++------ int_test.go | 119 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 109 insertions(+), 43 deletions(-) diff --git a/dec_int_test.go b/dec_int_test.go index f2b6209..5b7ccb0 100644 --- a/dec_int_test.go +++ b/dec_int_test.go @@ -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) { diff --git a/dec_test.go b/dec_test.go index 38e2cb7..9190c70 100644 --- a/dec_test.go +++ b/dec_test.go @@ -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() { @@ -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)) } } diff --git a/int_test.go b/int_test.go index 79fe90f..274e793 100644 --- a/int_test.go +++ b/int_test.go @@ -1,11 +1,11 @@ package jx import ( - "bytes" "fmt" "io" "math" "strconv" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -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 + }) + })) } } @@ -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 + }) + })) } }