-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvert_func_test.go
More file actions
143 lines (125 loc) · 2.66 KB
/
Copy pathconvert_func_test.go
File metadata and controls
143 lines (125 loc) · 2.66 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package converter
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_FuncChainErrors(t *testing.T) {
tests := []struct {
name string
convertFuncs []any
errorSubstr string
}{
{
name: "no args",
convertFuncs: []any{func() {}},
errorSubstr: "2 or 3",
},
{
name: "one args",
convertFuncs: []any{func(_ T1) {}},
errorSubstr: "2 or 3",
},
{
name: "four args",
convertFuncs: []any{func(_ T1, _ *T2, _ T2, _ T3) {}},
errorSubstr: "2 or 3",
},
{
name: "three args",
convertFuncs: []any{func(_ T1, _ *T2, _ T1) {}},
errorSubstr: "must the first",
},
{
name: "same args",
convertFuncs: []any{func(_ T1, _ T1) {}},
errorSubstr: "different types",
},
{
name: "2 arg func chain",
convertFuncs: []any{func(_ FuncChain, _ T1) {}},
errorSubstr: "2 more",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var panicValue any
func() {
defer func() {
panicValue = recover()
}()
_ = NewFuncChain(test.convertFuncs...)
}()
err, isErr := panicValue.(error)
require.True(t, isErr)
require.ErrorContains(t, err, test.errorSubstr)
})
}
}
func Test_FuncChain(t *testing.T) {
chain := NewFuncChain(t1ToT2, t2ToT1, t2ToT3, t3ToT2, t3ToT4, t4ToT5, t3ToT5)
from := t1{
Name: "name-value-from-1",
Custom1: "custom-value-from-1",
}
to := t3{}
err := chain.Convert(from, &to)
require.NoError(t, err)
require.Equal(t, from.Name, to.Name)
require.Equal(t, from.Custom1, to.Custom3)
backToT1 := t1{}
err = chain.Convert(to, &backToT1)
require.NoError(t, err)
require.Equal(t, from.Name, backToT1.Name)
require.Equal(t, from.Custom1, backToT1.Custom1)
shortestToT5 := t5{}
err = chain.Convert(to, &shortestToT5)
require.NoError(t, err)
require.Equal(t, "FromT3", shortestToT5.Name)
}
type t1 struct {
Name string
Custom1 string
}
type t2 struct {
Name string
Custom2 string
}
type t3 struct {
Name string
Custom3 string
}
type t4 struct {
Name string
Custom3 string
}
type t5 struct {
Name string
Custom3 string
}
func t1ToT2(t1 t1, t2 *t2) error {
t2.Custom2 = t1.Custom1
return nil
}
func t2ToT3(t2 t2, t3 *t3) error {
t3.Custom3 = t2.Custom2
return nil
}
func t3ToT2(_ FuncChain, t3 t3, t2 *t2) error {
t2.Custom2 = t3.Custom3
return nil
}
func t2ToT1(_ FuncChain, t2 t2, t1 *t1) error {
t1.Custom1 = t2.Custom2
return nil
}
func t3ToT4(_ FuncChain, _ t3, _ *t4) error {
return nil
}
func t4ToT5(_ FuncChain, _ t4, t5 *t5) error {
t5.Name = "FromT4"
return nil
}
func t3ToT5(_ FuncChain, _ t3, t5 *t5) error {
t5.Name = "FromT3"
return nil
}