|
1 | 1 | package assert |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
4 | 7 |
|
5 | 8 | type Assertion struct { |
6 | 9 | t *testing.T |
7 | 10 | } |
8 | 11 |
|
| 12 | +// ######################### |
| 13 | +// ## Assertion Functions ## |
| 14 | +// ######################### |
| 15 | + |
9 | 16 | // New returns an assertion instance for verifying invariants. |
10 | 17 | func New(t *testing.T) *Assertion { |
11 | 18 | a := new(Assertion) |
12 | 19 |
|
| 20 | + if t == nil { |
| 21 | + panic("parameter t is required") |
| 22 | + } |
13 | 23 | a.t = t |
14 | 24 |
|
15 | 25 | return a |
@@ -44,3 +54,150 @@ func (a *Assertion) Panic(fn func(), message ...string) (err error) { |
44 | 54 | func (a *Assertion) NotPanic(fn func(), message ...string) (err error) { |
45 | 55 | return NotPanic(a.t, fn, message...) |
46 | 56 | } |
| 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 | +} |
0 commit comments