2019-05-02 11:28:17 -05:00
|
|
|
package arg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-04-19 15:21:04 -05:00
|
|
|
func assertCardinality(t *testing.T, typ reflect.Type, expected cardinality) {
|
|
|
|
actual, err := cardinalityOf(typ)
|
|
|
|
assert.Equal(t, expected, actual, "expected %v to have cardinality %v but got %v", typ, expected, actual)
|
2021-04-19 14:49:49 -05:00
|
|
|
if expected == unsupported {
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2019-05-02 11:28:17 -05:00
|
|
|
}
|
|
|
|
|
2021-04-19 15:21:04 -05:00
|
|
|
func TestCardinalityOf(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{}
|
2021-04-19 16:50:05 -05:00
|
|
|
var unsupported4 map[struct{}]string
|
2019-05-02 11:28:17 -05:00
|
|
|
|
2021-04-19 15:21:04 -05:00
|
|
|
assertCardinality(t, reflect.TypeOf(b), zero)
|
|
|
|
assertCardinality(t, reflect.TypeOf(i), one)
|
|
|
|
assertCardinality(t, reflect.TypeOf(s), one)
|
|
|
|
assertCardinality(t, reflect.TypeOf(f), one)
|
2019-05-02 11:28:17 -05:00
|
|
|
|
2021-04-19 15:21:04 -05:00
|
|
|
assertCardinality(t, reflect.TypeOf(&b), zero)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&s), one)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&i), one)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&f), one)
|
2019-05-02 11:28:17 -05:00
|
|
|
|
2021-04-19 15:21:04 -05:00
|
|
|
assertCardinality(t, reflect.TypeOf(bs), multiple)
|
|
|
|
assertCardinality(t, reflect.TypeOf(is), multiple)
|
2019-05-02 11:28:17 -05:00
|
|
|
|
2021-04-19 15:21:04 -05:00
|
|
|
assertCardinality(t, reflect.TypeOf(&bs), multiple)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&is), multiple)
|
2021-04-19 14:49:49 -05:00
|
|
|
|
2021-04-19 15:21:04 -05:00
|
|
|
assertCardinality(t, reflect.TypeOf(m), multiple)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&m), multiple)
|
2021-04-19 14:49:49 -05:00
|
|
|
|
2021-04-19 15:21:04 -05:00
|
|
|
assertCardinality(t, reflect.TypeOf(unsupported1), unsupported)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&unsupported1), unsupported)
|
|
|
|
assertCardinality(t, reflect.TypeOf(unsupported2), unsupported)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&unsupported2), unsupported)
|
|
|
|
assertCardinality(t, reflect.TypeOf(unsupported3), unsupported)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&unsupported3), unsupported)
|
2021-04-19 16:50:05 -05:00
|
|
|
assertCardinality(t, reflect.TypeOf(unsupported4), unsupported)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&unsupported4), unsupported)
|
2019-05-02 11:28:17 -05:00
|
|
|
}
|
2019-05-02 11:32:23 -05:00
|
|
|
|
|
|
|
type implementsTextUnmarshaler struct{}
|
|
|
|
|
|
|
|
func (*implementsTextUnmarshaler) UnmarshalText(text []byte) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 15:21:04 -05:00
|
|
|
func TestCardinalityTextUnmarshaler(t *testing.T) {
|
2021-04-19 14:49:49 -05:00
|
|
|
var x implementsTextUnmarshaler
|
|
|
|
var s []implementsTextUnmarshaler
|
|
|
|
var m []implementsTextUnmarshaler
|
2021-04-19 15:21:04 -05:00
|
|
|
assertCardinality(t, reflect.TypeOf(x), one)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&x), one)
|
|
|
|
assertCardinality(t, reflect.TypeOf(s), multiple)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&s), multiple)
|
|
|
|
assertCardinality(t, reflect.TypeOf(m), multiple)
|
|
|
|
assertCardinality(t, reflect.TypeOf(&m), multiple)
|
2019-05-02 11:32:23 -05:00
|
|
|
}
|
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})))
|
|
|
|
}
|
2021-04-19 16:50:05 -05:00
|
|
|
|
|
|
|
func TestCardinalityString(t *testing.T) {
|
|
|
|
assert.Equal(t, "zero", zero.String())
|
|
|
|
assert.Equal(t, "one", one.String())
|
|
|
|
assert.Equal(t, "multiple", multiple.String())
|
|
|
|
assert.Equal(t, "unsupported", unsupported.String())
|
|
|
|
assert.Equal(t, "unknown(42)", cardinality(42).String())
|
|
|
|
}
|
2021-04-19 23:03:43 -05:00
|
|
|
|
|
|
|
func TestIsZero(t *testing.T) {
|
|
|
|
var zero int
|
|
|
|
var notZero = 3
|
|
|
|
var nilSlice []int
|
|
|
|
var nonNilSlice = []int{1, 2, 3}
|
|
|
|
var nilMap map[string]string
|
|
|
|
var nonNilMap = map[string]string{"foo": "bar"}
|
|
|
|
var uncomparable = func() {}
|
|
|
|
|
|
|
|
assert.True(t, isZero(reflect.ValueOf(zero)))
|
|
|
|
assert.False(t, isZero(reflect.ValueOf(notZero)))
|
|
|
|
|
|
|
|
assert.True(t, isZero(reflect.ValueOf(nilSlice)))
|
|
|
|
assert.False(t, isZero(reflect.ValueOf(nonNilSlice)))
|
|
|
|
|
|
|
|
assert.True(t, isZero(reflect.ValueOf(nilMap)))
|
|
|
|
assert.False(t, isZero(reflect.ValueOf(nonNilMap)))
|
|
|
|
|
|
|
|
assert.False(t, isZero(reflect.ValueOf(uncomparable)))
|
|
|
|
}
|