-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.go
64 lines (50 loc) · 1.47 KB
/
assert.go
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
package godino
import (
"errors"
"fmt"
"strings"
"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
var ErrAssertion error = errors.New("assertion failed")
func Assert(condition bool, messages ...string) {
message := strings.Join(messages, "\n")
err := errors.New(message)
if len(message) == 0 {
err = ErrAssertion
}
if !condition {
panic(err)
}
}
func AssertEqual[T comparable](x, y T) {
Assert(x == y, fmt.Sprintf("%v does not equal %v", x, y))
}
func AssertTrue(condition bool) {
Assert(condition)
}
func AssertFalse(condition bool) {
Assert(!condition)
}
func AssertGreaterThan[T constraints.Ordered](x, y T) {
Assert(x > y, fmt.Sprintf("%v is not greater than %v", x, y))
}
func AssertGreaterThanOrEqual[T constraints.Ordered](x, y T) {
Assert(x >= y, fmt.Sprintf("%v is not greater than or equal to %v", x, y))
}
func AssertLessThan[T constraints.Ordered](x, y T) {
Assert(x < y, fmt.Sprintf("%v is not less than %v", x, y))
}
func AssertLessThanOrEqual[T constraints.Ordered](x, y T) {
Assert(x <= y, fmt.Sprintf("%v is not less than or equal to %v", x, y))
}
func AssertMapsEqual[M map[K]V, K comparable, V comparable](x, y M) {
Assert(maps.Equal(x, y))
}
func AssertMembersEqual[L ~[]T, T comparable](x, y L) {
Assert(MembersMatch(x, y), fmt.Sprintf("Elements %v do not match elements %v", x, y))
}
func AssertSlicesEqual[L ~[]T, T comparable](x, y L) {
Assert(slices.Equal(x, y), fmt.Sprintf("%v does not equal %v", x, y))
}