-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathassertions_test.go
74 lines (58 loc) · 1.58 KB
/
assertions_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
package goconfig
import (
"reflect"
"runtime/debug"
"testing"
)
// AssertNil checks whether the obtained field is equal to nil,
// failing in other case.
func AssertNil(t *testing.T, obtained interface{}) {
if nil == obtained {
return
}
if reflect.ValueOf(obtained).IsNil() {
return
}
line := GetStackLine(2)
t.Errorf("\nExpected: nil \nObtained:%#v\nat %s\n", obtained, line)
}
// AssertNotNil checks whether the obtained field is distinct to nil,
// failing in other case.
func AssertNotNil(t *testing.T, obtained interface{}) {
line := GetStackLine(2)
if nil == obtained {
t.Errorf("\nExpected: not nil \nObtained:%#v\nat %s\n", obtained, line)
return
}
if reflect.ValueOf(obtained).IsNil() {
t.Errorf("\nExpected: not nil \nObtained:%#v\nat %s\n", obtained, line)
return
}
}
// AssertEqual checks whether the obtained and expected fields are equal
// failing in other case.
func AssertEqual(t *testing.T, obtained, expected interface{}) bool {
if reflect.DeepEqual(expected, obtained) {
return true
}
line := GetStackLine(2)
t.Errorf("\nExpected: %#v\nObtained: %#v\nat %s\n", expected, obtained, line)
return false
}
// GetStackLine accesses the stack trace to get some lines
// so they can be showed by the tests in case of error.
func GetStackLine(linesToSkip int) string {
stack := debug.Stack()
lines := make([]string, 0)
index := 0
for i := 0; i < len(stack); i++ {
if stack[i] == []byte("\n")[0] {
lines = append(lines, string(stack[index:i-1]))
index = i + 1
}
}
if linesToSkip >= len(lines) {
return ""
}
return lines[linesToSkip]
}