-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
95 lines (83 loc) · 2.95 KB
/
map.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
85
86
87
88
89
90
91
92
93
94
95
// Copyright (C) 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package assert
import (
"reflect"
"github.com/google/gapid/core/data/compare"
)
// OnMap is the result of calling ThatMap on an Assertion.
// It provides assertion tests that are specific to map types.
type OnMap struct {
Assertion
mp interface{}
}
// ThatMap returns an OnMap for assertions on map type objects.
// Calling this with a non map type will result in panics.
func (a Assertion) ThatMap(mp interface{}) OnMap {
return OnMap{Assertion: a, mp: mp}
}
// IsEmpty asserts that the map was of length 0
func (o OnMap) IsEmpty() bool {
value := reflect.ValueOf(o.mp)
return o.CompareRaw(value.Len(), "is", "empty").Test(value.Len() == 0)
}
// IsNotEmpty asserts that the map has elements
func (o OnMap) IsNotEmpty() bool {
value := reflect.ValueOf(o.mp)
return o.Compare(value.Len(), "length >", 0).Test(value.Len() > 0)
}
// IsLength asserts that the map has exactly the specified number of elements
func (o OnMap) IsLength(length int) bool {
value := reflect.ValueOf(o.mp)
return o.Compare(value.Len(), "length ==", length).Test(value.Len() == length)
}
// Equals asserts the array or map matches expected.
func (o OnMap) Equals(expected interface{}) bool {
return o.mapsEqual(expected, func(a, b interface{}) bool { return a == b })
}
// EqualsWithComparator asserts the array or map matches expected using a comparator function
func (o OnMap) EqualsWithComparator(expected interface{}, same func(a, b interface{}) bool) bool {
return o.mapsEqual(expected, same)
}
// DeepEquals asserts the array or map matches expected using a deep-equal comparison.
func (o OnMap) DeepEquals(expected interface{}) bool {
return o.mapsEqual(expected, compare.DeepEqual)
}
func (o OnMap) mapsEqual(expected interface{}, same func(a, b interface{}) bool) bool {
return o.Test(func() bool {
gs := reflect.ValueOf(o.mp)
es := reflect.ValueOf(expected)
equal := true
for _, k := range gs.MapKeys() {
gv := gs.MapIndex(k)
ev := es.MapIndex(k)
if !ev.IsValid() {
o.Printf("\tExtra key: %#v\n", k.Interface())
equal = false
continue
}
if !same(gv.Interface(), ev.Interface()) {
o.Printf("\tKey: %#v, %#v differs from expected: %#v\n", k.Interface(), gv.Interface(), ev.Interface())
equal = false
}
}
for _, k := range es.MapKeys() {
if !gs.MapIndex(k).IsValid() {
o.Printf("\tKey missing: %#v\n", k.Interface())
equal = false
}
}
return equal
}())
}