Skip to content

Commit 913f154

Browse files
authored
Merge pull request #13 from launchdarkly/eb/sc-140611/matchers-helper
call t.Helper() from matchers so they're not in the stacktrace
2 parents b0c59bc + ed0f6ae commit 913f154

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

matchers/assertion_scope.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ type TestingT interface {
88
FailNow()
99
}
1010

11+
// Go's testing.T and other compatible packages provide an additional method, Helper(),
12+
// which indicates that whatever function called it should not be included in stacktraces.
13+
type helperT interface {
14+
Helper()
15+
}
16+
1117
// AssertionScope is a context for executing assertions.
1218
type AssertionScope struct {
1319
t TestingT
@@ -54,6 +60,9 @@ func (a AssertionScope) For(name string) AssertionScope {
5460
// scope's Errorf method. This logs a failure but does not stop the test.
5561
func (a AssertionScope) Assert(value interface{}, matcher Matcher) bool {
5662
if pass, desc := matcher.Test(value); !pass {
63+
if h, ok := a.t.(helperT); ok {
64+
h.Helper()
65+
}
5766
a.fail(desc)
5867
return false
5968
}
@@ -66,6 +75,9 @@ func (a AssertionScope) Assert(value interface{}, matcher Matcher) bool {
6675
// the test.
6776
func (a AssertionScope) Require(value interface{}, matcher Matcher) bool {
6877
if pass, desc := matcher.Test(value); !pass {
78+
if h, ok := a.t.(helperT); ok {
79+
h.Helper()
80+
}
6981
a.fail(desc)
7082
a.t.FailNow()
7183
return false // does not return since FailNow() will force an early exit
@@ -74,5 +86,8 @@ func (a AssertionScope) Require(value interface{}, matcher Matcher) bool {
7486
}
7587

7688
func (a AssertionScope) fail(desc string) {
89+
if h, ok := a.t.(helperT); ok {
90+
h.Helper()
91+
}
7792
a.t.Errorf("%s%s", a.prefix, desc)
7893
}

0 commit comments

Comments
 (0)