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{}
|
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)
|
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})))
|
|
|
|
}
|