/
testutils.go
44 lines (39 loc) · 898 Bytes
/
testutils.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
package jsonpath
import (
"testing"
"github.com/stretchr/testify/assert"
)
func assertMatchingStringArray(t *testing.T, expected []string, result []interface{}) {
actual := make([]string, len(result), len(result))
for i, item := range result {
actual[i] = item.(string)
}
if !assert.True(t, sameStringSlice(expected, actual)) {
assert.EqualValues(t, expected, result)
}
}
func sameStringSlice(x, y []string) bool {
if len(x) != len(y) {
return false
}
// create a map of string -> int
diff := make(map[string]int, len(x))
for _, _x := range x {
// 0 value for int is 0, so just increment a counter for the string
diff[_x]++
}
for _, _y := range y {
// If the string _y is not in diff bail out early
if _, ok := diff[_y]; !ok {
return false
}
diff[_y] -= 1
if diff[_y] == 0 {
delete(diff, _y)
}
}
if len(diff) == 0 {
return true
}
return false
}