Skip to content

Commit

Permalink
Added: found-elements to json output (#852)
Browse files Browse the repository at this point in the history
closes #845
  • Loading branch information
aelsabbahy authored Nov 4, 2023
1 parent 75133e6 commit 10b895d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
7 changes: 7 additions & 0 deletions matchers/consist_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/onsi/gomega/matchers"
"github.com/samber/lo"
)

type ConsistOfMatcher struct {
Expand All @@ -21,12 +22,18 @@ func ConsistOf(elements ...interface{}) GossMatcher {
func (m *ConsistOfMatcher) FailureResult(actual interface{}) MatcherResult {
missingElements := getUnexported(m, "missingElements")
extraElements := getUnexported(m, "extraElements")
missingEl, ok := missingElements.([]interface{})
var foundElements any
if ok {
foundElements, _ = lo.Difference(m.Elements, missingEl)
}
return MatcherResult{
Actual: actual,
Message: "to consist of",
Expected: m.Elements,
MissingElements: missingElements,
ExtraElements: extraElements,
FoundElements: foundElements,
}
}

Expand Down
7 changes: 7 additions & 0 deletions matchers/contain_elements_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/onsi/gomega/matchers"
"github.com/samber/lo"
)

type ContainElementsMatcher struct {
Expand All @@ -19,11 +20,17 @@ func ContainElements(elements ...interface{}) GossMatcher {
}
func (m *ContainElementsMatcher) FailureResult(actual interface{}) MatcherResult {
missingElements := getUnexported(m, "missingElements")
missingEl, ok := missingElements.([]interface{})
var foundElements any
if ok {
foundElements, _ = lo.Difference(m.Elements, missingEl)
}
return MatcherResult{
Actual: actual,
Message: "to contain elements matching",
Expected: m.Elements,
MissingElements: missingElements,
FoundElements: foundElements,
}

}
Expand Down
7 changes: 5 additions & 2 deletions matchers/have_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type HavePatternsMatcher struct {

Elements interface{}
missingElements []string
foundElements []string
}

func HavePatterns(elements interface{}) GossMatcher {
Expand Down Expand Up @@ -107,9 +108,10 @@ func (m *HavePatternsMatcher) Match(actual interface{}) (success bool, err error
}
}

foundSlice := patternsToSlice(found)
m.foundElements = foundSlice
if len(elements) != len(found) {
found := patternsToSlice(found)
m.missingElements = subtractSlice(elements, found)
m.missingElements = subtractSlice(elements, foundSlice)
return false, nil
}
return true, nil
Expand All @@ -128,6 +130,7 @@ func (m *HavePatternsMatcher) FailureResult(actual interface{}) MatcherResult {
Message: "to have patterns",
Expected: m.Elements,
MissingElements: m.missingElements,
FoundElements: m.foundElements,
}
}

Expand Down
15 changes: 8 additions & 7 deletions matchers/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ type GossMatcher interface {
}

type MatcherResult struct {
Actual interface{}
Message string
Expected interface{}
MissingElements interface{}
ExtraElements interface{}
TransformerChain []Transformer
UntransformedValue interface{}
Actual interface{} `json:"actual"`
Message string `json:"message"`
Expected interface{} `json:"expected"`
MissingElements interface{} `json:"missing-elements"`
FoundElements interface{} `json:"found-elements"`
ExtraElements interface{} `json:"extra-elements"`
TransformerChain []Transformer `json:"transform-chain"`
UntransformedValue interface{} `json:"untransformed-value"`
}

func getUnexported(i interface{}, field string) interface{} {
Expand Down

0 comments on commit 10b895d

Please sign in to comment.