summaryrefslogtreecommitdiff
path: root/assert.go
blob: 8a38e9ef2eb4d4788a29fac4027a65b4a380a2e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
}