From b7e284c68c7f34e23ab30ffe95d364f22b6ced4e Mon Sep 17 00:00:00 2001 From: tjp Date: Sun, 2 Jun 2024 20:04:57 -0600 Subject: initial commit [Not]Equal checks in `assert` and `must` packages --- assert.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 +++++ go.sum | 2 ++ must/must.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 assert.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 must/must.go 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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a1c5db6 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.tjp.lol/assert + +go 1.22.3 + +require github.com/google/go-cmp v0.6.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5a8d551 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/must/must.go b/must/must.go new file mode 100644 index 0000000..67a58dd --- /dev/null +++ b/must/must.go @@ -0,0 +1,30 @@ +// package must contains functions for checking values in unit tests. +// +// The functions in this package handle check failures by logging a message +// containing the relevant values, and aborting the test immediately as failed. +// +// For analogues which allow the remainder of the test to run, see package +// assert. +package must + +import ( + "testing" + + "git.tjp.lol/assert" +) + +// Equal asserts that two values compare as equal. +func Equal(t testing.TB, actual, expect any) { + t.Helper() + if !assert.Equal(t, actual, expect) { + t.FailNow() + } +} + +// NotEqual asserts that two values compare as unequal. +func NotEqual(t testing.TB, actual, expect any) { + t.Helper() + if !assert.NotEqual(t, actual, expect) { + t.FailNow() + } +} -- cgit v1.2.3