forked from jawher/mow.cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflect_test.go
More file actions
45 lines (39 loc) · 935 Bytes
/
reflect_test.go
File metadata and controls
45 lines (39 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package cli
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestVConv(t *testing.T) {
goodCases := []struct {
s string
to reflect.Type
value interface{}
}{
{"test", reflect.TypeOf(""), "test"},
{"true", reflect.TypeOf(true), true},
{"false", reflect.TypeOf(true), false},
{"42", reflect.TypeOf(42), 42},
{"a,b", reflect.TypeOf([]string{}), []string{"a", "b"}},
{"7,42", reflect.TypeOf([]int{}), []int{7, 42}},
}
for _, cas := range goodCases {
v, err := vconv(cas.s, cas.to)
require.Nil(t, err)
require.Equal(t, cas.value, v.Interface())
}
badCases := []struct {
s string
to reflect.Type
}{
{"42", reflect.TypeOf(true)},
{"xx", reflect.TypeOf(true)},
{"aa", reflect.TypeOf(42)},
{"a,b", reflect.TypeOf([]int{})},
{"7,42c", reflect.TypeOf([]int{})},
}
for _, cas := range badCases {
_, err := vconv(cas.s, cas.to)
require.NotNil(t, err)
}
}