-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcvts_tool_go113_test.go
107 lines (89 loc) · 3.12 KB
/
cvts_tool_go113_test.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//go:build go1.13
// +build go1.13
package evendeep
import (
"reflect"
"testing"
"gopkg.in/hedzr/errors.v3"
"github.com/hedzr/evendeep/dbglog"
"github.com/hedzr/evendeep/ref"
)
func TestFromFuncConverterGo113AndHigher(t *testing.T) { //nolint:revive
fn0 := func() string { return "hello" }
type C struct {
A int
B bool
}
type A struct {
A func() C
B func() bool
}
type B struct {
C *C
B bool
}
a0 := A{
func() C { return C{7, true} },
func() bool { return false },
}
b0 := B{nil, true}
b1 := B{&C{7, true}, false}
var boolTgt bool
intTgt := 1
stringTgt := "world"
lazyInitRoutines()
for ix, fnCase := range []struct {
fn interface{} //nolint:revive
target interface{} //nolint:revive
expect interface{} //nolint:revive
err error
}{
{func() ([]int, error) { return []int{2, 3}, nil }, &[]int{1}, []int{1, 2, 3}, nil},
// {func() ([2]int, error) { return [2]int{2, 3}, nil }, &[2]int{1}, [2]int{2, 3}},
{
func() A { return a0 },
&b0,
b1,
nil,
},
{
func() map[string]interface{} { return map[string]interface{}{"hello": "world"} }, //nolint:revive
&map[string]interface{}{"k": 1, "hello": "bob"}, //nolint:revive
map[string]interface{}{"hello": "world", "k": 1}, //nolint:revive
nil,
},
{func() string { return "hello" }, &stringTgt, "hello", nil},
{func() string { return "hello" }, &intTgt, 1, ErrCannotSet}, //nolint:revive,lll // string -> number, implicit converting will be tried, and failure if it's really not a number
{func() string { return "789" }, &intTgt, 789, nil},
{&fn0, &stringTgt, "hello", nil},
{func() ([2]int, error) { return [2]int{2, 3}, nil }, &[2]int{1}, [2]int{2, 3}, nil},
{func() ([2]int, error) { return [2]int{2, 3}, nil }, &[3]int{1}, [3]int{2, 3}, nil},
{func() ([3]int, error) { return [3]int{2, 3, 5}, nil }, &[2]int{1}, [2]int{2, 3}, nil},
{func() ([]int, error) { return []int{2, 3}, nil }, &[]int{1}, []int{1, 2, 3}, nil},
{func() bool { return true }, &boolTgt, true, nil},
{func() int { return 3 }, &intTgt, 3, nil},
{func() (int, error) { return 5, nil }, &intTgt, 5, nil},
} {
if fnCase.fn != nil { //nolint:gocritic //nestingReduce: invert if cond, replace body with `continue`, move old body after the statement
fnv := reflect.ValueOf(&fnCase.fn)
tgtv := reflect.ValueOf(&fnCase.target)
ff, tt := ref.Rdecodesimple(fnv), ref.Rdecodesimple(tgtv)
dbglog.Log("---- CASE %d. %v -> %v", ix, ref.Typfmtv(&ff), ref.Typfmtv(&tt))
t.Logf("---- CASE %d. %v -> %v", ix, ref.Typfmtv(&ff), ref.Typfmtv(&tt))
c := fromFuncConverter{}
ctx := newValueConverterContextForTest(nil)
err := c.CopyTo(ctx, fnv, tgtv)
if err != nil {
if ret := tt.Interface(); reflect.DeepEqual(ret, fnCase.expect) {
return
}
if fnCase.err != nil && errors.Is(err, fnCase.err) {
return
}
t.Fatalf("has error: %v, but expecting %v", err, fnCase.err)
} else if ret := tt.Interface(); reflect.DeepEqual(ret, fnCase.expect) == false { //nolint:revive
t.Fatalf("unexpect result: expect %v but got %v", fnCase.expect, ret)
}
}
}
}