...
1
2
3
4
5
6
7
8
9
10
11
12
13 package mockdb
14
15 import (
16 "encoding/json"
17 "reflect"
18
19 kivik "github.com/go-kivik/kivik/v4"
20 )
21
22 func meets(a, e expectation) bool {
23 if reflect.TypeOf(a).Elem().Name() != reflect.TypeOf(e).Elem().Name() {
24 return false
25 }
26
27 if _, ok := e.(*ExpectedDB); !ok {
28 if !dbMeetsExpectation(a.dbo(), e.dbo()) {
29 return false
30 }
31 }
32 if !optionsMeetExpectation(a.opts(), e.opts()) {
33 return false
34 }
35 return a.met(e)
36 }
37
38 func dbMeetsExpectation(a, e *DB) bool {
39 if e == nil {
40 return true
41 }
42 e.mu.RLock()
43 defer e.mu.RUnlock()
44 a.mu.RLock()
45 defer a.mu.RUnlock()
46 return e.name == a.name && e.id == a.id
47 }
48
49 func optionsMeetExpectation(a, e kivik.Option) bool {
50 if e == nil {
51 return true
52 }
53
54 return reflect.DeepEqual(convertOptions(e), convertOptions(a))
55 }
56
57
58 func convertOptions(a kivik.Option) []kivik.Option {
59 if a == nil {
60 return nil
61 }
62 t := reflect.TypeOf(a)
63 if t.Kind() != reflect.Slice {
64 return []kivik.Option{a}
65 }
66 v := reflect.ValueOf(a)
67 result := make([]kivik.Option, 0, v.Len())
68 for i := 0; i < v.Len(); i++ {
69 opt := v.Index(i)
70 if !opt.IsNil() {
71 result = append(result, convertOptions(opt.Interface().(kivik.Option))...)
72 }
73 }
74 return result
75 }
76
77 func jsonMeets(e, a interface{}) bool {
78 eJSON, _ := json.Marshal(e)
79 aJSON, _ := json.Marshal(a)
80 var eI, aI interface{}
81 _ = json.Unmarshal(eJSON, &eI)
82 _ = json.Unmarshal(aJSON, &aI)
83 return reflect.DeepEqual(eI, aI)
84 }
85
View as plain text