-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_test.go
138 lines (132 loc) · 2.81 KB
/
matrix_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
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
package testmatrix
import (
"fmt"
"strings"
"testing"
)
func TestNew(t *testing.T) {
t.Parallel()
cases := []struct {
name string
matrixFunc func() Matrix
wantCount int
}{
{"empy", func() Matrix {
return New()
}, 0},
{"one", func() Matrix {
return New(makeTestDim(1, 1))
}, 1},
{"two", func() Matrix {
return New(makeTestDims(2, alwaysOneValue)...)
}, 2},
{"five", func() Matrix {
return New(makeTestDims(5, alwaysOneValue)...)
}, 5},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
gotMatrix, want := tc.matrixFunc(), tc.wantCount
got := len(gotMatrix.dimensions)
if want != got {
t.Errorf("got %d dimensions; want %d", got, want)
}
})
}
}
func TestNew_error(t *testing.T) {
t.Parallel()
cases := []struct {
name string
matrixFunc func() Matrix
wantPanic string
}{
{"dupe", func() Matrix {
return New(
Dim("dim1", "", Values{"a": struct{}{}}),
Dim("dim1", "", Values{"b": struct{}{}}),
)
}, `duplicate dimension name "dim1"`},
{"novals", func() Matrix {
return New(
Dim("dim1", "", Values{"a": struct{}{}}),
Dim("dim2", "", Values{}),
)
}, `no values for dimension "dim2"`},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
want := tc.wantPanic
func() {
defer func() {
gotPanic := recover()
if gotPanic == nil {
t.Fatalf("did not panic; want panic with %q", want)
}
got := fmt.Sprint(gotPanic)
if !strings.Contains(got, want) {
t.Errorf("got panic %q; want it to contain %q", got, want)
}
}()
tc.matrixFunc()
}()
})
}
}
func TestMatrix_String(t *testing.T) {
t.Parallel()
cases := []struct {
name string
in Matrix
want string
}{
{
"empty",
New(),
"",
},
{
"onedim-oneval",
New(Dim("dim1", "yolo", Values{"dim1val1": 1})),
"dim1\t\n-\t\ndim1val1\t\n",
},
{
"twodim-twoval",
New(
Dim("dim1", "yolo", Values{"dim1val1": 1, "dim1val2": 2}),
Dim("dim2", "yolo", Values{"dim2val1": 1, "dim2val2": 2}),
),
"dim1\tdim2\t\n-\t-\t\ndim1val1\tdim2val1\t\ndim1val2\tdim2val2\t\n",
},
{
"twodim-unevenval1",
New(
Dim("dim1", "yolo", Values{"dim1val1": 1}),
Dim("dim2", "yolo", Values{"dim2val1": 1, "dim2val2": 2}),
),
"dim1\tdim2\t\n-\t-\t\ndim1val1\tdim2val1\t\n\tdim2val2\t\n",
},
{
"twodim-unevanval2",
New(
Dim("dim1", "yolo", Values{"dim1val1": 1, "dim1val2": 2}),
Dim("dim2", "yolo", Values{"dim2val2": 2}),
),
"dim1\tdim2\t\n-\t-\t\ndim1val1\tdim2val2\t\ndim1val2\t\t\n",
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, want := tc.in.String(), tc.want
if got != want {
t.Errorf("got %q; want %q", got, want)
}
})
}
}