45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
|
|
package commandkit
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"math"
|
||
|
|
"reflect"
|
||
|
|
"strconv"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestParseCLIJSONValues(t *testing.T) {
|
||
|
|
for _, tc := range []struct {
|
||
|
|
input string
|
||
|
|
want any
|
||
|
|
}{
|
||
|
|
{`{"a":[null,true,"x",-9223372036854775808,9223372036854775807,9223372036854775808,18446744073709551615],"b":{"n":1.0,"exp":1e3}}`, map[string]any{
|
||
|
|
"a": []any{nil, true, "x", int64(math.MinInt64), int64(math.MaxInt64), uint64(1 << 63), uint64(math.MaxUint64)},
|
||
|
|
"b": map[string]any{"n": float64(1), "exp": float64(1000)},
|
||
|
|
}},
|
||
|
|
{`0`, int64(0)}, {`-0`, int64(0)}, {`42.0`, float64(42)}, {`1e0`, float64(1)},
|
||
|
|
{`0.1`, float64(0.1)}, {`"1"`, "1"}, {`false`, false}, {`null`, nil},
|
||
|
|
} {
|
||
|
|
t.Run(tc.input, func(t *testing.T) {
|
||
|
|
got, err := ParseCLIJSON(tc.input)
|
||
|
|
if err != nil || !reflect.DeepEqual(got, tc.want) {
|
||
|
|
t.Fatalf("got %#v, %v; want %#v", got, err, tc.want)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParseCLIJSONRejectsInvalidInput(t *testing.T) {
|
||
|
|
for _, input := range []string{"", "[", "01", `1 2`, `true false`, `{} garbage`, `-9223372036854775809`, `18446744073709551616`, `1e400`, `[-1e400]`} {
|
||
|
|
t.Run(input, func(t *testing.T) {
|
||
|
|
if _, err := ParseCLIJSON(input); err == nil {
|
||
|
|
t.Fatalf("accepted %q", input)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
_, err := ParseCLIJSON(`18446744073709551616`)
|
||
|
|
if !errors.Is(err, strconv.ErrRange) {
|
||
|
|
t.Fatalf("range error = %v", err)
|
||
|
|
}
|
||
|
|
}
|