aicra/datatype/builtin/bool_test.go

103 lines
1.6 KiB
Go

package builtin_test
import (
"fmt"
"testing"
"git.xdrm.io/go/aicra/datatype/builtin"
)
func TestBool_AvailableTypes(t *testing.T) {
t.Parallel()
dt := builtin.BoolDataType{}
tests := []struct {
Type string
Handled bool
}{
{"bool", true},
{"Bool", false},
{"boolean", false},
{" bool", false},
{"bool ", false},
{" bool ", false},
}
for _, test := range tests {
t.Run(test.Type, func(t *testing.T) {
validator := dt.Build(test.Type)
if validator == nil {
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 TestBool_Values(t *testing.T) {
t.Parallel()
const typeName = "bool"
validator := builtin.BoolDataType{}.Build(typeName)
if validator == nil {
t.Errorf("expect %q to be handled", typeName)
t.Fail()
}
tests := []struct {
Value interface{}
Valid bool
}{
{true, true},
{false, true},
{1, false},
{0, false},
{-1, false},
// json number
{"-1", false},
{"0", false},
{"1", false},
// json string
{"true", true},
{"false", true},
{[]byte("true"), true},
{[]byte("false"), true},
{"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) {
if _, isValid := validator(test.Value); isValid {
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()
}
})
}
}