Skip to content

Commit

Permalink
chore: rename tests to follow Go convention
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Mar 11, 2022
1 parent e317317 commit 6782d83
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 67 deletions.
6 changes: 3 additions & 3 deletions bool_test.go
Expand Up @@ -6,19 +6,19 @@ import (
"github.com/stretchr/testify/require"
)

func Test_true(t *testing.T) {
func TestTrue(t *testing.T) {
should := require.New(t)
iter := DecodeStr(`true`)
should.True(iter.Bool())
}

func Test_false(t *testing.T) {
func TestFalse(t *testing.T) {
should := require.New(t)
iter := DecodeStr(`false`)
should.False(iter.Bool())
}

func Test_write_true_false(t *testing.T) {
func TestWriteTrueFalse(t *testing.T) {
should := require.New(t)
w := GetEncoder()
w.Bool(true)
Expand Down
1 change: 1 addition & 0 deletions dec_skip_cases_test.go
Expand Up @@ -119,6 +119,7 @@ var testNumbers = append([]string{
"\n9223372036854775807", // valid
"\n9223372036854775808", // valid
"\n9223372036854775807.1", // valid
"-12.000000", // valid
}, []string{
// Test cases from strconv.

Expand Down
110 changes: 63 additions & 47 deletions float_test.go
Expand Up @@ -15,7 +15,7 @@ import (
// epsilon to compare floats.
const epsilon = 1e-6

func Test_read_big_float(t *testing.T) {
func TestReadBigFloat(t *testing.T) {
should := require.New(t)
r := DecodeStr(`12.3`)
val, err := r.BigFloat()
Expand All @@ -24,7 +24,7 @@ func Test_read_big_float(t *testing.T) {
should.Equal(12.3, val64)
}

func Test_read_big_int(t *testing.T) {
func TestReadBigInt(t *testing.T) {
should := require.New(t)
iter := DecodeStr(`92233720368547758079223372036854775807`)
val, err := iter.BigInt()
Expand All @@ -33,7 +33,7 @@ func Test_read_big_int(t *testing.T) {
should.Equal(`92233720368547758079223372036854775807`, val.String())
}

func Test_encode_inf(t *testing.T) {
func TestEncodeInf(t *testing.T) {
should := require.New(t)
_, err := json.Marshal(math.Inf(1))
should.Error(err)
Expand All @@ -43,7 +43,7 @@ func Test_encode_inf(t *testing.T) {
should.Error(err)
}

func Test_encode_nan(t *testing.T) {
func TestEncodeNaN(t *testing.T) {
should := require.New(t)
_, err := json.Marshal(math.NaN())
should.Error(err)
Expand All @@ -53,54 +53,70 @@ func Test_encode_nan(t *testing.T) {
should.Error(err)
}

func Test_read_float(t *testing.T) {
func TestReadFloat(t *testing.T) {
inputs := []string{
`1.1`, `1000`, `9223372036854775807`, `12.3`, `-12.3`, `720368.54775807`, `720368.547758075`,
`1e1`, `1e+1`, `1e-1`, `1E1`, `1E+1`, `1E-1`, `-1e1`, `-1e+1`, `-1e-1`,
`1.1`,
`1000`,
`9223372036854775807`,
`12.3`,
`-12.3`,
`720368.54775807`,
`720368.547758075`,
`1e1`,
`1e+1`,
`1e-1`,
`1E1`,
`1E+1`,
`1E-1`,
`-1e1`,
`-1e+1`,
`-1e-1`,
}
for _, input := range inputs {
// non-streaming
t.Run(input, func(t *testing.T) {
should := require.New(t)
r := DecodeStr(input + ",")
expected, err := strconv.ParseFloat(input, 32)
should.NoError(err)
got, err := r.Float32()
should.NoError(err)
should.Equal(float32(expected), got)
})
t.Run(input, func(t *testing.T) {
should := require.New(t)
r := DecodeStr(input + ",")
expected, err := strconv.ParseFloat(input, 64)
should.NoError(err)
got, err := r.Float64()
should.NoError(err)
should.Equal(expected, got)
})
t.Run(input, func(t *testing.T) {
should := require.New(t)
iter := Decode(bytes.NewBufferString(input+","), 2)
expected, err := strconv.ParseFloat(input, 32)
should.NoError(err)
got, err := iter.Float32()
should.NoError(err)
should.Equal(float32(expected), got)
})
t.Run(input, func(t *testing.T) {
should := require.New(t)
iter := Decode(bytes.NewBufferString(input+","), 2)
val := float64(0)
err := json.Unmarshal([]byte(input), &val)
should.NoError(err)
got, err := iter.Float64()
should.NoError(err)
should.Equal(val, got)
for i, input := range inputs {
t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) {
// non-streaming
t.Run("Float32", func(t *testing.T) {
should := require.New(t)
r := DecodeStr(input + ",")
expected, err := strconv.ParseFloat(input, 32)
should.NoError(err)
got, err := r.Float32()
should.NoError(err)
should.Equal(float32(expected), got)
})
t.Run("Float64", func(t *testing.T) {
should := require.New(t)
r := DecodeStr(input + ",")
expected, err := strconv.ParseFloat(input, 64)
should.NoError(err)
got, err := r.Float64()
should.NoError(err)
should.Equal(expected, got)
})
t.Run("Reader", func(t *testing.T) {
should := require.New(t)
iter := Decode(bytes.NewBufferString(input+","), 2)
expected, err := strconv.ParseFloat(input, 32)
should.NoError(err)
got, err := iter.Float32()
should.NoError(err)
should.Equal(float32(expected), got)
})
t.Run("StdJSONCompliance", func(t *testing.T) {
should := require.New(t)
iter := Decode(bytes.NewBufferString(input+","), 2)
val := float64(0)
err := json.Unmarshal([]byte(input), &val)
should.NoError(err)
got, err := iter.Float64()
should.NoError(err)
should.Equal(val, got)
})
})
}
}

func Test_write_float32(t *testing.T) {
func TestWriteFloat32(t *testing.T) {
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
for _, val := range vals {
Expand All @@ -119,7 +135,7 @@ func Test_write_float32(t *testing.T) {
should.Equal("1e-7", string(e.Bytes()))
}

func Test_write_float64(t *testing.T) {
func TestWriteFloat64(t *testing.T) {
vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
for _, val := range vals {
Expand Down
20 changes: 10 additions & 10 deletions int_test.go
Expand Up @@ -12,14 +12,14 @@ import (
"github.com/stretchr/testify/require"
)

func Test_read_uint64_invalid(t *testing.T) {
func TestReadUint64Invalid(t *testing.T) {
should := require.New(t)
iter := DecodeStr(",")
_, err := iter.UInt64()
should.Error(err)
}

func TestDecoder_int_numbers(t *testing.T) {
func TestDecoderIntNumbers(t *testing.T) {
for i := 1; i < 10; i++ { // 10 digits
var data []byte
v := 0
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestDecoder_int_numbers(t *testing.T) {
}
}

func Test_read_int32(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) {
Expand All @@ -96,7 +96,7 @@ func Test_read_int32(t *testing.T) {
}
}

func TestDecoder_int_overflow(t *testing.T) {
func TestDecoderIntOverflow(t *testing.T) {
t.Run("32", func(t *testing.T) {
for _, s := range []string{
"18446744073709551617",
Expand Down Expand Up @@ -167,14 +167,14 @@ func TestDecoder_int_overflow(t *testing.T) {
})
}

func Test_read_int64_overflow(t *testing.T) {
func TestReadInt64Overflow(t *testing.T) {
s := `123456789232323232321545111111111111111111111111111111145454545445`
iter := DecodeStr(s)
_, err := iter.Int64()
require.Error(t, err)
}

func Test_read_int64(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) {
Expand All @@ -198,7 +198,7 @@ func Test_read_int64(t *testing.T) {
}
}

func Test_write_uint32(t *testing.T) {
func TestWriteUint32(t *testing.T) {
vals := []uint32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff}
for _, val := range vals {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
Expand All @@ -215,7 +215,7 @@ func Test_write_uint32(t *testing.T) {
should.Equal("a4294967295", e.String())
}

func Test_write_int32(t *testing.T) {
func TestWriteInt32(t *testing.T) {
vals := []int32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0x7fffffff, -0x80000000}
for _, val := range vals {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
Expand All @@ -232,7 +232,7 @@ func Test_write_int32(t *testing.T) {
should.Equal("a-2147483647", e.String())
}

func Test_write_uint64(t *testing.T) {
func TestWriteUint64(t *testing.T) {
vals := []uint64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
0xfffffffffffffff, 0xffffffffffffffff}
Expand All @@ -251,7 +251,7 @@ func Test_write_uint64(t *testing.T) {
should.Equal("a4294967295", e.String())
}

func Test_write_int64(t *testing.T) {
func TestWriteInt64(t *testing.T) {
vals := []int64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
0xfffffffffffffff, 0x7fffffffffffffff, -0x8000000000000000}
Expand Down
8 changes: 4 additions & 4 deletions null_test.go
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/stretchr/testify/require"
)

func Test_write_null(t *testing.T) {
func TestWriteNull(t *testing.T) {
should := require.New(t)
e := GetEncoder()
e.Null()
should.Equal("null", e.String())
}

func Test_decode_null_array_element(t *testing.T) {
func TestDecodeNullArrayElement(t *testing.T) {
should := require.New(t)
iter := DecodeStr(`[null,"a"]`)
should.True(iter.Elem())
Expand All @@ -24,7 +24,7 @@ func Test_decode_null_array_element(t *testing.T) {
should.Equal("a", s)
}

func Test_decode_null_string(t *testing.T) {
func TestDecodeNullString(t *testing.T) {
should := require.New(t)
iter := DecodeStr(`[null,"a"]`)
should.True(iter.Elem())
Expand All @@ -35,7 +35,7 @@ func Test_decode_null_string(t *testing.T) {
should.Equal("a", s)
}

func Test_decode_null_skip(t *testing.T) {
func TestDecodeNullSkip(t *testing.T) {
iter := DecodeStr(`[null,"a"]`)
iter.Elem()
iter.Skip()
Expand Down
4 changes: 2 additions & 2 deletions obj_test.go
Expand Up @@ -9,15 +9,15 @@ import (
"github.com/stretchr/testify/require"
)

func Test_empty_object(t *testing.T) {
func TestEmptyObject(t *testing.T) {
iter := DecodeStr(`{}`)
require.NoError(t, iter.Obj(func(iter *Decoder, field string) error {
t.Error("should not call")
return nil
}))
}

func Test_one_field(t *testing.T) {
func TestOneField(t *testing.T) {
should := require.New(t)
d := DecodeStr(`{"a": "stream"}`)
should.NoError(d.Obj(func(iter *Decoder, field string) error {
Expand Down
2 changes: 1 addition & 1 deletion string_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require"
)

func Test_read_string(t *testing.T) {
func TestReadString(t *testing.T) {
badInputs := []string{
``,
`null`,
Expand Down

0 comments on commit 6782d83

Please sign in to comment.