Skip to content

Commit a538eaa

Browse files
committed
refactor: make Assertion inherience from testing.T.
1 parent 32f1894 commit a538eaa

File tree

3 files changed

+14
-162
lines changed

3 files changed

+14
-162
lines changed

assertion.go

Lines changed: 2 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package assert
22

33
import (
44
"testing"
5-
"time"
65
)
76

87
type Assertion struct {
9-
t *testing.T
8+
*testing.T
109
}
1110

1211
// New returns an assertion instance for verifying invariants.
@@ -16,154 +15,7 @@ func New(t *testing.T) *Assertion {
1615
if t == nil {
1716
panic("parameter t is required")
1817
}
19-
a.t = t
18+
a.T = t
2019

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

equal.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ import (
99
// DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to
1010
// fail if they are not deeply equal, and it doesn't stop the execution.
1111
func (a *Assertion) DeepEqual(actual, expect any, message ...string) error {
12-
return tryDeepEqual(a.t, false, actual, expect, message...)
12+
return tryDeepEqual(a.T, false, actual, expect, message...)
1313
}
1414

1515
// DeepEqualNow tests the deep equality between actual and expect parameters, and it'll stop the
1616
// execution if they are not deeply equal.
1717
func (a *Assertion) DeepEqualNow(actual, expect any, message ...string) error {
18-
return tryDeepEqual(a.t, true, actual, expect, message...)
18+
return tryDeepEqual(a.T, true, actual, expect, message...)
1919
}
2020

2121
// NotDeepEqual tests the deep inequality between actual and expected parameters. It'll set the
2222
// result to fail if they are deeply equal, but it doesn't stop the execution.
2323
func (a *Assertion) NotDeepEqual(actual, expect any, message ...string) error {
24-
return tryNotDeepEqual(a.t, false, actual, expect, message...)
24+
return tryNotDeepEqual(a.T, false, actual, expect, message...)
2525
}
2626

2727
// NotDeepEqualNow tests the deep inequality between actual and expected parameters, and it'll stop
2828
// the execution if they are deeply equal.
2929
func (a *Assertion) NotDeepEqualNow(actual, expect any, message ...string) error {
30-
return tryNotDeepEqual(a.t, true, actual, expect, message...)
30+
return tryNotDeepEqual(a.T, true, actual, expect, message...)
3131
}
3232

3333
// tryDeepEqual try to testing the deeply equality between actual and expect values, and it'll
@@ -60,7 +60,7 @@ func tryNotDeepEqual(t *testing.T, failedNow bool, actual, expect any, message .
6060
// always return false if the value is a bool, an integer, a floating number, a complex, or a
6161
// string.
6262
func (a *Assertion) Nil(val any, message ...string) error {
63-
return tryNil(a.t, false, val, message...)
63+
return tryNil(a.T, false, val, message...)
6464
}
6565

6666
// NilNow tests whether a value is nil or not, and it'll fail when the value is not nil. It will
@@ -69,14 +69,14 @@ func (a *Assertion) Nil(val any, message ...string) error {
6969
//
7070
// This function will set the result to fail, and stop the execution if the value is not nil.
7171
func (a *Assertion) NilNow(val any, message ...string) error {
72-
return tryNil(a.t, true, val, message...)
72+
return tryNil(a.T, true, val, message...)
7373
}
7474

7575
// NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will
7676
// always return true if the value is a bool, an integer, a floating number, a complex, or a
7777
// string.
7878
func (a *Assertion) NotNil(val any, message ...string) error {
79-
return tryNotNil(a.t, false, val, message...)
79+
return tryNotNil(a.T, false, val, message...)
8080
}
8181

8282
// NotNilNow tests whether a value is nil or not, and it'll fail when the value is nil. It will
@@ -85,7 +85,7 @@ func (a *Assertion) NotNil(val any, message ...string) error {
8585
//
8686
// This function will set the result to fail, and stop the execution if the value is nil.
8787
func (a *Assertion) NotNilNow(val any, message ...string) error {
88-
return tryNotNil(a.t, true, val, message...)
88+
return tryNotNil(a.T, true, val, message...)
8989
}
9090

9191
// tryNil try to testing a value is nil or not, and it'll fail the value is nil.

panic.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ import (
88
// Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't
99
// panic.
1010
func (a *Assertion) Panic(fn func(), message ...string) error {
11-
return tryPanic(a.t, false, fn, message...)
11+
return tryPanic(a.T, false, fn, message...)
1212
}
1313

1414
// PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't
1515
// panic, and stop the execution.
1616
func (a *Assertion) PanicNow(fn func(), message ...string) error {
17-
return tryPanic(a.t, true, fn, message...)
17+
return tryPanic(a.T, true, fn, message...)
1818
}
1919

2020
// NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the
2121
// function panic.
2222
func (a *Assertion) NotPanic(fn func(), message ...string) error {
23-
return tryNotPanic(a.t, false, fn, message...)
23+
return tryNotPanic(a.T, false, fn, message...)
2424
}
2525

2626
// NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the
2727
// function panic, and it also stops the execution.
2828
func (a *Assertion) NotPanicNow(fn func(), message ...string) error {
29-
return tryNotPanic(a.t, false, fn, message...)
29+
return tryNotPanic(a.T, false, fn, message...)
3030
}
3131

3232
// tryPanic executes the function fn, and try to catching the panic error. It expect the function

0 commit comments

Comments
 (0)