Skip to content

Commit 13a5f3f

Browse files
committed
initial commit
0 parents  commit 13a5f3f

11 files changed

+1041
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# goassert

assert_containment.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package goassert
2+
3+
import "testing"
4+
5+
func SliceContains[K comparable](t *testing.T, s []K, element K) {
6+
if !sliceContains(s, element) {
7+
t.Errorf("Element %v could not be found in the slice %v", element, s)
8+
}
9+
}
10+
11+
func SliceNotContains[K comparable](t *testing.T, s []K, element K) {
12+
if sliceContains(s, element) {
13+
t.Errorf("Element %v was not expected to be found in the slice %v", element, s)
14+
}
15+
}
16+
17+
func sliceContains[K comparable](s []K, element K) bool {
18+
for _, v := range s {
19+
if v == element {
20+
return true
21+
}
22+
}
23+
24+
return false
25+
}
26+
27+
func MapContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) {
28+
actualValue, found := m[k]
29+
30+
if !found {
31+
t.Errorf("Key %v was not found in the map", k)
32+
return
33+
}
34+
35+
if v != actualValue {
36+
t.Errorf("Expected %v for key %v in the map but got %v", v, k, actualValue)
37+
}
38+
}
39+
40+
func MapNotContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) {
41+
value, found := m[k]
42+
43+
if found && v == value {
44+
t.Errorf("Key %v and value %v was not expected to be found in the map", k, v)
45+
}
46+
}

assert_containment_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package goassert
2+
3+
import "testing"
4+
5+
func Test_SliceContainsShouldPass_GivenElementInGivenSlice(t *testing.T) {
6+
tester := new(testing.T)
7+
8+
slice := []int{10, 5, 16, 3}
9+
element := 5
10+
11+
SliceContains(tester, slice, element)
12+
13+
if tester.Failed() {
14+
t.Error("SliceContains did not pass when given element is found within given slice")
15+
}
16+
}
17+
18+
func Test_SliceContainsShouldFail_GivenElementNotInGivenSlice(t *testing.T) {
19+
tester := new(testing.T)
20+
21+
slice := []int{10, 5, 16, 3}
22+
element := 7
23+
24+
SliceContains(tester, slice, element)
25+
26+
if !tester.Failed() {
27+
t.Error("SliceContains did not fail when given element is not found within given slice")
28+
}
29+
}
30+
31+
func Test_SliceNotContainsShouldPass_GivenElementNotInGivenSlice(t *testing.T) {
32+
tester := new(testing.T)
33+
34+
slice := []int{10, 5, 16, 3}
35+
element := 7
36+
37+
SliceNotContains(tester, slice, element)
38+
39+
if tester.Failed() {
40+
t.Error("SliceNotContains did not pass when given element is not found within given slice")
41+
}
42+
}
43+
44+
func Test_SliceNotContainsShouldFail_GivenElementInGivenSlice(t *testing.T) {
45+
tester := new(testing.T)
46+
47+
slice := []int{10, 5, 16, 3}
48+
element := 10
49+
50+
SliceNotContains(tester, slice, element)
51+
52+
if !tester.Failed() {
53+
t.Error("SliceNotContains did not fail when given element is not found within given slice")
54+
}
55+
}
56+
57+
func Test_MapContainsShouldPass_GivenKeyValuePairInGivenMap(t *testing.T) {
58+
tester := new(testing.T)
59+
60+
m := map[int]int{
61+
5: 5,
62+
10: 10,
63+
16: 16,
64+
}
65+
k, v := 10, 10
66+
67+
MapContains(tester, m, k, v)
68+
69+
if tester.Failed() {
70+
t.Error("MapContains did not pass when given key value pair is found in given map")
71+
}
72+
}
73+
74+
func Test_MapContainsShouldFail_GivenKeyNotInGivenMap(t *testing.T) {
75+
tester := new(testing.T)
76+
77+
m := map[int]int{
78+
5: 5,
79+
10: 10,
80+
16: 16,
81+
}
82+
k, v := 7, 10
83+
84+
MapContains(tester, m, k, v)
85+
86+
if !tester.Failed() {
87+
t.Error("MapContains did not fail when given key value pair is not found in given map")
88+
}
89+
}
90+
91+
func Test_MapNotContainsShouldPass_GivenKeyValuePairNotInGivenMap(t *testing.T) {
92+
tester := new(testing.T)
93+
94+
m := map[int]int{
95+
5: 5,
96+
10: 10,
97+
16: 16,
98+
}
99+
k, v := 3, 7
100+
101+
MapNotContains(tester, m, k, v)
102+
103+
if tester.Failed() {
104+
t.Error("MapContains did not pass when given key value pair is not found in given map")
105+
}
106+
}
107+
108+
func Test_MapNotContainsShouldPass_GivenValueNotInGivenMap(t *testing.T) {
109+
tester := new(testing.T)
110+
111+
m := map[int]int{
112+
5: 5,
113+
10: 10,
114+
16: 16,
115+
}
116+
k, v := 10, 7
117+
118+
MapNotContains(tester, m, k, v)
119+
120+
if tester.Failed() {
121+
t.Error("MapContains did not pass when given key value pair is not found in given map")
122+
}
123+
}
124+
125+
func Test_MapNotContainsShouldFail_GivenKeyValuePairInGivenMap(t *testing.T) {
126+
tester := new(testing.T)
127+
128+
m := map[int]int{
129+
5: 5,
130+
10: 10,
131+
16: 16,
132+
}
133+
k, v := 5, 5
134+
135+
MapNotContains(tester, m, k, v)
136+
137+
if !tester.Failed() {
138+
t.Error("MapContains did not fail when given key value pair is found in given map")
139+
}
140+
}

assert_equality.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package goassert
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Equal[K comparable](t testing.TB, expected K, actual K) {
9+
if actual != expected {
10+
t.Error(inequalityMsg(expected, actual))
11+
}
12+
}
13+
14+
func NotEqual[K comparable](t testing.TB, expected K, actual K) {
15+
if actual == expected {
16+
t.Error(equalityMsg(expected))
17+
}
18+
}
19+
20+
func Nil(t testing.TB, actual interface{}) {
21+
if !isNil(actual) {
22+
t.Error(inequalityMsg(nil, actual))
23+
}
24+
}
25+
26+
func NotNil(t testing.TB, actual interface{}) {
27+
if isNil(actual) {
28+
t.Error(equalityMsg("nil"))
29+
}
30+
}
31+
32+
func isNil(value interface{}) bool {
33+
if value == nil {
34+
return true
35+
}
36+
37+
switch reflect.TypeOf(value).Kind() {
38+
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
39+
return reflect.ValueOf(value).IsNil()
40+
}
41+
42+
return false
43+
}
44+
45+
func EqualSlice[K comparable](t testing.TB, expected []K, actual []K) {
46+
if !areEqualSlices(expected, actual) {
47+
t.Error(inequalityMsg(expected, actual))
48+
}
49+
}
50+
51+
func NotEqualSlice[K comparable](t testing.TB, expected []K, actual []K) {
52+
if areEqualSlices(expected, actual) {
53+
t.Error(equalityMsg(expected))
54+
}
55+
}
56+
57+
func areEqualSlices[K comparable](expected []K, actual []K) bool {
58+
expectedLength := len(expected)
59+
actualLength := len(actual)
60+
if actualLength != expectedLength {
61+
return false
62+
}
63+
64+
for i := 0; i < expectedLength; i++ {
65+
if actual[i] != expected[i] {
66+
return false
67+
}
68+
}
69+
70+
return true
71+
}
72+
73+
func SimilarSlice[K comparable](t testing.TB, expected []K, actual []K) {
74+
if !areSimilarSlices(expected, actual) {
75+
t.Error(inequalityMsg(expected, actual))
76+
}
77+
}
78+
79+
func NotSimilarSlice[K comparable](t testing.TB, expected []K, actual []K) {
80+
if areSimilarSlices(expected, actual) {
81+
t.Error(equalityMsg(expected))
82+
}
83+
}
84+
85+
func areSimilarSlices[K comparable](expected []K, actual []K) bool {
86+
expectedLength := len(expected)
87+
actualLength := len(actual)
88+
if actualLength != expectedLength {
89+
return false
90+
}
91+
92+
expectedElementsMap := make(map[K]int, expectedLength)
93+
for _, v := range expected {
94+
expectedElementsMap[v] += 1
95+
}
96+
97+
actualElementsMap := make(map[K]int, actualLength)
98+
for _, v := range actual {
99+
actualElementsMap[v] += 1
100+
}
101+
102+
return isEqualMap(expectedElementsMap, actualElementsMap)
103+
}
104+
105+
func EqualMap[K, V comparable](t testing.TB, expected map[K]V, actual map[K]V) {
106+
if !isEqualMap(expected, actual) {
107+
t.Error(inequalityMsg(expected, actual))
108+
}
109+
}
110+
111+
func NotEqualMap[K, V comparable](t testing.TB, expected map[K]V, actual map[K]V) {
112+
if isEqualMap(expected, actual) {
113+
t.Error(equalityMsg(expected))
114+
}
115+
}
116+
117+
func isEqualMap[K, V comparable](expected map[K]V, actual map[K]V) bool {
118+
expectedLength := len(expected)
119+
actualLength := len(actual)
120+
if actualLength != expectedLength {
121+
return false
122+
}
123+
124+
for actualKey, actualValue := range actual {
125+
expectedValue, found := expected[actualKey]
126+
if !found || actualValue != expectedValue {
127+
return false
128+
}
129+
}
130+
131+
return true
132+
}

0 commit comments

Comments
 (0)