-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathassertions.go
62 lines (54 loc) · 1.38 KB
/
assertions.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
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"bytes"
"fmt"
"log"
"path/filepath"
"reflect"
"runtime"
"testing"
"text/tabwriter"
)
// AssertEqual checks if values are equal
func AssertEqual(t testing.TB, expected interface{}, actual interface{}, description ...string) {
if reflect.DeepEqual(expected, actual) {
return
}
var aType = "<nil>"
var bType = "<nil>"
if reflect.ValueOf(expected).IsValid() {
aType = reflect.TypeOf(expected).Name()
}
if reflect.ValueOf(actual).IsValid() {
bType = reflect.TypeOf(actual).Name()
}
testName := "AssertEqual"
if t != nil {
testName = t.Name()
}
_, file, line, _ := runtime.Caller(1)
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 5, ' ', 0)
fmt.Fprintf(w, "\nTest:\t%s", testName)
fmt.Fprintf(w, "\nTrace:\t%s:%d", filepath.Base(file), line)
fmt.Fprintf(w, "\nError:\tNot equal")
fmt.Fprintf(w, "\nExpect:\t%v\t[%s]", expected, aType)
fmt.Fprintf(w, "\nResult:\t%v\t[%s]", actual, bType)
if len(description) > 0 {
fmt.Fprintf(w, "\nDescription:\t%s", description[0])
}
result := ""
if err := w.Flush(); err != nil {
result = err.Error()
} else {
result = buf.String()
}
if t != nil {
t.Fatal(result)
} else {
log.Fatal(result)
}
}