go-arg/reflect_test.go

81 lines
2.2 KiB
Go
Raw Normal View History

2019-05-02 11:28:17 -05:00
package arg
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
2021-04-19 14:49:49 -05:00
func assertKind(t *testing.T, typ reflect.Type, expected kind) {
actual, err := kindOf(typ)
assert.Equal(t, expected, actual, "expected %v to have kind %v but got %v", typ, expected, actual)
if expected == unsupported {
assert.Error(t, err)
}
2019-05-02 11:28:17 -05:00
}
2021-04-19 14:49:49 -05:00
func TestKindOf(t *testing.T) {
2019-05-02 11:28:17 -05:00
var b bool
var i int
var s string
var f float64
var bs []bool
var is []int
2021-04-19 14:49:49 -05:00
var m map[string]int
var unsupported1 struct{}
var unsupported2 []struct{}
var unsupported3 map[string]struct{}
2019-05-02 11:28:17 -05:00
2021-04-19 14:49:49 -05:00
assertKind(t, reflect.TypeOf(b), binary)
assertKind(t, reflect.TypeOf(i), regular)
assertKind(t, reflect.TypeOf(s), regular)
assertKind(t, reflect.TypeOf(f), regular)
2019-05-02 11:28:17 -05:00
2021-04-19 14:49:49 -05:00
assertKind(t, reflect.TypeOf(&b), binary)
assertKind(t, reflect.TypeOf(&s), regular)
assertKind(t, reflect.TypeOf(&i), regular)
assertKind(t, reflect.TypeOf(&f), regular)
2019-05-02 11:28:17 -05:00
2021-04-19 14:49:49 -05:00
assertKind(t, reflect.TypeOf(bs), sequence)
assertKind(t, reflect.TypeOf(is), sequence)
2019-05-02 11:28:17 -05:00
2021-04-19 14:49:49 -05:00
assertKind(t, reflect.TypeOf(&bs), sequence)
assertKind(t, reflect.TypeOf(&is), sequence)
assertKind(t, reflect.TypeOf(m), mapping)
assertKind(t, reflect.TypeOf(&m), mapping)
assertKind(t, reflect.TypeOf(unsupported1), unsupported)
assertKind(t, reflect.TypeOf(&unsupported1), unsupported)
assertKind(t, reflect.TypeOf(unsupported2), unsupported)
assertKind(t, reflect.TypeOf(&unsupported2), unsupported)
assertKind(t, reflect.TypeOf(unsupported3), unsupported)
assertKind(t, reflect.TypeOf(&unsupported3), unsupported)
2019-05-02 11:28:17 -05:00
}
type implementsTextUnmarshaler struct{}
func (*implementsTextUnmarshaler) UnmarshalText(text []byte) error {
return nil
}
func TestCanParseTextUnmarshaler(t *testing.T) {
2021-04-19 14:49:49 -05:00
var x implementsTextUnmarshaler
var s []implementsTextUnmarshaler
var m []implementsTextUnmarshaler
assertKind(t, reflect.TypeOf(x), regular)
assertKind(t, reflect.TypeOf(&x), regular)
assertKind(t, reflect.TypeOf(s), sequence)
assertKind(t, reflect.TypeOf(&s), sequence)
assertKind(t, reflect.TypeOf(m), mapping)
assertKind(t, reflect.TypeOf(&m), mapping)
}
2021-01-31 20:29:22 -06:00
func TestIsExported(t *testing.T) {
assert.True(t, isExported("Exported"))
assert.False(t, isExported("notExported"))
assert.False(t, isExported(""))
assert.False(t, isExported(string([]byte{255})))
}