-
Notifications
You must be signed in to change notification settings - Fork 57
/
es_test.go
59 lines (49 loc) · 1.17 KB
/
es_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
package esquery
import (
"encoding/json"
"reflect"
"testing"
"github.com/jgroeneveld/trial/assert"
)
type mapTest struct {
name string
q Mappable
exp map[string]interface{}
}
func runMapTests(t *testing.T, tests []mapTest) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
m := test.q.Map()
// convert both maps to JSON in order to compare them. we do not
// use reflect.DeepEqual on the maps as this doesn't always work
exp, got, ok := sameJSON(test.exp, m)
if !ok {
t.Errorf("expected %s, got %s", exp, got)
}
})
}
}
func sameJSON(a, b map[string]interface{}) (aJSON, bJSON []byte, ok bool) {
aJSON, aErr := json.Marshal(a)
bJSON, bErr := json.Marshal(b)
if aErr != nil || bErr != nil {
return aJSON, bJSON, false
}
ok = reflect.DeepEqual(aJSON, bJSON)
return aJSON, bJSON, ok
}
type jsonTest struct {
name string
q json.Marshaler
expJSON string
expErr error
}
func runJSONTests(t *testing.T, tests []jsonTest) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
b, err := test.q.MarshalJSON()
assert.Equal(t, test.expErr, err)
assert.Equal(t, test.expJSON, string(b))
})
}
}