-
Notifications
You must be signed in to change notification settings - Fork 1
/
meets.go
51 lines (45 loc) · 992 Bytes
/
meets.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
package kivikmock
import (
"encoding/json"
"reflect"
kivik "github.com/go-kivik/kivik/v4"
)
func meets(a, e expectation) bool {
if reflect.TypeOf(a).Elem().Name() != reflect.TypeOf(e).Elem().Name() {
return false
}
// Skip the DB test for the dbo() method
if _, ok := e.(*ExpectedDB); !ok {
if !dbMeetsExpectation(a.dbo(), e.dbo()) {
return false
}
}
if !optionsMeetExpectation(a.opts(), e.opts()) {
return false
}
return a.met(e)
}
func dbMeetsExpectation(a, e *DB) bool {
if e == nil {
return true
}
e.mu.RLock()
defer e.mu.RUnlock()
a.mu.RLock()
defer a.mu.RUnlock()
return e.name == a.name && e.id == a.id
}
func optionsMeetExpectation(a, e kivik.Options) bool {
if e == nil {
return true
}
return reflect.DeepEqual(e, a)
}
func jsonMeets(e, a interface{}) bool {
eJSON, _ := json.Marshal(e)
aJSON, _ := json.Marshal(a)
var eI, aI interface{}
_ = json.Unmarshal(eJSON, &eI)
_ = json.Unmarshal(aJSON, &aI)
return reflect.DeepEqual(eI, aI)
}