-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_test.go
More file actions
99 lines (93 loc) · 2.42 KB
/
result_test.go
File metadata and controls
99 lines (93 loc) · 2.42 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
package txmng
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func ref[T any]() *T { var t T; return &t }
func refv[T any](t T) *T { return &t }
func TestResult_Scan(t *testing.T) {
type tmp struct {
a int
}
cases := []struct {
name string
values Result
actual []interface{}
expected []interface{}
expectedErr error
}{
{
name: "valid example",
values: NewResult(1, 4.2, "hello", tmp{41}, &tmp{42}),
actual: []interface{}{
ref[int](),
ref[float64](),
ref[string](),
ref[tmp](),
ref[*tmp](),
},
expected: []interface{}{
refv(1),
refv(4.2),
refv("hello"),
refv(tmp{41}),
refv(&tmp{42}),
},
expectedErr: nil,
},
{
name: "valid example, omitting values using nil",
values: NewResult(1, 2, 3),
actual: []interface{}{nil, ref[int](), nil},
expected: []interface{}{nil, refv(2), nil},
expectedErr: nil,
},
{
name: "valid example, omitting values using nil 2",
values: NewResult(nil, 2, 3),
actual: []interface{}{ref[int](), ref[int](), nil},
expected: []interface{}{refv(0), refv(2), nil},
expectedErr: nil,
},
{
name: "scan error: different lengths",
values: NewResult(1, 2, 3),
actual: []interface{}{ref[int](), ref[int]()},
expected: nil,
expectedErr: fmt.Errorf("invalid size: expect %d, got %d", 3, 2),
},
{
name: "scan error: arg is not a pointer",
values: NewResult(1, 2, 3),
actual: []interface{}{nil, 0, nil},
expected: nil,
expectedErr: fmt.Errorf("invalid argument at position %d: must be a pointer", 1),
},
{
name: "scan error: invalid arg kind",
values: NewResult(func() {}),
actual: []interface{}{ref[func()]()},
expected: nil,
expectedErr: fmt.Errorf("invalid argument kind at position %d: '%s'", 0, "func"),
},
{
name: "scan error: different types",
values: NewResult(1, 2, 3),
actual: []interface{}{nil, nil, ref[float64]()},
expected: nil,
expectedErr: fmt.Errorf("invalid argument type at position %d: expect '%s', got '%s'", 2, "int", "float64"),
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
err := c.values.Scan(c.actual...)
if c.expectedErr != nil {
assert.Equal(t, c.expectedErr, err)
} else {
assert.Equal(t, c.expected, c.actual)
}
})
}
}