-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.go
57 lines (48 loc) · 1.25 KB
/
test_helpers.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
package pipelines
import (
"reflect"
"testing"
)
func expectClosedChannel[T any](expect bool, stream <-chan T, t testing.TB) {
// read the stream first to remove race condition
for range stream {
}
if _, ok := <-stream; ok && expect {
t.Errorf("expected channel closure to be %v but it was %v", expect, ok)
}
}
func expectOrderedResultsList[W any](want []W, outStream <-chan W, t testing.TB) {
got := make([]W, len(want))
var idx int
for item := range outStream {
got[idx] = item
idx++
}
if !reflect.DeepEqual(want, got) {
t.Errorf("wanted resulting list to be %+v but got %+v\n", want, got)
}
}
func expectStreamLengthToBe[T any](expect int, stream <-chan T, t testing.TB) {
if cnt := lenStream(stream); cnt != expect {
t.Errorf("expected stream length to be %d but it was %d", expect, cnt)
}
}
func expectStreamLengthToBeLessThan[T any](max int, stream <-chan T, t testing.TB) {
if cnt := lenStream(stream); cnt > max {
t.Errorf("expected stream length to be less than %d but got %d\n", max, cnt)
}
}
func lenStream[T any](inStream <-chan T) (count int) {
for range inStream {
count++
}
return
}
func getIndexOf[T comparable](want T, list []T) int {
for idx, val := range list {
if want == val {
return idx
}
}
return -1
}