-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathassert.go
62 lines (48 loc) · 1.76 KB
/
assert.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
package barrier
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/buger/goterm"
"github.com/smartystreets/goconvey/convey"
)
type assertionError struct {
msg string
description string
Expected interface{}
Actual interface{}
}
func newassertionError(msg string) assertionError {
return assertionError{
msg: msg,
}
}
func (e assertionError) Error() string {
if e.Expected != nil && e.Actual != nil {
return goterm.Color(fmt.Sprintf("[FAIL] %s: expected: '%s', actual '%s'", e.msg, e.Expected, e.Actual), goterm.RED)
}
return goterm.Color(fmt.Sprintf("[FAIL] %s: %s", e.msg, e.description), goterm.RED)
}
// Assert can use goconvey function to perform an assertion.
func Assert(t TestInfo, message string, actual interface{}, f func(interface{}, ...interface{}) string, expected ...interface{}) {
if msg := f(actual, expected...); msg != "" {
r := newassertionError(message)
if err := json.Unmarshal([]byte(msg), &r); err != nil {
r.description = strings.Replace(strings.Replace(msg, "\n", ", ", -1), "\t", " ", -1)
}
panic(r)
}
fmt.Fprint(t, goterm.Color(fmt.Sprintf("- [PASS] %s", message), goterm.GREEN)) // nolint
fmt.Fprintln(t) // nolint
}
// Step runs a particular step.
func Step(t TestInfo, name string, step func() error) {
start := time.Now()
fmt.Fprintf(t, "%s\n", name) // nolint
if err := step(); err != nil {
fmt.Fprintf(t, "%s\n", goterm.Color(fmt.Sprintf("took: %s", time.Since(start).Round(time.Millisecond)), goterm.BLUE)) // nolint
Assert(t, "step should not return any error", err, convey.ShouldBeNil)
}
fmt.Fprintf(t, "%s\n\n", goterm.Color(fmt.Sprintf("took: %s", time.Since(start).Round(time.Millisecond)), goterm.BLUE)) // nolint
}