forked from tdewolff/test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
132 lines (112 loc) · 3.55 KB
/
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package test
import (
"bytes"
"errors"
"fmt"
"math"
"reflect"
"runtime"
"strings"
"testing"
)
// ErrPlain is the default error that is returned for functions in this package.
var ErrPlain = errors.New("error")
////////////////////////////////////////////////////////////////
func fileline(i int) string {
_, file, line, ok := runtime.Caller(i)
if !ok {
return ""
}
parts := strings.Split(file, "/")
file = parts[len(parts)-1]
return fmt.Sprintf("%s:%d", file, line)
}
func trace() string {
trace2 := fileline(2)
trace3 := fileline(3)
return "\r " + strings.Repeat(" ", len(fmt.Sprintf("%s:", trace2))) + "\r " + trace3
}
func message(msgs ...interface{}) string {
if len(msgs) == 0 {
return ""
}
s := fmt.Sprintln(msgs...)
s = s[:len(s)-1] // remove newline
return ": " + s
}
func printable(s string) string {
s = strings.Replace(s, "\n", `\n`, -1)
s = strings.Replace(s, "\r", `\r`, -1)
s = strings.Replace(s, "\t", `\t`, -1)
s = strings.Replace(s, "\x00", `\0`, -1)
return s
}
const (
Red = "31"
Green = "32"
)
func color(color string, s interface{}) string {
return fmt.Sprintf("\033[00;%sm%v\033[00m", color, s)
}
/////////////////////////////////////////////////////////////////
func Fail(t *testing.T, msgs ...interface{}) {
t.Errorf("%s%s", trace(), message(msgs...))
}
func Error(t *testing.T, err error, msgs ...interface{}) {
if err != nil {
t.Errorf("%s%s: %s", trace(), message(msgs...), color(Red, err.Error()))
}
}
func That(t *testing.T, condition bool, msgs ...interface{}) {
if !condition {
t.Errorf("%s%s: false", trace(), message(msgs...))
}
}
func T(t *testing.T, got, wanted interface{}, msgs ...interface{}) {
gotType := reflect.TypeOf(got)
wantedType := reflect.TypeOf(wanted)
if gotType != wantedType {
t.Errorf("%s%s: type %v != %v", trace(), message(msgs...), color(Red, gotType), color(Green, wantedType))
return
}
if got == wanted {
return
}
if wantedType != nil {
if equals, ok := wantedType.MethodByName("Equals"); ok && equals.Type.NumIn() == 2 && equals.Type.NumOut() == 1 && equals.Type.In(0) == wantedType && equals.Type.In(1) == gotType && equals.Type.Out(0).Kind() == reflect.Bool && equals.Func.Call([]reflect.Value{reflect.ValueOf(wanted), reflect.ValueOf(got)})[0].Bool() {
return
}
}
t.Errorf("%s%s: %v != %v", trace(), message(msgs...), color(Red, got), color(Green, wanted))
}
func Bytes(t *testing.T, got, wanted []byte, msgs ...interface{}) {
if !bytes.Equal(got, wanted) {
gotString := printable(string(got))
wantedString := printable(string(wanted))
t.Errorf("%s%s:\n%s\n%s", trace(), message(msgs...), color(Red, gotString), color(Green, wantedString))
}
}
func String(t *testing.T, got, wanted string, msgs ...interface{}) {
if got != wanted {
gotString := printable(got)
wantedString := printable(wanted)
t.Errorf("%s%s:\n%s\n%s", trace(), message(msgs...), color(Red, gotString), color(Green, wantedString))
}
}
func Float(t *testing.T, got, wanted float64, msgs ...interface{}) {
if math.Abs(got-wanted) > 1e-6 {
t.Errorf("%s%s: %v != %v", trace(), message(msgs...), color(Red, got), color(Green, wanted))
}
}
func Minify(t *testing.T, input string, err error, got, wanted string, msgs ...interface{}) {
inputString := printable(input)
if err != nil {
t.Errorf("%s%s:\n%s\n%s", trace(), message(msgs...), inputString, color(Red, err.Error()))
return
}
if got != wanted {
gotString := printable(got)
wantedString := printable(wanted)
t.Errorf("%s%s:\n%s\n%s\n%s", trace(), message(msgs...), inputString, color(Red, gotString), color(Green, wantedString))
}
}