| 1 |
// Package check provide helpers to complement Go testing package. |
| 2 |
// |
| 3 |
// # Features |
| 4 |
// |
| 5 |
// This package is like testify/assert on steroids. :) |
| 6 |
// |
| 7 |
// - Compelling output from failed tests: |
| 8 |
// - Very easy-to-read dumps for expected and actual values. |
| 9 |
// - Same text diff you loved in testify/assert. |
| 10 |
// - Statistics with amount of passed/failed checks. |
| 11 |
// - Colored output in terminal. |
| 12 |
// - 100% compatible with testing package - check package just provide |
| 13 |
// convenient wrappers for [*testing.T] methods and doesn't introduce |
| 14 |
// new concepts like BDD, custom test suite or unusual execution flow. |
| 15 |
// - All checks you may ever need! :) |
| 16 |
// - Very easy to add your own check functions. |
| 17 |
// - Concise, handy and consistent API, without dot-import! |
| 18 |
// |
| 19 |
// # Quickstart |
| 20 |
// |
| 21 |
// Wrap each (including subtests) [*testing.T]/[*testing.B]/[*testing.F] |
| 22 |
// using [Must] and write tests as usually with testing package. |
| 23 |
// Call new methods provided by this package to have more clean/concise test code |
| 24 |
// and cool dump/diff. |
| 25 |
// |
| 26 |
// [Must] stops the test on the first failed check (like testify/require). |
| 27 |
// Use [New] instead for the softer, testify/assert-like behavior |
| 28 |
// where a failed check doesn't stop the test. |
| 29 |
// |
| 30 |
// import "github.com/powerman/check" |
| 31 |
// |
| 32 |
// func TestSomething(tt *testing.T) { |
| 33 |
// tt.Parallel() |
| 34 |
// t := check.Must(tt) |
| 35 |
// t.Equal(2, 2) |
| 36 |
// t.Log("You can use new t just like usual *testing.T") |
| 37 |
// tt.Run("Subtests/Parallel example", func(tt *testing.T) { |
| 38 |
// tt.Parallel() |
| 39 |
// t := check.Must(tt) |
| 40 |
// t.NotEqual(2, 3, "should not be 3!") |
| 41 |
// obj, err := NewObj() |
| 42 |
// if t.Nil(err) { |
| 43 |
// t.Match(obj.field, `^\d+$`) |
| 44 |
// } |
| 45 |
// }) |
| 46 |
// } |
| 47 |
// |
| 48 |
// To get optional statistics about executed checkers add: |
| 49 |
// |
| 50 |
// func TestMain(m *testing.M) { check.TestMain(m) } |
| 51 |
// |
| 52 |
// [TB] (returned by [New]/[Must]) doesn't provide Run/Parallel: |
| 53 |
// call tb.Run()/tb.Parallel() on the original |
| 54 |
// [*testing.T]/[*testing.B]/[*testing.F] before wrapping it |
| 55 |
// (this also satisfies the paralleltest linter). |
| 56 |
// |
| 57 |
// [C] (returned by the legacy [T]) is a soft-mode by default, |
| 58 |
// [*testing.T]-only compatibility shell kept for old code: |
| 59 |
// it behaves exactly like it always did, |
| 60 |
// including direct access to the wrapped [*testing.T] via its T field, |
| 61 |
// and does provide Run/Parallel. New code should prefer [New]/[Must]. |
| 62 |
// |
| 63 |
// # Hints |
| 64 |
// |
| 65 |
// ★ How to check for errors: |
| 66 |
// |
| 67 |
// // If you just want nil: |
| 68 |
// t.Nil(err) |
| 69 |
// t.Err(err, nil) |
| 70 |
// |
| 71 |
// // Check for (absence of) concrete (possibly wrapped) error: |
| 72 |
// t.Err(err, io.EOF) |
| 73 |
// t.NotErr(err, io.EOF) // nil is not io.EOF, so it's ok too |
| 74 |
// |
| 75 |
// // When need to match by error's text: |
| 76 |
// t.Match(err, `file.*permission`) |
| 77 |
// |
| 78 |
// // Use Equal ONLY when checking for same instance: |
| 79 |
// t.Equal(io.EOF, io.EOF) // this works |
| 80 |
// t.Equal(io.EOF, errors.New("EOF")) // this doesn't work! |
| 81 |
// t.Err(io.EOF, errors.New("EOF")) // this works |
| 82 |
// t.DeepEqual(io.EOF, errors.New("EOF")) // this works too |
| 83 |
// |
| 84 |
// // ErrIs/ErrAs are pure errors.Is/errors.As wrappers: |
| 85 |
// t.ErrIs(err, io.EOF) // errors.Is(err, io.EOF) |
| 86 |
// t.ErrAs(err, &targetType) // errors.As(err, &targetType) |
| 87 |
// |
| 88 |
// When to use which: |
| 89 |
// |
| 90 |
// - Err — same type and value (unwraps to root, compares by value), |
| 91 |
// support for extra custom error types (e.g. gRPC status or validator.FieldError) |
| 92 |
// - ErrIs — standard [errors.Is] (not value comparison) |
| 93 |
// - ErrAs — extract the first matching error type |
| 94 |
// - Match — check by error text against a regexp |
| 95 |
// |
| 96 |
// ★ Each check returns bool, so you can easily skip problematic code: |
| 97 |
// |
| 98 |
// if t.Nil(err) { |
| 99 |
// t.Match(obj.field, `^\d+$`) |
| 100 |
// } |
| 101 |
// |
| 102 |
// ★ You can turn any soft ([New], legacy [T]) check into assertion to stop test immediately: |
| 103 |
// |
| 104 |
// t.Must(t.Nil(err)) |
| 105 |
// |
| 106 |
// ★ You can turn all soft checks into assertions to stop test immediately (or just use [Must]): |
| 107 |
// |
| 108 |
// t = t.MustAll() |
| 109 |
// t.Nil(err) |
| 110 |
// |
| 111 |
// ★ You can provide extra description to each check: |
| 112 |
// |
| 113 |
// t.Equal(got, want, "Just msg: will Print(), % isn't special") |
| 114 |
// t.Equal(got, want, "Msg with args: will Printf(): %v", extra) |
| 115 |
// |
| 116 |
// ★ There are short synonyms for checks implementing usual ==, !=, etc.: |
| 117 |
// |
| 118 |
// t.EQ(got, want) // same as t.Equal |
| 119 |
// t.NE(got, want) // same as t.NotEqual |
| 120 |
// t.LT(got, want) // same as t.Less |
| 121 |
// t.LE(got, want) // same as t.LessOrEqual |
| 122 |
// t.GT(got, want) // same as t.Greater |
| 123 |
// t.GE(got, want) // same as t.GreaterOrEqual |
| 124 |
// |
| 125 |
// ★ If you need custom check, which isn't available out-of-box - see |
| 126 |
// [Should] checker, it'll let you plug in your own checker with ease. |
| 127 |
// |
| 128 |
// ★ It will panic when called with arg of wrong type - because this |
| 129 |
// means bug in your test. |
| 130 |
// |
| 131 |
// ★ If you don't see colors in `go test` output it may happen because |
| 132 |
// either you're not running in a terminal or your $TERM is set to "dumb" |
| 133 |
// (or empty). To force colored output set one of these variables: |
| 134 |
// |
| 135 |
// export FORCE_COLOR=1 |
| 136 |
// export CLICOLOR_FORCE=1 |
| 137 |
// export GO_TEST_COLOR=1 |
| 138 |
// |
| 139 |
// To disable colors (overrides all other variables): |
| 140 |
// |
| 141 |
// export NO_COLOR=1 |
| 142 |
// |
| 143 |
// ★ With the legacy [T] (whose [C] does provide Run/Parallel), |
| 144 |
// if you use t.Parallel() inside a subtest, |
| 145 |
// prefer calling tt.Parallel() on the original *[testing.T] before wrapping with check.T() — |
| 146 |
// this satisfies the paralleltest linter: |
| 147 |
// |
| 148 |
// t.Run("subtest", func(tt *testing.T) { |
| 149 |
// tt.Parallel() |
| 150 |
// t := check.T(tt) |
| 151 |
// t.Equal(2, 2) |
| 152 |
// }) |
| 153 |
// |
| 154 |
// ★ Inject an application base context (e.g. one carrying a slog handler) |
| 155 |
// into a test on top of the per-test cancellation/deadline |
| 156 |
// [testing.TB.Context] already provides: |
| 157 |
// |
| 158 |
// t := check.Must(tt).MergeContext(appCtx) |
| 159 |
// t.Context() // merged values and cancellation from both contexts |
| 160 |
// |
| 161 |
// ★ Enable Protobuf message comparison and gRPC status error comparison by: |
| 162 |
// |
| 163 |
// import _ "github.com/powerman/checkgrpc" |
| 164 |
// |
| 165 |
// This enables proto.Equal for protobuf messages in [DeepEqual]/[NotDeepEqual] |
| 166 |
// and gRPC status comparison in [Err]/[NotErr]. |
| 167 |
// |
| 168 |
// # Contents |
| 169 |
// |
| 170 |
// Constructors: |
| 171 |
// |
| 172 |
// New Must T |
| 173 |
// |
| 174 |
// Other special methods (assertion, context, custom checkers, etc.). |
| 175 |
// |
| 176 |
// Context MergeContext |
| 177 |
// Error Errorf |
| 178 |
// Fatal Fatalf |
| 179 |
// Fail FailNow |
| 180 |
// Must MustAll |
| 181 |
// Should |
| 182 |
// TODO |
| 183 |
// |
| 184 |
// Everything else are just trivial (mostly) checkers which works in |
| 185 |
// obvious way and accept values of any types which makes sense (and |
| 186 |
// panics on everything else). |
| 187 |
// |
| 188 |
// Nil NotNil |
| 189 |
// Zero NotZero |
| 190 |
// True False |
| 191 |
// |
| 192 |
// Equal NotEqual EQ NE |
| 193 |
// DeepEqual NotDeepEqual |
| 194 |
// Err NotErr |
| 195 |
// ErrIs NotErrIs |
| 196 |
// ErrAs NotErrAs |
| 197 |
// BytesEqual NotBytesEqual |
| 198 |
// JSONEqual |
| 199 |
// |
| 200 |
// Greater LessOrEqual GT LE |
| 201 |
// Less GreaterOrEqual LT GE |
| 202 |
// Between NotBetween |
| 203 |
// BetweenOrEqual NotBetweenOrEqual |
| 204 |
// InDelta NotInDelta |
| 205 |
// InSMAPE NotInSMAPE |
| 206 |
// |
| 207 |
// Len NotLen |
| 208 |
// Match NotMatch |
| 209 |
// HasPrefix NotHasPrefix |
| 210 |
// HasSuffix NotHasSuffix |
| 211 |
// HasKey NotHasKey |
| 212 |
// Contains NotContains |
| 213 |
// SortEqual NotSortEqual |
| 214 |
// Subset NotSubset |
| 215 |
// |
| 216 |
// HasType NotHasType |
| 217 |
// Implements NotImplements |
| 218 |
// |
| 219 |
// FileExists NotFileExists |
| 220 |
// DirExists NotDirExists |
| 221 |
// |
| 222 |
// Panic NotPanic |
| 223 |
// PanicMatch PanicNotMatch |
| 224 |
package check |