| 1 |
// Package qt implements assertions and other helpers wrapped around the |
| 2 |
// standard library's testing types. |
| 3 |
package qt |
| 4 |
|
| 5 |
import ( |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
// Assert checks that the provided argument passes the given check and calls |
| 10 |
// tb.Fatal otherwise, including any Comment arguments in the failure. |
| 11 |
func Assert(t testing.TB, checker Checker, comments ...Comment) bool { |
| 12 |
t.Helper() |
| 13 |
return check(t, checkParams{ |
| 14 |
fail: t.Fatal, |
| 15 |
checker: checker, |
| 16 |
comments: comments, |
| 17 |
}) |
| 18 |
} |
| 19 |
|
| 20 |
// Check checks that the provided argument passes the given check and calls |
| 21 |
// tb.Error otherwise, including any Comment arguments in the failure. |
| 22 |
func Check(t testing.TB, checker Checker, comments ...Comment) bool { |
| 23 |
t.Helper() |
| 24 |
return check(t, checkParams{ |
| 25 |
fail: t.Error, |
| 26 |
checker: checker, |
| 27 |
comments: comments, |
| 28 |
}) |
| 29 |
} |
| 30 |
|
| 31 |
func check(t testing.TB, p checkParams) bool { |
| 32 |
t.Helper() |
| 33 |
rp := reportParams{ |
| 34 |
comments: p.comments, |
| 35 |
} |
| 36 |
|
| 37 |
// Allow checkers to annotate messages. |
| 38 |
note := func(key string, value any) { |
| 39 |
rp.notes = append(rp.notes, note{ |
| 40 |
key: key, |
| 41 |
value: value, |
| 42 |
}) |
| 43 |
} |
| 44 |
|
| 45 |
// Ensure that we have a checker. |
| 46 |
if p.checker == nil { |
| 47 |
p.fail(report(BadCheckf("nil checker provided"), rp)) |
| 48 |
return false |
| 49 |
} |
| 50 |
rp.args = p.checker.Args() |
| 51 |
|
| 52 |
// Run the check. |
| 53 |
if err := p.checker.Check(note); err != nil { |
| 54 |
p.fail(report(err, rp)) |
| 55 |
return false |
| 56 |
} |
| 57 |
return true |
| 58 |
} |
| 59 |
|
| 60 |
type checkParams struct { |
| 61 |
fail func(...any) |
| 62 |
checker Checker |
| 63 |
comments []Comment |
| 64 |
} |