Skip to content

Commit 54cee31

Browse files
authored
[feature] add not empty assertions (#5)
* Add NotEmpty assertions for slice and map * update empty assertions fail when slice/map is nil
1 parent dc6a71a commit 54cee31

File tree

3 files changed

+139
-12
lines changed

3 files changed

+139
-12
lines changed

assert_collections.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,34 @@ package goassert
22

33
import "testing"
44

5-
func EmptySlice[T any](t *testing.T, s []T) {
5+
func EmptySlice[T any](t testing.TB, s []T) {
66
t.Helper()
77

8+
if s == nil {
9+
t.Error("Expected empty slice but got nil")
10+
return
11+
}
12+
813
length := len(s)
914
if length != 0 {
1015
t.Errorf("Expected empty slice but got slice with length %d", length)
1116
}
1217
}
1318

14-
func SliceLength[T any](t *testing.T, s []T, expectedLength int) {
19+
func NotEmptySlice[T any](t testing.TB, s []T) {
20+
t.Helper()
21+
22+
if s == nil {
23+
t.Error("Expected empty slice but got nil")
24+
return
25+
}
26+
27+
if len(s) == 0 {
28+
t.Error("Expected non- empty slice but got empty slice")
29+
}
30+
}
31+
32+
func SliceLength[T any](t testing.TB, s []T, expectedLength int) {
1533
t.Helper()
1634

1735
length := len(s)
@@ -20,15 +38,15 @@ func SliceLength[T any](t *testing.T, s []T, expectedLength int) {
2038
}
2139
}
2240

23-
func SliceContains[K comparable](t *testing.T, s []K, element K) {
41+
func SliceContains[K comparable](t testing.TB, s []K, element K) {
2442
t.Helper()
2543

2644
if !sliceContains(s, element) {
2745
t.Errorf("Element %v could not be found in the slice %v", element, s)
2846
}
2947
}
3048

31-
func SliceNotContains[K comparable](t *testing.T, s []K, element K) {
49+
func SliceNotContains[K comparable](t testing.TB, s []K, element K) {
3250
t.Helper()
3351

3452
if sliceContains(s, element) {
@@ -46,16 +64,34 @@ func sliceContains[K comparable](s []K, element K) bool {
4664
return false
4765
}
4866

49-
func EmptyMap[K comparable, V any](t *testing.T, m map[K]V) {
67+
func EmptyMap[K comparable, V any](t testing.TB, m map[K]V) {
5068
t.Helper()
5169

70+
if m == nil {
71+
t.Error("Expected empty map but got nil")
72+
return
73+
}
74+
5275
length := len(m)
5376
if length != 0 {
5477
t.Errorf("Expected empty map but got map with length of %d", length)
5578
}
5679
}
5780

58-
func MapLength[K comparable, V any](t *testing.T, m map[K]V, expectedLength int) {
81+
func NotEmptyMap[K comparable, V any](t testing.TB, m map[K]V) {
82+
t.Helper()
83+
84+
if m == nil {
85+
t.Error("Expected non-empty map but got nil")
86+
return
87+
}
88+
89+
if len(m) == 0 {
90+
t.Error("Expected non-empty map but got empty map")
91+
}
92+
}
93+
94+
func MapLength[K comparable, V any](t testing.TB, m map[K]V, expectedLength int) {
5995
t.Helper()
6096

6197
length := len(m)
@@ -64,7 +100,7 @@ func MapLength[K comparable, V any](t *testing.T, m map[K]V, expectedLength int)
64100
}
65101
}
66102

67-
func MapContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) {
103+
func MapContains[K, V comparable](t testing.TB, m map[K]V, k K, v V) {
68104
t.Helper()
69105

70106
actualValue, found := m[k]
@@ -79,7 +115,7 @@ func MapContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) {
79115
}
80116
}
81117

82-
func MapNotContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) {
118+
func MapNotContains[K, V comparable](t testing.TB, m map[K]V, k K, v V) {
83119
t.Helper()
84120

85121
value, found := m[k]

assert_collections_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ func Test_EmptySliceShouldPass_GivenEmptySlice(t *testing.T) {
1414
}
1515
}
1616

17+
func Test_EmptySliceShouldFail_GivenNilSlice(t *testing.T) {
18+
tester := new(testing.T)
19+
20+
EmptySlice[int](tester, nil)
21+
22+
if !tester.Failed() {
23+
t.Error("EmptySlice did not fail when nil slice was given")
24+
}
25+
}
26+
1727
func Test_EmptySliceShouldFail_GivenNonEmptySlice(t *testing.T) {
1828
tester := new(testing.T)
1929

@@ -26,6 +36,40 @@ func Test_EmptySliceShouldFail_GivenNonEmptySlice(t *testing.T) {
2636
}
2737
}
2838

39+
func Test_NotEmptySliceShouldPass_GivenNonEmptySlice(t *testing.T) {
40+
tester := new(testing.T)
41+
42+
slice := []int{10, 16}
43+
44+
NotEmptySlice(tester, slice)
45+
46+
if tester.Failed() {
47+
t.Error("NotEmptySlice did not pass when non-empty slice was given")
48+
}
49+
}
50+
51+
func Test_NotEmptySliceShouldFail_GivenNilSlice(t *testing.T) {
52+
tester := new(testing.T)
53+
54+
NotEmptySlice[string](tester, nil)
55+
56+
if !tester.Failed() {
57+
t.Error("NotEmptySlice did not fail when nil slice was given")
58+
}
59+
}
60+
61+
func Test_NotEmptySliceShouldFail_GivenEmptySlice(t *testing.T) {
62+
tester := new(testing.T)
63+
64+
slice := make([]int, 0)
65+
66+
NotEmptySlice(tester, slice)
67+
68+
if !tester.Failed() {
69+
t.Error("NotEmptySlice did not fail when empty slice was given")
70+
}
71+
}
72+
2973
func Test_SliceLengthShouldPass_IfGivenSliceLengthMatchesGivenLength(t *testing.T) {
3074
tester := new(testing.T)
3175

@@ -114,6 +158,16 @@ func Test_EmptyMapShouldPass_GivenEmptyMap(t *testing.T) {
114158
}
115159
}
116160

161+
func Test_EmptyMapShouldFail_GivenNilEmptyMap(t *testing.T) {
162+
tester := new(testing.T)
163+
164+
EmptyMap[int, int](tester, nil)
165+
166+
if !tester.Failed() {
167+
t.Error("EmptyMap did not fail given nil map")
168+
}
169+
}
170+
117171
func Test_EmptyMapShouldFail_GivenNonEmptyMap(t *testing.T) {
118172
tester := new(testing.T)
119173

@@ -129,6 +183,43 @@ func Test_EmptyMapShouldFail_GivenNonEmptyMap(t *testing.T) {
129183
}
130184
}
131185

186+
func Test_NotEmptyMapShouldPass_GivenNonEmptyMap(t *testing.T) {
187+
tester := new(testing.T)
188+
189+
m := map[int]int{
190+
10: 5,
191+
16: 16,
192+
}
193+
194+
NotEmptyMap(tester, m)
195+
196+
if tester.Failed() {
197+
t.Error("NotEmptyMap did not pass given non-empty map")
198+
}
199+
}
200+
201+
func Test_NotEmptyMapShouldFail_GivenNilMap(t *testing.T) {
202+
tester := new(testing.T)
203+
204+
NotEmptyMap[int, string](tester, nil)
205+
206+
if !tester.Failed() {
207+
t.Error("NotEmptyMap did not fail given nil map")
208+
}
209+
}
210+
211+
func Test_NotEmptyMapShouldFail_GivenEmptyMap(t *testing.T) {
212+
tester := new(testing.T)
213+
214+
m := make(map[int]int, 0)
215+
216+
NotEmptyMap(tester, m)
217+
218+
if !tester.Failed() {
219+
t.Error("NotEmptyMap did not fail given empty map")
220+
}
221+
}
222+
132223
func Test_MapLengthShouldPass_IfGivenMapLengthMatchesGivenLength(t *testing.T) {
133224
tester := new(testing.T)
134225

assert_panic.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55
)
66

7-
func Panic(t *testing.T, underTest func()) {
7+
func Panic(t testing.TB, underTest func()) {
88
t.Helper()
99

1010
defer func() {
@@ -16,7 +16,7 @@ func Panic(t *testing.T, underTest func()) {
1616
underTest()
1717
}
1818

19-
func NotPanic(t *testing.T, underTest func()) {
19+
func NotPanic(t testing.TB, underTest func()) {
2020
t.Helper()
2121

2222
defer func() {
@@ -28,7 +28,7 @@ func NotPanic(t *testing.T, underTest func()) {
2828
underTest()
2929
}
3030

31-
func PanicWithError[K comparable](t *testing.T, expectedError K, underTest func()) {
31+
func PanicWithError[K comparable](t testing.TB, expectedError K, underTest func()) {
3232
t.Helper()
3333

3434
defer func() {
@@ -53,7 +53,7 @@ func PanicWithError[K comparable](t *testing.T, expectedError K, underTest func(
5353
underTest()
5454
}
5555

56-
func NotPanicWithError[K comparable](t *testing.T, expectedError K, underTest func()) {
56+
func NotPanicWithError[K comparable](t testing.TB, expectedError K, underTest func()) {
5757
t.Helper()
5858

5959
defer func() {

0 commit comments

Comments
 (0)