summaryrefslogtreecommitdiff
path: root/assert.go
diff options
context:
space:
mode:
authortjp <tjp@ctrl-c.club>2024-06-02 20:04:57 -0600
committertjp <tjp@ctrl-c.club>2024-06-02 20:04:57 -0600
commitb7e284c68c7f34e23ab30ffe95d364f22b6ced4e (patch)
tree743cc1cddb07e5f7e1e82f23d98cec28a38ac94b /assert.go
initial commitmain
[Not]Equal checks in `assert` and `must` packages
Diffstat (limited to 'assert.go')
-rw-r--r--assert.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/assert.go b/assert.go
new file mode 100644
index 0000000..8a38e9e
--- /dev/null
+++ b/assert.go
@@ -0,0 +1,55 @@
+// package assert contains functions for checking values in unit tests.
+//
+// The functions in this package will log messages and mark tests as failed,
+// not bail out of the test immediately, but rather return a boolean of whether
+// or not the check passed.
+//
+// For analogues which stop the test and lose the boolean return value, see
+// package assert/must.
+package assert
+
+import (
+ "testing"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+// Equal asserts that two values compare as equal.
+func Equal(t testing.TB, actual, expect any) bool {
+ t.Helper()
+ if !cmp.Equal(actual, expect) {
+ t.Errorf(`
+Equal check failed
+------------------
+actual:
+%v
+
+expect:
+%v
+
+diff:
+%s`[1:],
+ actual,
+ expect,
+ cmp.Diff(expect, actual),
+ )
+ return false
+ }
+ return true
+}
+
+// NotEqual asserts that two values compare as unequal.
+func NotEqual(t testing.TB, actual, expect any) bool {
+ t.Helper()
+ if cmp.Equal(actual, expect) {
+ t.Errorf(`
+NotEqual check failed
+---------------------
+value:
+%v`[1:],
+ expect,
+ )
+ return false
+ }
+ return true
+}