-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_test.go
117 lines (102 loc) · 2.65 KB
/
test_test.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
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
package gfn_test
import (
"fmt"
"math"
"runtime/debug"
"strings"
"testing"
. "github.com/suchen-sci/gfn"
)
func fail(t *testing.T, failMsg string, tags ...string) {
t.Helper()
if len(tags) == 0 {
t.Error(failMsg)
} else {
t.Errorf("%s, tags: %s", failMsg, strings.Join(tags, ", "))
}
}
func AssertEqual[T comparable](t *testing.T, expected T, actual T, tags ...string) {
t.Helper()
if expected != actual {
fail(t, fmt.Sprintf("expected: %v, actual: %v", expected, actual), tags...)
}
}
func AssertFloatEqual[T Float](t *testing.T, expected T, actual T, tags ...string) {
t.Helper()
if math.Abs(float64(expected-actual)) > 0.0001 {
fail(t, fmt.Sprintf("expected: %v, actual: %v", expected, actual), tags...)
}
}
func AssertSliceEqual[T comparable](t *testing.T, expected []T, actual []T, tags ...string) {
t.Helper()
if expected == nil || actual == nil {
if expected == nil && actual == nil {
return
}
fail(t, fmt.Sprintf("expected: %v, actual: %v", expected, actual), tags...)
return
}
if len(expected) != len(actual) {
fail(t, fmt.Sprintf("expected: %v, actual: %v", expected, actual), tags...)
return
}
for i := 0; i < len(expected); i++ {
if expected[i] != actual[i] {
fail(t, fmt.Sprintf("expected: %v, actual: %v", expected, actual), tags...)
return
}
}
}
func AssertMapEqual[T comparable, V comparable](t *testing.T, expected map[T]V, actual map[T]V, tags ...string) {
t.Helper()
if expected == nil || actual == nil {
if expected == nil && actual == nil {
return
}
fail(t, fmt.Sprintf("expected: %v, actual: %v", expected, actual), tags...)
return
}
if len(expected) != len(actual) {
fail(t, fmt.Sprintf("expected: %v, actual: %v", expected, actual), tags...)
return
}
for k, v := range expected {
if actualV, ok := actual[k]; !ok || v != actualV {
fail(t, fmt.Sprintf("expected: %v, actual: %v", expected, actual), tags...)
return
}
}
}
func AssertTrue(t *testing.T, actual bool, tags ...string) {
t.Helper()
if !actual {
fail(t, "expected: true, actual: false", tags...)
}
}
func AssertFalse(t *testing.T, actual bool, tags ...string) {
t.Helper()
if actual {
fail(t, "expected: false, actual: true", tags...)
}
}
func AssertPanics(t *testing.T, fn func(), tags ...string) {
t.Helper()
defer func() {
t.Helper()
if r := recover(); r == nil {
fail(t, "expected: panic, actual: not panic", tags...)
}
}()
fn()
}
func AssertNotPanics(t *testing.T, fn func(), tags ...string) {
t.Helper()
defer func() {
t.Helper()
if r := recover(); r != nil {
stack := string(debug.Stack())
fail(t, fmt.Sprintf("expected: not panic, actual: %v, %v", r, stack), tags...)
}
}()
fn()
}