-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
149 lines (131 loc) · 2.71 KB
/
Copy pathmap.go
File metadata and controls
149 lines (131 loc) · 2.71 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package its
import (
"fmt"
"sort"
"github.com/youta-t/its/itskit"
"github.com/youta-t/its/itskit/itsio"
)
type keyedMatch struct {
strkey string
match itskit.Match
}
func (a keyedMatch) less(b keyedMatch) bool {
return a.strkey < b.strkey
}
type mapSpec[K comparable, V any] map[K]Matcher[V]
type mapMatcher[K comparable, V any] struct {
header itskit.Label
spec mapSpec[K, V]
}
type MapSpec[K comparable, V any] map[K]Matcher[V]
func Map[K comparable, V any](spec map[K]Matcher[V]) Matcher[map[K]V] {
cancel := itskit.SkipStack()
defer cancel()
return mapMatcher[K, V]{
header: itskit.NewLabelWithLocation(
"map[%T]%T{... ( keys: %d, %d; +%d, -%d )",
*new(K), *new(V), itskit.Got, itskit.Want(len(spec)),
itskit.Placeholder, itskit.Placeholder,
),
spec: spec,
}
}
func (mm mapMatcher[K, V]) Match(actual map[K]V) itskit.Match {
allkeys := map[K]struct{}{}
for k := range actual {
allkeys[k] = struct{}{}
}
for k := range mm.spec {
allkeys[k] = struct{}{}
}
matches := []keyedMatch{}
extra := 0
miss := 0
ng := 0
for k := range allkeys {
strkey := fmt.Sprintf("%+v", k)
x, xok := mm.spec[k]
if !xok {
extra += 1
matches = append(
matches,
keyedMatch{
strkey: strkey,
match: itskit.NG(
fmt.Sprintf("%+v: (not in want)", k),
itskit.NG(
itskit.NewLabel(
"%v, %v",
itskit.Got, itskit.Want("??"),
).Fill(actual[k]),
),
),
},
)
continue
}
a, aok := actual[k]
if !aok {
miss += 1
matches = append(
matches,
keyedMatch{
strkey: strkey,
match: itskit.NG(
fmt.Sprintf("%+v: (not in got)", k),
itskit.NG(mm.spec[k].String()),
),
},
)
continue
}
match := x.Match(a)
matches = append(
matches,
keyedMatch{
strkey: strkey,
match: itskit.NewMatch(
match.Ok(),
fmt.Sprintf("%+v:", k),
match,
),
},
)
if !match.Ok() {
ng += 1
}
}
submatches := make([]itskit.Match, 0, len(matches))
sort.Slice(
matches,
func(a, b int) bool { return matches[a].less(matches[b]) },
)
for _, m := range matches {
submatches = append(submatches, m.match)
}
return itskit.NewMatch(
ng+extra+miss == 0,
mm.header.Fill(len(actual), ng+extra, ng+miss),
submatches...,
)
}
func (mm mapMatcher[K, V]) Write(w itsio.Writer) error {
if err := w.WriteStringln(mm.header.String()); err != nil {
return err
}
iw := w.Indent()
for k, m := range mm.spec {
ks := fmt.Sprintf("%+v:", k)
if err := iw.WriteStringln(ks); err != nil {
return err
}
iiw := iw.Indent()
if err := m.Write(iiw); err != nil {
return err
}
}
return nil
}
func (mm mapMatcher[K, V]) String() string {
return itskit.MatcherToString[map[K]V](mm)
}