2019-05-05 19:20:05 +00:00
|
|
|
package builtin_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-21 19:08:22 +00:00
|
|
|
"github.com/xdrm-io/aicra/validator/builtin"
|
2019-05-05 19:20:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUint_AvailableTypes(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-14 15:13:38 +00:00
|
|
|
dt := builtin.UintDataType{}
|
2019-05-05 19:20:05 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Type string
|
|
|
|
Handled bool
|
|
|
|
}{
|
|
|
|
{"uint", true},
|
|
|
|
{"Uint", false},
|
|
|
|
{"UINT", false},
|
|
|
|
{" uint", false},
|
|
|
|
{"uint ", false},
|
|
|
|
{" uint ", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.Type, func(t *testing.T) {
|
2021-06-21 19:08:22 +00:00
|
|
|
validator := dt.Validator(test.Type)
|
2020-03-14 15:13:38 +00:00
|
|
|
if validator == nil {
|
2019-05-05 19:20:05 +00:00
|
|
|
if test.Handled {
|
|
|
|
t.Errorf("expect %q to be handled", test.Type)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !test.Handled {
|
|
|
|
t.Errorf("expect %q NOT to be handled", test.Type)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUint_Values(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const typeName = "uint"
|
|
|
|
|
2021-06-21 19:08:22 +00:00
|
|
|
validator := builtin.UintDataType{}.Validator(typeName)
|
2020-03-14 15:13:38 +00:00
|
|
|
if validator == nil {
|
2019-05-05 19:20:05 +00:00
|
|
|
t.Errorf("expect %q to be handled", typeName)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Value interface{}
|
|
|
|
Valid bool
|
|
|
|
}{
|
|
|
|
{uint(0), true},
|
|
|
|
{uint(math.MaxInt64), true},
|
|
|
|
{uint(math.MaxUint64), true},
|
|
|
|
{-1, false},
|
|
|
|
{-math.MaxInt64, false},
|
|
|
|
|
|
|
|
{float64(math.MinInt64), false},
|
|
|
|
{float64(0), true},
|
|
|
|
{float64(math.MaxInt64), true},
|
|
|
|
// we cannot just compare because of how precision works
|
|
|
|
{float64(math.MaxUint64 - 1024), true},
|
|
|
|
{float64(math.MaxUint64 + 1), false},
|
|
|
|
|
|
|
|
// json number
|
|
|
|
{fmt.Sprintf("%d", math.MinInt64), false},
|
|
|
|
{"-1", false},
|
|
|
|
{"0", true},
|
|
|
|
{"1", true},
|
|
|
|
{fmt.Sprintf("%d", math.MaxInt64), true},
|
|
|
|
{fmt.Sprintf("%d", uint(math.MaxUint64)), true},
|
|
|
|
// strane offset because of how precision works
|
|
|
|
{fmt.Sprintf("%f", float64(math.MaxUint64+1024*3)), false},
|
|
|
|
|
2020-03-01 20:43:28 +00:00
|
|
|
{[]byte(fmt.Sprintf("%d", math.MaxInt64)), true},
|
|
|
|
{[]byte(fmt.Sprintf("%d", uint(math.MaxUint64))), true},
|
|
|
|
// strane offset because of how precision works
|
|
|
|
{[]byte(fmt.Sprintf("%f", float64(math.MaxUint64+1024*3))), false},
|
|
|
|
|
2019-05-05 19:20:05 +00:00
|
|
|
{"string", false},
|
|
|
|
{[]byte("bytes"), false},
|
|
|
|
{-0.1, false},
|
|
|
|
{0.1, false},
|
|
|
|
{nil, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
2020-03-14 15:13:38 +00:00
|
|
|
if _, isValid := validator(test.Value); isValid {
|
2019-05-05 19:20:05 +00:00
|
|
|
if !test.Valid {
|
|
|
|
t.Errorf("expect value to be invalid")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if test.Valid {
|
|
|
|
t.Errorf("expect value to be valid")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|