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
107 lines (91 loc) · 2.93 KB
/
test.go
File metadata and controls
107 lines (91 loc) · 2.93 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
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
// Package test contains some simple utils for more readable tests.
// It is based on https://github.com/benbjohnson/testing.
package test
import (
"errors"
"fmt"
"path/filepath"
"runtime"
"testing"
"github.com/google/go-cmp/cmp"
)
// Assert fails the test if the condition is false. It returns true, if
// the test did not fail.
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) bool {
if condition {
return true
}
_, file, line, _ := runtime.Caller(1)
// msg has to be this way, else params "v" for it do not work.
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.Fail()
return false
}
// Ok fails the test if an err is not nil. It returns true, if the test
// did not fail.
func Ok(tb testing.TB, err error) bool {
if err == nil {
return true
}
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.Error(err)
return false
}
// Equals fails the test if exp is not equal to act. It returns true, if
// the test did not fail.
func Equals(tb testing.TB, exp, act interface{}, options ...cmp.Option) bool {
if cmp.Equal(exp, act, options...) {
return true
}
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.Fail()
return false
}
// NotEquals fails the test if exp is equal to act. It returns true, if
// the test did not fail.
func NotEquals(tb testing.TB, exp, act interface{}, options ...cmp.Option) bool {
if !cmp.Equal(exp, act, options...) {
return true
}
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\tnot exp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.Fail()
return false
}
type ExpectedErrorResult int
const (
// IsCorrectNil is returned if both the expected and the actual
// error are nil.
IsCorrectNil ExpectedErrorResult = iota
// IsCorrectErr is returned if both the expected and the actual
// error are not nil and identical.
IsCorrectErr
// IsWrongErr is returned if the expected and the actual error
// don't match.
IsWrongErr
)
// ExpectError checks if the actual error is expected given error, using
// errors.Is. In all cases the test Fails if expected does not match actual.
//
// The three states are useful if you want to check for errors and do different
// things based on the result. For example if the actual error is only in some
// cases not nil and you want to continue execution in that case, but stop on
// the other cases.
func ExpectedError(tb testing.TB, exp, act error) ExpectedErrorResult {
if exp == nil {
if Ok(tb, act) {
return IsCorrectNil
}
fmt.Printf("expected NO error, got \n%v", act)
tb.Fail()
return IsWrongErr
}
if errors.Is(act, exp) {
return IsCorrectErr
}
fmt.Printf("expected error \n%v, got \n%v", exp, act)
tb.Fail()
return IsWrongErr
}