aboutsummaryrefslogtreecommitdiffstats
path: root/assert.go
blob: 897d8c5f8049358a4de2295a68cc1d1efe7dbda6 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package checkmate

import (
	"fmt"

	"github.com/google/go-cmp/cmp"
)

// AssertDeepEqual checks if two values are deeply equal using Google's
// go-cmp cmp.Equal. If they are not equal, it logs the differences
// using cmp.Diff. An optional message and arguments for any format
// placeholders in that message can be provided for if a failure occurs.
func AssertDeepEqual(t TestingT, actual, expected any, msgAndArgs ...any) {
	if ht, ok := t.(helperT); ok {
		ht.Helper()
	}

	if diff := cmp.Diff(expected, actual); diff != "" {
		if len(msgAndArgs) == 0 {
			msgAndArgs = []any{"mismatch (-expected +actual):\n%s", diff}
		}
		Assert(t, false, msgAndArgs...)
	}
}

// AssertEqual checks if two primitive values are equal. An optional
// message and arguments for any format placeholders in that message
// can be provided for if a failure occurs.
func AssertEqual(t TestingT, actual, expected any, msgAndArgs ...any) {
	if ht, ok := t.(helperT); ok {
		ht.Helper()
	}

	if actual != expected {
		if len(msgAndArgs) == 0 {
			msgAndArgs = []any{"expected %v to equal %v", expected, actual}
		}
		Assert(t, false, msgAndArgs...)
	}
}

// AssertNotEqual checks if two values are not equal. It fails the test if
// the values are equal. An optional message and arguments for any format
// placeholders in that message can be provided if a failure occurs.
func AssertNotEqual(t TestingT, actual, expected any, msgAndArgs ...any) {
	if ht, ok := t.(helperT); ok {
		ht.Helper()
	}

	if actual == expected {
		if len(msgAndArgs) == 0 {
			msgAndArgs = []any{"expected %v to not equal %v", expected, actual}
		}
		Assert(t, false, msgAndArgs...)
	}
}

// AssertLenEqual checks if the length of a given slice or array equals the
// expected length. It fails the test if the lengths are not equal. An optional
// message and arguments for any format placeholders in that message can be provided
// if a failure occurs.
func AssertLenEqual[T any](t TestingT, l []T, expectedLen int, msgAndArgs ...any) {
	if ht, ok := t.(helperT); ok {
		ht.Helper()
	}

	if len(msgAndArgs) == 0 {
		msgAndArgs = []any{"expected %v to have len %d, got len %d", l, len(l), expectedLen}
	}
	Assert(t, len(l) == expectedLen, msgAndArgs...)
}

// Assert checks a boolean condition and fails the test if the condition is false.
// This is a general-purpose assertion function used to validate if a condition holds true.
// An optional message and arguments for any format placeholders in that message
// can be provided if a failure occurs.
func Assert(t TestingT, condition bool, msgAndArgs ...any) {
	if ht, ok := t.(helperT); ok {
		ht.Helper()
	}
	if len(msgAndArgs) == 0 {
		msgAndArgs = []any{"assertion failed"}
	}
	passed := Check(t, condition, msgAndArgs...)
	if !passed {
		t.FailNow()
	}
}

// Check evaluates a boolean condition and logs a message if the condition is false.
// Unlike Assert, it does not stop the test when the condition is false but returns
// a boolean indicating the result. This function is useful for cases where you want
// to perform multiple checks in a single test function without stopping the test
// execution after the first failure. An optional message and arguments for any format
// placeholders in that message can be provided if the check fails.
func Check(t TestingT, condition bool, msgAndArgs ...any) bool {
	if ht, ok := t.(helperT); ok {
		ht.Helper()
	}

	if !condition {
		message := "check failed"
		if len(msgAndArgs) > 0 {
			switch msgAndArgs[0].(type) {
			case string:
				message = fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
			default:
				t.Log("checkmate: assertion called with a non-string message, using default message")
			}
		}
		t.Log(message)
		return false
	}
	return true
}

// The subset of testing.T which is used by the
// checkmate package.
type TestingT interface {
	Log(args ...any)
	FailNow()
}

type helperT interface {
	Helper()
}