forked from verless/verless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.go
More file actions
46 lines (40 loc) · 1.29 KB
/
test.go
File metadata and controls
46 lines (40 loc) · 1.29 KB
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
// Package test contains some simple utils for more readable tests.
// It is based on https://github.com/benbjohnson/testing.
package test
import (
"fmt"
"path/filepath"
"runtime"
"testing"
"github.com/google/go-cmp/cmp"
)
// Formatting strings for printing test results.
const (
assertFormat string = "\\033[31m%s:%d: %v\\033[39m\\n\\n"
okFormat string = "\u001B[31m%s:%d: unexpected error: %s\u001B[39m\n\n"
equalsFormat string = "\u001B[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\u001B[39m\n\n"
)
// Assert fails the test if the condition is false.
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
if !condition {
_, file, line, _ := runtime.Caller(1)
fmt.Printf(assertFormat, append([]interface{}{filepath.Base(file), line, msg}, v...)...)
tb.FailNow()
}
}
// Ok fails the test if an err is not nil.
func Ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf(okFormat, filepath.Base(file), line, err.Error())
tb.Error(err)
}
}
// Equals fails the test if exp is not equal to act.
func Equals(tb testing.TB, exp, act interface{}, options ...cmp.Option) {
if !cmp.Equal(exp, act, options...) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf(equalsFormat, filepath.Base(file), line, exp, act)
tb.FailNow()
}
}