-
Notifications
You must be signed in to change notification settings - Fork 6
/
assertion.go
111 lines (86 loc) · 2.29 KB
/
assertion.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
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
108
109
110
111
package testutil
import (
"fmt"
"reflect"
)
// TODO(tmrts): Add meaningful diff messages to failed assertions (goconvey doesn't have them).
// Checks whether two slices contain the same elements.
func ShouldSetEqual(actual interface{}, expected ...interface{}) (msg string) {
actualSlice, expectedSlice := actual.([]string), expected[0].([]string)
msg = fmt.Sprintf("Expected:\t%q\nActual:\t%q\n(Should have the same elements)", expectedSlice, actualSlice)
if len(actualSlice) != len(expectedSlice) {
return
}
actualSet := make(map[string]bool)
for _, a := range actualSlice {
actualSet[a] = true
}
for _, e := range expectedSlice {
if actualSet[e] != true {
return
}
}
expectedSet := make(map[string]bool)
for _, e := range expectedSlice {
expectedSet[e] = true
}
for _, a := range actualSlice {
if expectedSet[a] != true {
return
}
}
return ""
}
func ShouldConsistOf(actual interface{}, expected ...interface{}) (msg string) {
msg = fmt.Sprintf("Expected:\n%q\nActual:\n%q\n(Should consist of the given elements)", expected, actual)
switch actual.(type) {
case []int:
if len(actual.([]int)) != len(expected) {
return
}
for i, v := range actual.([]int) {
if !reflect.DeepEqual(v, expected[i]) {
return
}
}
case []string:
if len(actual.([]string)) != len(expected) {
return
}
for i, v := range actual.([]string) {
if !reflect.DeepEqual(v, expected[i]) {
return
}
}
default:
if fmt.Sprint(actual) != fmt.Sprint(expected) {
return
}
}
return ""
}
func ShouldStructEqual(actual interface{}, expected ...interface{}) (msg string) {
msg = fmt.Sprintf("Expected:\n%v\nActual:\n%v\n(Should be equal)", expected[0], actual)
a, e := fmt.Sprintf("%v", actual), fmt.Sprintf("%v", expected[0])
if a != e {
return
}
return ""
}
func ShouldBeSuperSetOf(super interface{}, sub ...interface{}) (msg string) {
superSlice, subSlice := super.([]string), sub[0].([]string)
msg = fmt.Sprintf("SubSet:\t%q\nSuperSet:\t%q\n(Should contain every element of the given slice)", subSlice, superSlice)
if len(superSlice) < len(subSlice) {
return
}
superSet := make(map[string]bool)
for _, e := range superSlice {
superSet[e] = true
}
for _, a := range subSlice {
if _, ok := superSet[a]; ok != true {
return
}
}
return ""
}