Skip to content

Commit cac3341

Browse files
committed
feat: add delegation functions.
1 parent 4949508 commit cac3341

File tree

3 files changed

+223
-1
lines changed

3 files changed

+223
-1
lines changed

assertion.go

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
package assert
22

3-
import "testing"
3+
import (
4+
"testing"
5+
"time"
6+
)
47

58
type Assertion struct {
69
t *testing.T
710
}
811

12+
// #########################
13+
// ## Assertion Functions ##
14+
// #########################
15+
916
// New returns an assertion instance for verifying invariants.
1017
func New(t *testing.T) *Assertion {
1118
a := new(Assertion)
1219

20+
if t == nil {
21+
panic("parameter t is required")
22+
}
1323
a.t = t
1424

1525
return a
@@ -44,3 +54,150 @@ func (a *Assertion) Panic(fn func(), message ...string) (err error) {
4454
func (a *Assertion) NotPanic(fn func(), message ...string) (err error) {
4555
return NotPanic(a.t, fn, message...)
4656
}
57+
58+
// ##########################
59+
// ## Delegation Functions ##
60+
// ##########################
61+
62+
// Cleanup registers a function to be called when the test (or subtest) and all its subtests
63+
// complete. Cleanup functions will be called in last added, first called order.
64+
func (a *Assertion) Cleanup(f func()) {
65+
a.t.Cleanup(f)
66+
}
67+
68+
// Deadline reports the time at which the test binary will have exceeded the timeout specified by
69+
// the -timeout flag.
70+
//
71+
// The ok result is false if the -timeout flag indicates “no timeout” (0).
72+
func (a *Assertion) Deadline() (deadline time.Time, ok bool) {
73+
return a.t.Deadline()
74+
}
75+
76+
// Error is equivalent to Log followed by Fail.
77+
func (a *Assertion) Error(args ...any) {
78+
a.t.Error(args...)
79+
}
80+
81+
// Errorf is equivalent to Logf followed by Fail.
82+
func (a *Assertion) Errorf(format string, args ...any) {
83+
a.t.Errorf(format, args...)
84+
}
85+
86+
// Fail marks the function as having failed but continues execution.
87+
func (a *Assertion) Fail() {
88+
a.t.Fail()
89+
}
90+
91+
// FailNow marks the function as having failed and stops its execution by calling runtime.Goexit
92+
// (which then runs all deferred calls in the current goroutine). Execution will continue at the
93+
// next test or benchmark. FailNow must be called from the goroutine running the test or benchmark
94+
// function, not from other goroutines created during the test. Calling FailNow does not stop
95+
// those other goroutines.
96+
func (a *Assertion) FailNow() {
97+
a.t.FailNow()
98+
}
99+
100+
// Failed reports whether the function has failed.
101+
func (a *Assertion) Failed() bool {
102+
return a.t.Failed()
103+
}
104+
105+
// Fatal is equivalent to Log followed by FailNow.
106+
func (a *Assertion) Fatal(args ...any) {
107+
a.t.Fatal(args...)
108+
}
109+
110+
// Fatalf is equivalent to Logf followed by FailNow.
111+
func (a *Assertion) Fatalf(format string, args ...any) {
112+
a.t.Fatalf(format, args...)
113+
}
114+
115+
// Helper marks the calling function as a test helper function. When printing file and line
116+
// information, that function will be skipped. Helper may be called simultaneously from multiple
117+
// goroutines.
118+
func (a *Assertion) Helper() {
119+
a.t.Helper()
120+
}
121+
122+
// Log formats its arguments using default formatting, analogous to Println, and records the text
123+
// in the error log. For tests, the text will be printed only if the test fails or the -test.v
124+
// flag is set. For benchmarks, the text is always printed to avoid having performance depend on
125+
// the value of the -test.v flag.
126+
func (a *Assertion) Log(args ...any) {
127+
a.t.Log(args...)
128+
}
129+
130+
// Logf formats its arguments according to the format, analogous to Printf, and records the text in
131+
// the error log. A final newline is added if not provided. For tests, the text will be printed
132+
// only if the test fails or the -test.v flag is set. For benchmarks, the text is always printed to
133+
// avoid having performance depend on the value of the -test.v flag.
134+
func (a *Assertion) Logf(format string, args ...any) {
135+
a.t.Logf(format, args...)
136+
}
137+
138+
// Name returns the name of the running (sub-) test or benchmark.
139+
//
140+
// The name will include the name of the test along with the names of any nested sub-tests. If two
141+
// sibling sub-tests have the same name, Name will append a suffix to guarantee the returned name
142+
// is unique.
143+
func (a *Assertion) Name() string {
144+
return a.t.Name()
145+
}
146+
147+
// Parallel signals that this test is to be run in parallel with (and only with) other parallel
148+
// tests. When a test is run multiple times due to use of -test.count or -test.cpu, multiple
149+
// instances of a single test never run in parallel with each other.
150+
func (a *Assertion) Parallel() {
151+
a.t.Parallel()
152+
}
153+
154+
// Run runs f as a subtest of t called name. It runs f in a separate goroutine and blocks until f
155+
// returns or calls t.Parallel to become a parallel test. Run reports whether f succeeded (or at
156+
// least did not fail before calling t.Parallel).
157+
//
158+
// Run may be called simultaneously from multiple goroutines, but all such calls must return before
159+
// the outer test function for t returns.
160+
func (a *Assertion) Run(name string, f func(*testing.T)) bool {
161+
return a.t.Run(name, f)
162+
}
163+
164+
// Setenv calls os.Setenv(key, value) and uses Cleanup to restore the environment variable to its
165+
// original value after the test.
166+
//
167+
// Because Setenv affects the whole process, it cannot be used in parallel tests or tests with
168+
// parallel ancestors.
169+
func (a *Assertion) Setenv(key, value string) {
170+
a.t.Setenv(key, value)
171+
}
172+
173+
// Skip is equivalent to Log followed by SkipNow.
174+
func (a *Assertion) Skip(args ...any) {
175+
a.t.Skip(args...)
176+
}
177+
178+
// SkipNow marks the test as having been skipped and stops its execution by calling runtime.Goexit.
179+
// If a test fails (see Error, Errorf, Fail) and is then skipped, it is still considered to have
180+
// failed. Execution will continue at the next test or benchmark. See also FailNow. SkipNow must be
181+
// called from the goroutine running the test, not from other goroutines created during the test.
182+
// Calling SkipNow does not stop those other goroutines.
183+
func (a *Assertion) SkipNow() {
184+
a.t.SkipNow()
185+
}
186+
187+
// Skipf is equivalent to Logf followed by SkipNow.
188+
func (a *Assertion) Skipf(format string, args ...any) {
189+
a.t.Skipf(format, args...)
190+
}
191+
192+
// Skipped reports whether the test was skipped.
193+
func (a *Assertion) Skipped() bool {
194+
return a.t.Skipped()
195+
}
196+
197+
// TempDir returns a temporary directory for the test to use. The directory is automatically
198+
// removed by Cleanup when the test and all its subtests complete. Each subsequent call to
199+
// t.TempDir returns a unique directory; if the directory creation fails, TempDir terminates the
200+
// test by calling Fatal.
201+
func (a *Assertion) TempDir() string {
202+
return a.t.TempDir()
203+
}

assertion_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package assert
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestNewAssertion(t *testing.T) {
9+
Panic(t, func() {
10+
New(nil)
11+
})
12+
13+
NotPanic(t, func() {
14+
New(new(testing.T))
15+
})
16+
}
17+
18+
func TestFailed(t *testing.T) {
19+
testFailed(t, func(a *Assertion) {
20+
// do nothing
21+
}, false)
22+
testFailed(t, func(a *Assertion) {
23+
a.Log("test")
24+
}, false)
25+
testFailed(t, func(a *Assertion) {
26+
a.Logf("test")
27+
}, false)
28+
testFailed(t, func(a *Assertion) {
29+
a.Error("test")
30+
}, true)
31+
testFailed(t, func(a *Assertion) {
32+
a.Errorf("test")
33+
}, true)
34+
testFailed(t, func(a *Assertion) {
35+
a.Fail()
36+
}, true)
37+
}
38+
39+
func testFailed(t *testing.T, f func(a *Assertion), failed bool) {
40+
assert := New(new(testing.T))
41+
42+
f(assert)
43+
DeepEqual(t, assert.Failed(), failed)
44+
}
45+
46+
func TestRun(t *testing.T) {
47+
assert := New(t)
48+
49+
assert.Setenv("env", "assert test")
50+
assert.Run("test Run", func(t *testing.T) {
51+
DeepEqual(t, t.Name(), "TestRun/test_Run")
52+
DeepEqual(t, os.Getenv("env"), "assert test")
53+
})
54+
}

error_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package assert
2+
3+
import "testing"
4+
5+
func TestAssertionError(t *testing.T) {
6+
err := newAssertionError("default message")
7+
DeepEqual(t, err.Error(), "assert error: default message")
8+
9+
err = newAssertionError("default message", "custom message")
10+
DeepEqual(t, err.Error(), "custom message")
11+
}

0 commit comments

Comments
 (0)