forked from tdewolff/test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
114 lines (96 loc) · 3.03 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
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\t" + strings.Repeat(" ", len(fmt.Sprintf("%s:", trace2))) + "\r\t" + trace3
}
func message(msgs ...interface{}) string {
if len(msgs) == 0 {
return "\n"
}
return ": " + fmt.Sprintln(msgs...)
}
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)
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 That(t *testing.T, condition bool, msgs ...interface{}) {
if !condition {
t.Errorf("%s%s", trace(), message(msgs...))
}
}
func V(t *testing.T, context string, got, wanted interface{}) {
gotType := reflect.TypeOf(got)
wantedType := reflect.TypeOf(wanted)
if gotType != wantedType {
t.Errorf("%s: %s: type %v != %v", trace(), context, color(Red, gotType), color(Green, wantedType))
}
if got != wanted {
t.Errorf("%s: %s: %v != %v", trace(), context, color(Red, got), color(Green, wanted))
}
}
func Error(t *testing.T, err, expected error, msgs ...interface{}) {
if err != expected {
t.Errorf("%s%s error: %v\nexpected: %v\n", trace(), message(msgs...), err, expected)
}
}
func Int(t *testing.T, output, expected int, msgs ...interface{}) {
if output != expected {
t.Errorf("%s%s output: %d\nexpected: %d\n", trace(), message(msgs...), output, expected)
}
}
func Float(t *testing.T, output, expected float64, msgs ...interface{}) {
if math.Abs(output-expected) > 1e-10 {
t.Errorf("%s%s output: %f\nexpected: %f\n", trace(), message(msgs...), output, expected)
}
}
func String(t *testing.T, output, expected string, msgs ...interface{}) {
if output != expected {
t.Errorf("%s%s output: %s\nexpected: %s\n", trace(), message(msgs...), printable(output), printable(expected))
}
}
func Bytes(t *testing.T, output, expected []byte, msgs ...interface{}) {
if !bytes.Equal(output, expected) {
t.Errorf("%s%s output: %s\nexpected: %s\n", trace(), message(msgs...), printable(string(output)), printable(string(expected)))
}
}
func Minify(t *testing.T, input string, err error, output, expected string, msgs ...interface{}) {
if err != nil {
t.Errorf("%s%s given: %s\n error: %v\n", trace(), message(msgs...), printable(input), err)
}
if output != expected {
t.Errorf("%s%s given: %s\n output: %s\nexpected: %s\n", trace(), message(msgs...), printable(input), printable(output), printable(expected))
}
}