Debian Codegraph @

combinator_test.go

raw symbols SCIP
1 package combinator
2
3 import (
4 "testing"
5 )
6
7 func TestCombinator(t *testing.T) {
8 type Data struct {
9 Color string
10 Number int
11 Enabled bool
12
13 // DataTests ignore this field
14 Untouched bool
15 }
16
17 type DataTests struct {
18 Color []string
19 Number []int
20 Enabled []bool
21
22 // DataTests ignore this field
23 Untouched []bool
24 }
25
26 td := DataTests{
27 Color: []string{"red", "green", "blue"},
28 Number: []int{0, 1},
29 Enabled: []bool{false, true},
30
31 // DataTests ignore this field
32 Untouched: []bool{},
33 }
34 tdl := len(td.Color) * len(td.Number) * len(td.Enabled)
35
36 // initialize check matrix
37 matrix := map[string]map[int]map[bool]bool{}
38 for ci := 0; ci < len(td.Color); ci++ {
39 matrix[td.Color[ci]] = map[int]map[bool]bool{}
40
41 for ni := 0; ni < len(td.Number); ni++ {
42 matrix[td.Color[ci]][td.Number[ni]] = map[bool]bool{}
43
44 for bi := 0; bi < len(td.Enabled); bi++ {
45 matrix[td.Color[ci]][td.Number[ni]][td.Enabled[bi]] = false
46 }
47 }
48 }
49
50 var data []Data
51 err := Generate(&data, td)
52 if err != nil {
53 t.Fatalf("unexpected error: %v", err)
54 }
55
56 if len(data) != tdl {
57 t.Errorf("expected %d permutations, got %d", tdl, len(data))
58 }
59 for _, v := range data {
60 // fmt.Println(i, v)
61
62 cmatrix, ok := matrix[v.Color]
63 if !ok {
64 t.Errorf("unexpected value %s", v.Color)
65 continue
66 }
67 nmatrix, ok := cmatrix[v.Number]
68 if !ok {
69 t.Errorf("unexpected value %s %d", v.Color, v.Number)
70 continue
71 }
72 _, ok = nmatrix[v.Enabled]
73 if !ok {
74 t.Errorf("unexpected value %s %d %v", v.Color, v.Number, v.Enabled)
75 continue
76 }
77
78 // flag combination as found
79 matrix[v.Color][v.Number][v.Enabled] = true
80 }
81
82 // check all combinations have been found
83 for ck, cv := range matrix {
84 for nk, nv := range cv {
85 for bk, bv := range nv {
86 if !bv {
87 t.Errorf("combination not found: %s %d %v", ck, nk, bk)
88 }
89 }
90 }
91 }
92 }
93
94 func TestConstValue(t *testing.T) {
95 type Data struct {
96 Number int
97 Enabled bool
98 }
99
100 type DataTests struct {
101 Number []int
102 Enabled bool
103 }
104
105 td := DataTests{
106 Number: []int{0, 1},
107 Enabled: true,
108 }
109
110 var data []Data
111 err := Generate(&data, td)
112 if err != nil {
113 t.Fatalf("unexpected error: %v", err)
114 }
115
116 for _, v := range data {
117 if !v.Enabled {
118 t.Fatalf("expected enabled to be set to true")
119 }
120 }
121 }
122
123 func TestUnmatchedField(t *testing.T) {
124 type Data struct {
125 Number int
126 }
127
128 type DataTests struct {
129 Number []int
130
131 // Data does not actually contain this field
132 Unmatched []bool
133 }
134
135 td := DataTests{
136 Number: []int{0, 1},
137
138 // Data does not actually contain this field
139 Unmatched: []bool{false},
140 }
141
142 var data []Data
143 err := Generate(&data, td)
144 if err == nil {
145 t.Errorf("expected error for unmatched fields in data, got nil")
146 }
147 }
148
149 func TestInvalidType(t *testing.T) {
150 td := struct {
151 Number []int
152 }{}
153
154 var s string
155 err := Generate(s, td)
156 if err == nil {
157 t.Errorf("expected error for non-pointer type, got nil")
158 }
159
160 err = Generate(&s, td)
161 if err == nil {
162 t.Errorf("expected error for invalid type, got nil")
163 }
164
165 err = Generate(&[]string{}, "")
166 if err == nil {
167 t.Errorf("expected error for invalid type, got nil")
168 }
169 }