-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
111 lines (86 loc) · 2.98 KB
/
testing.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
package util
import (
"bytes"
"runtime"
"slices"
"testing"
"time"
)
type RESULT struct {
S interface{}
}
type ERROR struct {
S string
}
func Error(s string) ERROR {
return ERROR{S: s}
}
func Result(s interface{}) RESULT {
return RESULT{S: s}
}
func Assert_no_error(t *testing.T, err error, skip_level int) {
t.Helper()
_, _, line_number, _ := runtime.Caller(skip_level) // lol you have to use this to get the line number lmao
if err != nil {
t.Fatal("Line number:", line_number, "Test failed. Expected no error but an error occurred:", err)
}
}
func Assert_error_equals(t *testing.T, err error, expected string, skip_level int) {
t.Helper()
_, _, line_number, _ := runtime.Caller(skip_level)
if err == nil {
t.Fatal("Line number:", line_number, "Test failed. Expected error but no error occurred.")
}
if err.Error() != expected {
t.Fatal("Line number:", line_number, "Test failed. Expected error", expected, "got", err.Error())
}
}
func Assert_result_equals_bytes(t *testing.T, actual []byte, err error, expected string, skip_level int) {
t.Helper()
_, _, line_number, _ := runtime.Caller(skip_level)
if err != nil {
t.Fatal("Line number:", line_number, "Fail. Expected no error, got", err.Error())
}
if !bytes.Equal(actual, []byte(expected)) {
t.Fatal("Line number:", line_number, "Fail. Strings not equal. Expected", expected, "got", string(actual))
}
}
func Assert_result_equals_time(t *testing.T, actual time.Time, err error, expected time.Time, line_number int) {
t.Helper()
if err != nil {
t.Fatal("Line number:", line_number, "Fail. Expected no error, got", err.Error())
}
if !actual.Equal(expected) {
t.Fatal("Line number:", line_number, "Fail. Times not equal. Expected:", expected, "got:", actual)
}
}
func Assert_result_equals_bool(t *testing.T, actual bool, err error, expected bool, skip_level int) {
t.Helper()
_, _, line_number, _ := runtime.Caller(skip_level)
if err != nil {
t.Fatal("Line number:", line_number, "Fail. Expected no error, got", err.Error())
}
if actual != expected {
t.Fatal("Line number:", line_number, "Fail. Booleans not equal. Expected:", expected, "got:", actual)
}
}
func Assert_result_equals_interface(t *testing.T, actual interface{}, err error, expected interface{}, skip_level int) {
t.Helper()
_, _, line_number, _ := runtime.Caller(skip_level)
if err != nil {
t.Fatal("Line number:", line_number, "Fail. Expected no error, got", err.Error())
}
if actual != expected {
t.Fatal("Line number:", line_number, "Fail. Interfaces not equal. Expected:", expected, "got:", actual)
}
}
func Assert_result_equals_string_slice(t *testing.T, actual []string, err error, expected []string, skip_level int) {
t.Helper()
_, _, line_number, _ := runtime.Caller(skip_level)
if err != nil {
t.Fatal("Line number:", line_number, "Fail. Expected no error, got", err.Error())
}
if slices.Compare(actual, expected) != 0 {
t.Fatal("Line number:", line_number, "Fail. String slices not equal. Expected:", expected, "got:", actual)
}
}