-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_test.go
More file actions
156 lines (126 loc) · 2.65 KB
/
matrix_test.go
File metadata and controls
156 lines (126 loc) · 2.65 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
144
145
146
147
148
149
150
151
152
153
154
155
156
package matrix
import (
"fmt"
"reflect"
"slices"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func ptrTo(i int) *int {
return &i
}
func TestSimple(t *testing.T) {
t.Parallel()
type tcase struct {
X string
Y int
Z *int
}
expected := []tcase{
{X: "a", Y: 1, Z: ptrTo(1)},
{X: "b", Y: 1, Z: ptrTo(1)},
{X: "a", Y: 2, Z: ptrTo(1)},
{X: "b", Y: 2, Z: ptrTo(1)},
{X: "a", Y: 1, Z: ptrTo(2)},
{X: "b", Y: 1, Z: ptrTo(2)},
{X: "a", Y: 2, Z: ptrTo(2)},
{X: "b", Y: 2, Z: ptrTo(2)},
}
generated := slices.Collect(Generate(t, tcase{},
[]string{"a", "b"},
[]int{1, 2},
[]*int{ptrTo(1), ptrTo(2)}))
if !reflect.DeepEqual(expected, generated) {
t.Logf("Expected: %v, got: %v", expected, generated)
t.Fail()
}
}
func TestFuncDimensions(t *testing.T) {
t.Parallel()
type tcase struct {
X func() int
Y func() string
}
type expectedValues struct {
int
string
}
expected := []expectedValues{
{2, "a"},
{3, "a"},
{2, "b"},
{3, "b"},
}
generated := slices.Collect(Generate(t, tcase{},
[]func() int{
func() int { return 2 },
func() int { return 3 },
},
[]func() string{
func() string { return "a" },
func() string { return "b" },
},
))
if len(generated) != len(expected) {
t.Logf("Expected %d items, got: %d", len(expected), len(generated))
t.Fail()
}
for i, tcase := range generated {
if tcase.X() != expected[i].int {
t.Logf("Expected: %d, got: %d", expected[i].int, tcase.X())
t.Fail()
}
if tcase.Y() != expected[i].string {
t.Logf("Expected: %s, got: %s", expected[i].string, tcase.Y())
t.Fail()
}
}
}
type testMock struct {
mock.Mock
}
func (m *testMock) Helper() {
m.Called()
}
func (m *testMock) Fatalf(format string, args ...any) {
m.Called(format, args)
panic(fmt.Sprintf(format, args...))
}
func TestSeq_UnexportedField(t *testing.T) {
t.Parallel()
type tcase struct {
y string //nolint:unused
}
tm := &testMock{}
tm.On("Helper")
tm.On("Fatalf", "tcase must not have unexported fields, got: %s", []interface{}{"y"}).Once()
assert.Panics(t, func() {
_ = Generate(tm, tcase{}, []string{"a", "b"})
})
tm.AssertExpectations(t)
}
func TestNilPointers(t *testing.T) {
t.Parallel()
type tcase struct {
Y *int
}
expected := []tcase{
{Y: nil},
{Y: ptrTo(1)},
}
generated := GenerateAndCollect(t, tcase{}, []*int{nil, ptrTo(1)})
assert.Equal(t, expected, generated)
}
func TestGenerateAndCollect(t *testing.T) {
t.Parallel()
type tcase struct {
Y bool
}
expected := []tcase{
{Y: false},
{Y: true},
}
generated := GenerateAndCollect(t, tcase{}, []bool{false, true})
assert.Equal(t, expected, generated)
}