Skip to content

Commit 074fad5

Browse files
golanglibseoh-bse
andauthored
make assertion methods 'Helper' to display correct lines on test failures
Co-authored-by: eoh <[email protected]>
1 parent 46c732a commit 074fad5

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

assert_containment.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package goassert
33
import "testing"
44

55
func SliceContains[K comparable](t *testing.T, s []K, element K) {
6+
t.Helper()
7+
68
if !sliceContains(s, element) {
79
t.Errorf("Element %v could not be found in the slice %v", element, s)
810
}
911
}
1012

1113
func SliceNotContains[K comparable](t *testing.T, s []K, element K) {
14+
t.Helper()
15+
1216
if sliceContains(s, element) {
1317
t.Errorf("Element %v was not expected to be found in the slice %v", element, s)
1418
}
@@ -25,6 +29,8 @@ func sliceContains[K comparable](s []K, element K) bool {
2529
}
2630

2731
func MapContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) {
32+
t.Helper()
33+
2834
actualValue, found := m[k]
2935

3036
if !found {
@@ -38,6 +44,8 @@ func MapContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) {
3844
}
3945

4046
func MapNotContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) {
47+
t.Helper()
48+
4149
value, found := m[k]
4250

4351
if found && v == value {

assert_equality.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,32 @@ import (
66
)
77

88
func Equal[K comparable](t testing.TB, expected K, actual K) {
9+
t.Helper()
10+
911
if actual != expected {
1012
t.Error(inequalityMsg(expected, actual))
1113
}
1214
}
1315

1416
func NotEqual[K comparable](t testing.TB, expected K, actual K) {
17+
t.Helper()
18+
1519
if actual == expected {
1620
t.Error(equalityMsg(expected))
1721
}
1822
}
1923

2024
func Nil(t testing.TB, actual interface{}) {
25+
t.Helper()
26+
2127
if !isNil(actual) {
2228
t.Error(inequalityMsg(nil, actual))
2329
}
2430
}
2531

2632
func NotNil(t testing.TB, actual interface{}) {
33+
t.Helper()
34+
2735
if isNil(actual) {
2836
t.Error(equalityMsg("nil"))
2937
}
@@ -43,12 +51,16 @@ func isNil(value interface{}) bool {
4351
}
4452

4553
func EqualSlice[K comparable](t testing.TB, expected []K, actual []K) {
54+
t.Helper()
55+
4656
if !areEqualSlices(expected, actual) {
4757
t.Error(inequalityMsg(expected, actual))
4858
}
4959
}
5060

5161
func NotEqualSlice[K comparable](t testing.TB, expected []K, actual []K) {
62+
t.Helper()
63+
5264
if areEqualSlices(expected, actual) {
5365
t.Error(equalityMsg(expected))
5466
}
@@ -71,12 +83,16 @@ func areEqualSlices[K comparable](expected []K, actual []K) bool {
7183
}
7284

7385
func SimilarSlice[K comparable](t testing.TB, expected []K, actual []K) {
86+
t.Helper()
87+
7488
if !areSimilarSlices(expected, actual) {
7589
t.Error(inequalityMsg(expected, actual))
7690
}
7791
}
7892

7993
func NotSimilarSlice[K comparable](t testing.TB, expected []K, actual []K) {
94+
t.Helper()
95+
8096
if areSimilarSlices(expected, actual) {
8197
t.Error(equalityMsg(expected))
8298
}
@@ -103,12 +119,16 @@ func areSimilarSlices[K comparable](expected []K, actual []K) bool {
103119
}
104120

105121
func EqualMap[K, V comparable](t testing.TB, expected map[K]V, actual map[K]V) {
122+
t.Helper()
123+
106124
if !isEqualMap(expected, actual) {
107125
t.Error(inequalityMsg(expected, actual))
108126
}
109127
}
110128

111129
func NotEqualMap[K, V comparable](t testing.TB, expected map[K]V, actual map[K]V) {
130+
t.Helper()
131+
112132
if isEqualMap(expected, actual) {
113133
t.Error(equalityMsg(expected))
114134
}

assert_panic.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
)
66

77
func Panic(t *testing.T, underTest func()) {
8+
t.Helper()
9+
810
defer func() {
911
if r := recover(); r == nil {
1012
t.Error("Expected panic but there was no panic")
@@ -15,6 +17,8 @@ func Panic(t *testing.T, underTest func()) {
1517
}
1618

1719
func NotPanic(t *testing.T, underTest func()) {
20+
t.Helper()
21+
1822
defer func() {
1923
if r := recover(); r != nil {
2024
t.Errorf("Expected no panic but there was panic: %v", r)
@@ -25,6 +29,8 @@ func NotPanic(t *testing.T, underTest func()) {
2529
}
2630

2731
func PanicWithError[K comparable](t *testing.T, expectedError K, underTest func()) {
32+
t.Helper()
33+
2834
defer func() {
2935
r := recover()
3036
if r == nil {
@@ -48,6 +54,8 @@ func PanicWithError[K comparable](t *testing.T, expectedError K, underTest func(
4854
}
4955

5056
func NotPanicWithError[K comparable](t *testing.T, expectedError K, underTest func()) {
57+
t.Helper()
58+
5159
defer func() {
5260
r := recover()
5361
if r == nil {

assert_true.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package goassert
33
import "testing"
44

55
func True(t testing.TB, assertion bool) {
6+
t.Helper()
7+
68
if !assertion {
79
t.Error(inequalityMsg(true, false))
810
}
911
}
1012

1113
func False(t testing.TB, assertion bool) {
14+
t.Helper()
15+
1216
if assertion {
1317
t.Error(inequalityMsg(false, true))
1418
}

0 commit comments

Comments
 (0)