Skip to content

Commit d73960d

Browse files
committed
chore: use subtests in all unit tests
closes #68
1 parent ecafd7e commit d73960d

File tree

9 files changed

+1124
-1026
lines changed

9 files changed

+1124
-1026
lines changed

collection_test.go

Lines changed: 93 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,33 @@ func TestPriorityQueue_Push(t *testing.T) {
2525
}
2626

2727
for name, test := range tests {
28-
queue := newPriorityQueue[int]()
28+
t.Run(name, func(t *testing.T) {
29+
queue := newPriorityQueue[int]()
2930

30-
for i, item := range test.items {
31-
queue.Push(item, test.priorities[i])
32-
}
31+
for i, item := range test.items {
32+
queue.Push(item, test.priorities[i])
33+
}
3334

34-
if queue.Len() != len(test.expectedPriorityItems) {
35-
t.Fatalf("%s: item length expectancy doesn't match: expected %v, got %v", name, len(test.expectedPriorityItems), queue.Len())
36-
}
35+
if queue.Len() != len(test.expectedPriorityItems) {
36+
t.Fatalf("item length expectancy doesn't match: expected %v, got %v", len(test.expectedPriorityItems), queue.Len())
37+
}
3738

38-
popped := make([]int, queue.Len())
39+
popped := make([]int, queue.Len())
3940

40-
for queue.Len() > 0 {
41-
item, _ := queue.Pop()
42-
popped = append(popped, item)
43-
}
41+
for queue.Len() > 0 {
42+
item, _ := queue.Pop()
43+
popped = append(popped, item)
44+
}
4445

45-
n := len(popped)
46+
n := len(popped)
4647

47-
for i, item := range test.expectedPriorityItems {
48-
poppedItem := popped[n-1-i]
49-
if item.value != poppedItem {
50-
t.Errorf("%s: item doesn't match: expected %v at index %d, got %v", name, item.value, i, poppedItem)
48+
for i, item := range test.expectedPriorityItems {
49+
poppedItem := popped[n-1-i]
50+
if item.value != poppedItem {
51+
t.Errorf("item doesn't match: expected %v at index %d, got %v", item.value, i, poppedItem)
52+
}
5153
}
52-
}
54+
})
5355
}
5456
}
5557

@@ -80,21 +82,23 @@ func TestPriorityQueue_Pop(t *testing.T) {
8082
}
8183

8284
for name, test := range tests {
83-
queue := newPriorityQueue[int]()
85+
t.Run(name, func(t *testing.T) {
86+
queue := newPriorityQueue[int]()
8487

85-
for i, item := range test.items {
86-
queue.Push(item, test.priorities[i])
87-
}
88+
for i, item := range test.items {
89+
queue.Push(item, test.priorities[i])
90+
}
8891

89-
item, err := queue.Pop()
92+
item, err := queue.Pop()
9093

91-
if test.shouldFail != (err != nil) {
92-
t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, (err != nil), err)
93-
}
94+
if test.shouldFail != (err != nil) {
95+
t.Fatalf("error expectancy doesn't match: expected %v, got %v (error: %v)", test.shouldFail, (err != nil), err)
96+
}
9497

95-
if item != test.expectedItem {
96-
t.Errorf("%s: item expectancy doesn't match: expected %v, got %v", name, test.expectedItem, item)
97-
}
98+
if item != test.expectedItem {
99+
t.Errorf("item expectancy doesn't match: expected %v, got %v", test.expectedItem, item)
100+
}
101+
})
98102
}
99103
}
100104

@@ -156,33 +160,35 @@ func TestPriorityQueue_UpdatePriority(t *testing.T) {
156160
}
157161

158162
for name, test := range tests {
159-
queue := newPriorityQueue[int]()
163+
t.Run(name, func(t *testing.T) {
164+
queue := newPriorityQueue[int]()
160165

161-
for _, item := range test.items {
162-
queue.Push(item.value, item.priority)
163-
}
166+
for _, item := range test.items {
167+
queue.Push(item.value, item.priority)
168+
}
164169

165-
queue.UpdatePriority(test.decreaseItem, test.decreasePriority)
170+
queue.UpdatePriority(test.decreaseItem, test.decreasePriority)
166171

167-
if queue.Len() != len(test.expectedPriorityItems) {
168-
t.Fatalf("%s: item length expectancy doesn't match: expected %v, got %v", name, len(test.expectedPriorityItems), queue.Len())
169-
}
172+
if queue.Len() != len(test.expectedPriorityItems) {
173+
t.Fatalf("item length expectancy doesn't match: expected %v, got %v", len(test.expectedPriorityItems), queue.Len())
174+
}
170175

171-
popped := make([]int, queue.Len())
176+
popped := make([]int, queue.Len())
172177

173-
for queue.Len() > 0 {
174-
item, _ := queue.Pop()
175-
popped = append(popped, item)
176-
}
178+
for queue.Len() > 0 {
179+
item, _ := queue.Pop()
180+
popped = append(popped, item)
181+
}
177182

178-
n := len(popped)
183+
n := len(popped)
179184

180-
for i, item := range test.expectedPriorityItems {
181-
poppedItem := popped[n-1-i]
182-
if item.value != poppedItem {
183-
t.Errorf("%s: item doesn't match: expected %v at index %d, got %v", name, item.value, i, poppedItem)
185+
for i, item := range test.expectedPriorityItems {
186+
poppedItem := popped[n-1-i]
187+
if item.value != poppedItem {
188+
t.Errorf("item doesn't match: expected %v at index %d, got %v", item.value, i, poppedItem)
189+
}
184190
}
185-
}
191+
})
186192
}
187193
}
188194

@@ -210,17 +216,19 @@ func TestPriorityQueue_Len(t *testing.T) {
210216
}
211217

212218
for name, test := range tests {
213-
queue := newPriorityQueue[int]()
219+
t.Run(name, func(t *testing.T) {
220+
queue := newPriorityQueue[int]()
214221

215-
for i, item := range test.items {
216-
queue.Push(item, test.priorities[i])
217-
}
222+
for i, item := range test.items {
223+
queue.Push(item, test.priorities[i])
224+
}
218225

219-
n := queue.Len()
226+
n := queue.Len()
220227

221-
if n != test.expectedLen {
222-
t.Errorf("%s: length expectancy doesn't match: expected %v, got %v", name, test.expectedLen, n)
223-
}
228+
if n != test.expectedLen {
229+
t.Errorf("length expectancy doesn't match: expected %v, got %v", test.expectedLen, n)
230+
}
231+
})
224232
}
225233
}
226234

@@ -229,21 +237,19 @@ func TestStack_push(t *testing.T) {
229237
t T
230238
}
231239
type testCase[T comparable] struct {
232-
name string
233240
elements []int
234241
args args[T]
235242
}
236-
tests := []testCase[int]{
237-
{
238-
"push 1",
243+
tests := map[string]testCase[int]{
244+
"push 1": {
239245
[]int{1, 2, 3, 4, 5, 6},
240246
args[int]{
241247
t: 1,
242248
},
243249
},
244250
}
245-
for _, tt := range tests {
246-
t.Run(tt.name, func(t *testing.T) {
251+
for name, tt := range tests {
252+
t.Run(name, func(t *testing.T) {
247253
s := newStack[int]()
248254

249255
for _, element := range tt.elements {
@@ -257,27 +263,24 @@ func TestStack_push(t *testing.T) {
257263

258264
func TestStack_pop(t *testing.T) {
259265
type testCase[T comparable] struct {
260-
name string
261266
elements []int
262267
want T
263268
wantErr bool
264269
}
265-
tests := []testCase[int]{
266-
{
267-
"pop element",
270+
tests := map[string]testCase[int]{
271+
"pop element": {
268272
[]int{1, 2, 3, 4, 5, 6},
269273
6,
270274
false,
271275
},
272-
{
273-
"pop element from empty stack",
276+
"pop element from empty stack": {
274277
[]int{},
275278
0,
276279
true,
277280
},
278281
}
279-
for _, tt := range tests {
280-
t.Run(tt.name, func(t *testing.T) {
282+
for name, tt := range tests {
283+
t.Run(name, func(t *testing.T) {
281284
s := newStack[int]()
282285

283286
for _, element := range tt.elements {
@@ -298,27 +301,24 @@ func TestStack_pop(t *testing.T) {
298301

299302
func TestStack_top(t *testing.T) {
300303
type testCase[T comparable] struct {
301-
name string
302304
elements []int
303305
want T
304306
wantErr bool
305307
}
306-
tests := []testCase[int]{
307-
{
308-
"top element",
308+
tests := map[string]testCase[int]{
309+
"top element": {
309310
[]int{1, 2, 3, 4, 5, 6},
310311
6,
311312
false,
312313
},
313-
{
314-
"top element of empty stack",
314+
"top element of empty stack": {
315315
[]int{},
316316
0,
317317
true,
318318
},
319319
}
320-
for _, tt := range tests {
321-
t.Run(tt.name, func(t *testing.T) {
320+
for name, tt := range tests {
321+
t.Run(name, func(t *testing.T) {
322322
s := newStack[int]()
323323

324324
for _, element := range tt.elements {
@@ -339,24 +339,21 @@ func TestStack_top(t *testing.T) {
339339

340340
func TestStack_isEmpty(t *testing.T) {
341341
type testCase[T comparable] struct {
342-
name string
343342
elements []int
344343
want bool
345344
}
346-
tests := []testCase[int]{
347-
{
348-
"empty",
345+
tests := map[string]testCase[int]{
346+
"empty": {
349347
[]int{},
350348
true,
351349
},
352-
{
353-
"not empty",
350+
"not empty": {
354351
[]int{1, 2, 3, 4, 5, 6},
355352
false,
356353
},
357354
}
358-
for _, tt := range tests {
359-
t.Run(tt.name, func(t *testing.T) {
355+
for name, tt := range tests {
356+
t.Run(name, func(t *testing.T) {
360357
s := newStack[int]()
361358

362359
for _, element := range tt.elements {
@@ -375,22 +372,20 @@ func TestStack_forEach(t *testing.T) {
375372
f func(T)
376373
}
377374
type testCase[T comparable] struct {
378-
name string
379375
elements []int
380376
args args[T]
381377
}
382-
tests := []testCase[int]{
383-
{
384-
name: "forEach",
378+
tests := map[string]testCase[int]{
379+
"forEach": {
385380
elements: []int{1, 2, 3, 4, 5, 6},
386381
args: args[int]{
387382
f: func(i int) {
388383
},
389384
},
390385
},
391386
}
392-
for _, tt := range tests {
393-
t.Run(tt.name, func(t *testing.T) {
387+
for name, tt := range tests {
388+
t.Run(name, func(t *testing.T) {
394389
s := newStack[int]()
395390

396391
for _, element := range tt.elements {
@@ -404,28 +399,26 @@ func TestStack_forEach(t *testing.T) {
404399

405400
func TestStack_contains(t *testing.T) {
406401
type testCase[T comparable] struct {
407-
name string
408402
elements []int
409403
arg T
410404
expected bool
411405
}
412-
tests := []testCase[int]{
413-
{
414-
name: "contains 6",
406+
tests := map[string]testCase[int]{
407+
408+
"contains 6": {
415409
elements: []int{1, 2, 3, 4, 5, 6},
416410
arg: 6,
417411
expected: true,
418412
},
419-
{
420-
name: "contains 7",
413+
"contains 7": {
421414
elements: []int{1, 2, 3, 4, 5, 6},
422415
arg: 7,
423416
expected: false,
424417
},
425418
}
426419

427-
for _, tt := range tests {
428-
t.Run(tt.name, func(t *testing.T) {
420+
for name, tt := range tests {
421+
t.Run(name, func(t *testing.T) {
429422
s := newStack[int]()
430423

431424
for _, element := range tt.elements {

0 commit comments

Comments
 (0)