-
Notifications
You must be signed in to change notification settings - Fork 0
/
stomp.go
81 lines (70 loc) · 1.83 KB
/
stomp.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
// Package stomp provides assertion helpers for testing.
package stomp
import (
"fmt"
"reflect"
)
// overview of how comparison works in go:
// https://medium.com/golangspec/comparison-operators-in-go-910d9d788ec0
// testChain contains the chainable assertion funcs
// returned by the expect func created by `MakeExpect`
type testChain struct {
ToEqual func(interface{})
ToDeepEqual func(interface{})
ToNotEqual func(interface{})
}
// Tester interface for easier testing
type Tester interface {
Errorf(string, ...interface{})
}
// MakeExpect binds *testing.T to the returned expect function
// expect is a `testChain` instance and implements those methods:
// - ToEqual
// - ToDeepEqual
// - ToNotEqual
func MakeExpect(t Tester) func(interface{}) *testChain {
return func(expected interface{}) *testChain {
return &testChain{
ToEqual: func(actual interface{}) {
if expected != actual {
t.Errorf("[!] error: expected %v, actual: %v\n", expected, actual)
}
},
ToDeepEqual: func(actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
t.Errorf("[!] error: expected %v, actual: %v\n", expected, actual)
}
},
ToNotEqual: func(actual interface{}) {
if expected == actual {
t.Errorf("[!] error: expected %v, actual: %v NOT to equal\n", expected, actual)
}
},
}
}
}
// Describe allows you to add output to `go test -v`
func Describe(msg string, f func()) {
fmt.Printf(" * %s\n", msg)
f()
}
// SameStringSlice compares two string slices regardless of order
func SameStringSlice(x, y []string) bool {
if len(x) != len(y) {
return false
}
diff := make(map[string]int, len(x))
for _, _x := range x {
diff[_x]++
}
for _, _y := range y {
if _, ok := diff[_y]; !ok {
return false
}
diff[_y]--
if diff[_y] == 0 {
delete(diff, _y)
}
}
return len(diff) == 0
}