Skip to content

Commit 10b895d

Browse files
authored
Added: found-elements to json output (#852)
closes #845
1 parent 75133e6 commit 10b895d

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

matchers/consist_of.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55

66
"github.com/onsi/gomega/matchers"
7+
"github.com/samber/lo"
78
)
89

910
type ConsistOfMatcher struct {
@@ -21,12 +22,18 @@ func ConsistOf(elements ...interface{}) GossMatcher {
2122
func (m *ConsistOfMatcher) FailureResult(actual interface{}) MatcherResult {
2223
missingElements := getUnexported(m, "missingElements")
2324
extraElements := getUnexported(m, "extraElements")
25+
missingEl, ok := missingElements.([]interface{})
26+
var foundElements any
27+
if ok {
28+
foundElements, _ = lo.Difference(m.Elements, missingEl)
29+
}
2430
return MatcherResult{
2531
Actual: actual,
2632
Message: "to consist of",
2733
Expected: m.Elements,
2834
MissingElements: missingElements,
2935
ExtraElements: extraElements,
36+
FoundElements: foundElements,
3037
}
3138
}
3239

matchers/contain_elements_matcher.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55

66
"github.com/onsi/gomega/matchers"
7+
"github.com/samber/lo"
78
)
89

910
type ContainElementsMatcher struct {
@@ -19,11 +20,17 @@ func ContainElements(elements ...interface{}) GossMatcher {
1920
}
2021
func (m *ContainElementsMatcher) FailureResult(actual interface{}) MatcherResult {
2122
missingElements := getUnexported(m, "missingElements")
23+
missingEl, ok := missingElements.([]interface{})
24+
var foundElements any
25+
if ok {
26+
foundElements, _ = lo.Difference(m.Elements, missingEl)
27+
}
2228
return MatcherResult{
2329
Actual: actual,
2430
Message: "to contain elements matching",
2531
Expected: m.Elements,
2632
MissingElements: missingElements,
33+
FoundElements: foundElements,
2734
}
2835

2936
}

matchers/have_patterns.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type HavePatternsMatcher struct {
2020

2121
Elements interface{}
2222
missingElements []string
23+
foundElements []string
2324
}
2425

2526
func HavePatterns(elements interface{}) GossMatcher {
@@ -107,9 +108,10 @@ func (m *HavePatternsMatcher) Match(actual interface{}) (success bool, err error
107108
}
108109
}
109110

111+
foundSlice := patternsToSlice(found)
112+
m.foundElements = foundSlice
110113
if len(elements) != len(found) {
111-
found := patternsToSlice(found)
112-
m.missingElements = subtractSlice(elements, found)
114+
m.missingElements = subtractSlice(elements, foundSlice)
113115
return false, nil
114116
}
115117
return true, nil
@@ -128,6 +130,7 @@ func (m *HavePatternsMatcher) FailureResult(actual interface{}) MatcherResult {
128130
Message: "to have patterns",
129131
Expected: m.Elements,
130132
MissingElements: m.missingElements,
133+
FoundElements: m.foundElements,
131134
}
132135
}
133136

matchers/matchers.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ type GossMatcher interface {
1919
}
2020

2121
type MatcherResult struct {
22-
Actual interface{}
23-
Message string
24-
Expected interface{}
25-
MissingElements interface{}
26-
ExtraElements interface{}
27-
TransformerChain []Transformer
28-
UntransformedValue interface{}
22+
Actual interface{} `json:"actual"`
23+
Message string `json:"message"`
24+
Expected interface{} `json:"expected"`
25+
MissingElements interface{} `json:"missing-elements"`
26+
FoundElements interface{} `json:"found-elements"`
27+
ExtraElements interface{} `json:"extra-elements"`
28+
TransformerChain []Transformer `json:"transform-chain"`
29+
UntransformedValue interface{} `json:"untransformed-value"`
2930
}
3031

3132
func getUnexported(i interface{}, field string) interface{} {

0 commit comments

Comments
 (0)