-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert_slice.go
More file actions
80 lines (65 loc) · 1.65 KB
/
Copy pathassert_slice.go
File metadata and controls
80 lines (65 loc) · 1.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
package goassert
import "testing"
/*
Asserts that the given slice is empty. The assertion fails if the given slice is nil
*/
func EmptySlice[T any](t testing.TB, s []T) {
t.Helper()
if s == nil {
t.Error("Expected empty slice but got nil")
return
}
length := len(s)
if length != 0 {
t.Errorf("Expected empty slice but got slice with length %d", length)
}
}
/*
Asserts that the given slice is not nil or empty
*/
func NotEmptySlice[T any](t testing.TB, s []T) {
t.Helper()
if s == nil {
t.Error("Expected empty slice but got nil")
return
}
if len(s) == 0 {
t.Error("Expected non- empty slice but got empty slice")
}
}
/*
Asserts that the given slice has length equal to the specified expected length
*/
func SliceLength[T any](t testing.TB, s []T, expectedLength int) {
t.Helper()
length := len(s)
if length != expectedLength {
t.Errorf("Expected slice to have length of %d but got %d", expectedLength, length)
}
}
/*
Asserts that the given slice contains the given element. The element must be [comparable]
*/
func SliceContains[K comparable](t testing.TB, s []K, element K) {
t.Helper()
if !sliceContains(s, element) {
t.Errorf("Element %v could not be found in the slice %v", element, s)
}
}
/*
Asserts that the given slice does not contain the given element. The element must be [comparable]
*/
func SliceNotContains[K comparable](t testing.TB, s []K, element K) {
t.Helper()
if sliceContains(s, element) {
t.Errorf("Element %v was not expected to be found in the slice %v", element, s)
}
}
func sliceContains[K comparable](s []K, element K) bool {
for _, v := range s {
if v == element {
return true
}
}
return false
}