forked from pepinns/go-hamcrest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyvalue_result.go
76 lines (69 loc) · 1.78 KB
/
keyvalue_result.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
package hamcrest
type KeyResult struct {
KeyDescription string
KeyResult MatchResult
ValueDescription string
ValueResult MatchResult
}
func (me *KeyResult) Matched() bool {
if me.ValueResult == nil {
return false
}
return me.KeyResult.Matched() && me.ValueResult.Matched()
}
func (me *KeyResult) WriteFailureReason(writer DescriptionWriter) {
if me.Matched() {
writer.WriteStringf("matched [%s:%s] because ", me.KeyDescription, me.ValueDescription)
me.KeyResult.WriteFailureReason(writer)
writer.WriteString(" and ")
me.ValueResult.WriteFailureReason(writer)
} else {
writer.WriteStringf("failed [%s:%s] because ", me.KeyDescription, me.ValueDescription)
if !me.KeyResult.Matched() {
me.KeyResult.WriteFailureReason(writer)
if me.ValueResult != nil {
writer.WriteString(" and ")
}
}
if me.ValueResult != nil {
me.ValueResult.WriteFailureReason(writer)
}
}
}
type KeyValueResult struct {
Failures []*KeyResult
Sucesses []*KeyResult
}
func (me *KeyValueResult) Matched() bool {
return len(me.Sucesses) > 0
}
func (me *KeyValueResult) WriteFailureReason(output DescriptionWriter) {
if me.Matched() {
output.WriteString("matched items [")
reset := output.IncreaseIndent(1)
for _, res := range me.Sucesses {
output.NewLine()
res.WriteFailureReason(output)
}
reset()
output.NewLine()
output.WriteString("]")
} else {
output.WriteString("failed to match [")
reset := output.IncreaseIndent(1)
for _, res := range me.Failures {
output.NewLine()
res.WriteFailureReason(output)
}
reset()
output.NewLine()
output.WriteString("]")
}
}
func (me *KeyValueResult) Add(keyResult *KeyResult) {
if keyResult.Matched() {
me.Sucesses = append(me.Sucesses, keyResult)
} else {
me.Failures = append(me.Failures, keyResult)
}
}