Skip to content

Commit dc6a71a

Browse files
golanglibseoh-bse
andauthored
add empty and length assertions for slice and map (#3)
Co-authored-by: eoh <[email protected]>
1 parent 074fad5 commit dc6a71a

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

assert_containment.go renamed to assert_collections.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ package goassert
22

33
import "testing"
44

5+
func EmptySlice[T any](t *testing.T, s []T) {
6+
t.Helper()
7+
8+
length := len(s)
9+
if length != 0 {
10+
t.Errorf("Expected empty slice but got slice with length %d", length)
11+
}
12+
}
13+
14+
func SliceLength[T any](t *testing.T, s []T, expectedLength int) {
15+
t.Helper()
16+
17+
length := len(s)
18+
if length != expectedLength {
19+
t.Errorf("Expected slice to have length of %d but got %d", expectedLength, length)
20+
}
21+
}
22+
523
func SliceContains[K comparable](t *testing.T, s []K, element K) {
624
t.Helper()
725

@@ -28,6 +46,24 @@ func sliceContains[K comparable](s []K, element K) bool {
2846
return false
2947
}
3048

49+
func EmptyMap[K comparable, V any](t *testing.T, m map[K]V) {
50+
t.Helper()
51+
52+
length := len(m)
53+
if length != 0 {
54+
t.Errorf("Expected empty map but got map with length of %d", length)
55+
}
56+
}
57+
58+
func MapLength[K comparable, V any](t *testing.T, m map[K]V, expectedLength int) {
59+
t.Helper()
60+
61+
length := len(m)
62+
if length != expectedLength {
63+
t.Errorf("Expected map to have length of %d but got %d", expectedLength, length)
64+
}
65+
}
66+
3167
func MapContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) {
3268
t.Helper()
3369

assert_containment_test.go renamed to assert_collections_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,54 @@ package goassert
22

33
import "testing"
44

5+
func Test_EmptySliceShouldPass_GivenEmptySlice(t *testing.T) {
6+
tester := new(testing.T)
7+
8+
slice := make([]int, 0)
9+
10+
EmptySlice(tester, slice)
11+
12+
if tester.Failed() {
13+
t.Error("EmptySlice did not pass when empty slice was given")
14+
}
15+
}
16+
17+
func Test_EmptySliceShouldFail_GivenNonEmptySlice(t *testing.T) {
18+
tester := new(testing.T)
19+
20+
slice := []int{10, 3, 5}
21+
22+
EmptySlice(tester, slice)
23+
24+
if !tester.Failed() {
25+
t.Error("EmptySlice did not fail when non-empty slice was given")
26+
}
27+
}
28+
29+
func Test_SliceLengthShouldPass_IfGivenSliceLengthMatchesGivenLength(t *testing.T) {
30+
tester := new(testing.T)
31+
32+
slice := []int{10, 3, 5}
33+
34+
SliceLength(tester, slice, 3)
35+
36+
if tester.Failed() {
37+
t.Error("SliceLength did not pass when given slice's length matches the expected length")
38+
}
39+
}
40+
41+
func Test_SliceLengthShouldFail_IfGivenSliceLengthDoesNotMatchGivenLength(t *testing.T) {
42+
tester := new(testing.T)
43+
44+
slice := []int{10, 3, 5}
45+
46+
SliceLength(tester, slice, 5)
47+
48+
if !tester.Failed() {
49+
t.Error("SliceLength did not fail when given slice's length does not match the expected length")
50+
}
51+
}
52+
553
func Test_SliceContainsShouldPass_GivenElementInGivenSlice(t *testing.T) {
654
tester := new(testing.T)
755

@@ -54,6 +102,63 @@ func Test_SliceNotContainsShouldFail_GivenElementInGivenSlice(t *testing.T) {
54102
}
55103
}
56104

105+
func Test_EmptyMapShouldPass_GivenEmptyMap(t *testing.T) {
106+
tester := new(testing.T)
107+
108+
m := make(map[int]int, 0)
109+
110+
EmptyMap(tester, m)
111+
112+
if tester.Failed() {
113+
t.Error("EmptyMap did not pass given empty map")
114+
}
115+
}
116+
117+
func Test_EmptyMapShouldFail_GivenNonEmptyMap(t *testing.T) {
118+
tester := new(testing.T)
119+
120+
m := map[int]int{
121+
10: 10,
122+
5: 5,
123+
}
124+
125+
EmptyMap(tester, m)
126+
127+
if !tester.Failed() {
128+
t.Error("EmptyMap did not fail given non-empty map")
129+
}
130+
}
131+
132+
func Test_MapLengthShouldPass_IfGivenMapLengthMatchesGivenLength(t *testing.T) {
133+
tester := new(testing.T)
134+
135+
m := map[int]int{
136+
10: 10,
137+
5: 5,
138+
}
139+
140+
MapLength(tester, m, 2)
141+
142+
if tester.Failed() {
143+
t.Error("MapLength did not pass when given map's length matches expected length")
144+
}
145+
}
146+
147+
func Test_MapLengthShouldFail_IfGivenMapLengthDoesNotMatchGivenLength(t *testing.T) {
148+
tester := new(testing.T)
149+
150+
m := map[int]int{
151+
10: 10,
152+
5: 5,
153+
}
154+
155+
MapLength(tester, m, 10)
156+
157+
if !tester.Failed() {
158+
t.Error("MapLength did not fail when given map's length does not match expected length")
159+
}
160+
}
161+
57162
func Test_MapContainsShouldPass_GivenKeyValuePairInGivenMap(t *testing.T) {
58163
tester := new(testing.T)
59164

0 commit comments

Comments
 (0)