-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_slices.go
84 lines (67 loc) · 1.81 KB
/
match_slices.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
package match
import (
"reflect"
"testing"
)
// Exactly compares two slices and returns true if they are _exactly_ the same.
// This means that the slices must have the same length and the same elements in the same order.
func Exactly(t *testing.T, expected, actual interface{}) {
if !matchExactly(expected, actual) {
t.Errorf("expected %v, got %v", expected, actual)
}
}
// Elements compares two slices and returns true if they contain the same elements.
// This means that the slices must have the same length and the same elements, but not necessarily in the same order.
func Elements(t *testing.T, expected, actual interface{}) {
if !matchElements(expected, actual) {
t.Errorf("expected %v, got %v", expected, actual)
}
}
func matchExactly(expected, actual interface{}) bool {
if !validType(expected, actual) {
return false
}
s1 := reflect.ValueOf(expected)
s2 := reflect.ValueOf(actual)
if s1.Len() != s2.Len() {
return false
}
for i := 0; i < s1.Len(); i++ {
if s1.Index(i).Interface() != s2.Index(i).Interface() {
return false
}
}
return true
}
func matchElements(expected, actual interface{}) bool {
if !validType(expected, actual) {
return false
}
s1 := reflect.ValueOf(expected)
s2 := reflect.ValueOf(actual)
if s1.Len() != s2.Len() {
return false
}
counter := make(map[interface{}]int)
for i := 0; i < s1.Len(); i++ {
element := s1.Index(i).Interface()
counter[element]++
}
for i := 0; i < s2.Len(); i++ {
element := s2.Index(i).Interface()
if counter[element] == 0 {
return false
}
counter[element]--
}
return true
}
func validType(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return false
}
if reflect.TypeOf(expected).Kind() != reflect.Slice || reflect.TypeOf(actual).Kind() != reflect.Slice {
return false
}
return true
}