Skip to content

Commit 0624748

Browse files
authored
[feature] add MapContainsKey, MapNotContainsKey assertions methods (#9)
1 parent 726538f commit 0624748

File tree

4 files changed

+317
-219
lines changed

4 files changed

+317
-219
lines changed

assert_collections.go renamed to assert_map.go

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

33
import "testing"
44

5-
/*
6-
Asserts that the given slice is empty. The assertion fails if the given slice is nil
7-
*/
8-
func EmptySlice[T any](t testing.TB, s []T) {
9-
t.Helper()
10-
11-
if s == nil {
12-
t.Error("Expected empty slice but got nil")
13-
return
14-
}
15-
16-
length := len(s)
17-
if length != 0 {
18-
t.Errorf("Expected empty slice but got slice with length %d", length)
19-
}
20-
}
21-
22-
/*
23-
Asserts that the given slice is not nil or empty
24-
*/
25-
func NotEmptySlice[T any](t testing.TB, s []T) {
26-
t.Helper()
27-
28-
if s == nil {
29-
t.Error("Expected empty slice but got nil")
30-
return
31-
}
32-
33-
if len(s) == 0 {
34-
t.Error("Expected non- empty slice but got empty slice")
35-
}
36-
}
37-
38-
/*
39-
Asserts that the given slice has length equal to the specified expected length
40-
*/
41-
func SliceLength[T any](t testing.TB, s []T, expectedLength int) {
42-
t.Helper()
43-
44-
length := len(s)
45-
if length != expectedLength {
46-
t.Errorf("Expected slice to have length of %d but got %d", expectedLength, length)
47-
}
48-
}
49-
50-
/*
51-
Asserts that the given slice contains the given element. The element must be [comparable]
52-
*/
53-
func SliceContains[K comparable](t testing.TB, s []K, element K) {
54-
t.Helper()
55-
56-
if !sliceContains(s, element) {
57-
t.Errorf("Element %v could not be found in the slice %v", element, s)
58-
}
59-
}
60-
61-
/*
62-
Asserts that the given slice does not contain the given element. The element must be [comparable]
63-
*/
64-
func SliceNotContains[K comparable](t testing.TB, s []K, element K) {
65-
t.Helper()
66-
67-
if sliceContains(s, element) {
68-
t.Errorf("Element %v was not expected to be found in the slice %v", element, s)
69-
}
70-
}
71-
72-
func sliceContains[K comparable](s []K, element K) bool {
73-
for _, v := range s {
74-
if v == element {
75-
return true
76-
}
77-
}
78-
79-
return false
80-
}
81-
825
/*
836
Asserts that the given map is empty. The assertion will fail if the given map is nil
847
*/
@@ -124,6 +47,30 @@ func MapLength[K comparable, V any](t testing.TB, m map[K]V, expectedLength int)
12447
}
12548
}
12649

50+
/*
51+
Asserts that the given map contains the given key. The key must be [comparable]
52+
*/
53+
func MapContainsKey[K comparable, V any](t testing.TB, m map[K]V, k K) {
54+
t.Helper()
55+
56+
_, found := m[k]
57+
if !found {
58+
t.Errorf("The given map was expected to contain key %v but did not", k)
59+
}
60+
}
61+
62+
/*
63+
Asserts that the given map does not contain the given key. The key must be [comparable]
64+
*/
65+
func MapNotContainsKey[K comparable, V any](t testing.TB, m map[K]V, k K) {
66+
t.Helper()
67+
68+
_, found := m[k]
69+
if found {
70+
t.Errorf("The given map was expected to not contain key %v but did", k)
71+
}
72+
}
73+
12774
/*
12875
Asserts that the given map contains the given key-value pair. The key and value must be [comparable]
12976
*/

0 commit comments

Comments
 (0)