Skip to content

Commit 345aeae

Browse files
authored
feat: support naming assertions to help describe their purpose (#19)
This change adds a new "Ensure(string)" function to the "Verifier" interface. This method will apply a description to the current verifier which will be prepended to potential failures, should those happen. Usage: With(t).Ensure("my feature should yield a popup").Verify(...)...
1 parent 5631421 commit 345aeae

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

asserter.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,48 @@ package justest
22

33
import (
44
"fmt"
5-
"github.com/arikkfir/justest/internal"
65
"path/filepath"
76
"regexp"
7+
"strings"
88
"time"
9+
10+
"github.com/arikkfir/justest/internal"
911
)
1012

1113
//go:noinline
12-
func With(t T) Verifier {
14+
func With(t T) VerifierAndEnsurer {
1315
if t == nil {
1416
panic("given T instance must not be nil")
1517
}
1618
GetHelper(t).Helper()
1719
return &verifier{t: t}
1820
}
1921

22+
type VerifierAndEnsurer interface {
23+
Verifier
24+
Ensure(string) Verifier
25+
}
26+
2027
type Verifier interface {
2128
Verify(actuals ...any) Asserter
2229
}
2330

2431
type verifier struct {
25-
t T
32+
t T
33+
desc string
34+
}
35+
36+
//go:noinline
37+
func (v *verifier) Ensure(description string) Verifier {
38+
GetHelper(v.t).Helper()
39+
v.desc = description
40+
return v
2641
}
2742

2843
//go:noinline
2944
func (v *verifier) Verify(actuals ...any) Asserter {
3045
GetHelper(v.t).Helper()
31-
return &asserter{t: v.t, actuals: actuals}
46+
return &asserter{t: v.t, desc: v.desc, actuals: actuals}
3247
}
3348

3449
type Asserter interface {
@@ -38,6 +53,7 @@ type Asserter interface {
3853
type asserter struct {
3954
t T
4055
actuals []any
56+
desc string
4157
}
4258

4359
//go:noinline
@@ -46,6 +62,7 @@ func (a *asserter) Will(m Matcher) Assertion {
4662

4763
aa := &assertion{
4864
t: a.t,
65+
desc: a.desc,
4966
location: nearestLocation(),
5067
actuals: a.actuals,
5168
matcher: m,
@@ -75,6 +92,7 @@ type assertion struct {
7592
contain bool
7693
cleanup []func()
7794
evaluated bool
95+
desc string
7896
}
7997

8098
//go:noinline
@@ -284,6 +302,11 @@ func (a *assertion) Failed() bool {
284302
func (a *assertion) Fatalf(format string, args ...any) {
285303
GetHelper(a).Helper()
286304

305+
if a.desc != "" {
306+
f := strings.ToLower(string(format[0])) + format[1:]
307+
format = a.desc + " failed: " + f
308+
}
309+
287310
if a.contain {
288311
panic(internal.FormatAndArgs{Format: &format, Args: args})
289312
} else {

asserter_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ func TestWith(t *testing.T) {
2222
}()
2323
With(nil)
2424
})
25+
t.Run("description propagated to failure message", func(t *testing.T) {
26+
t.Parallel()
27+
mt := NewMockT(t)
28+
defer mt.Verify(FailureVerifier("^user feature failed: unexpected.*"))
29+
With(mt).Ensure("user feature").Verify(1).Will(EqualTo(2)).OrFail()
30+
})
2531
}
2632

2733
func TestCorrectActualsPassedToMatcher(t *testing.T) {

0 commit comments

Comments
 (0)