From 10b895ddd55e27498b399ee7c0d2dd16e5d2d5af Mon Sep 17 00:00:00 2001 From: Ahmed Elsabbahy Date: Fri, 3 Nov 2023 21:13:24 -0400 Subject: [PATCH] Added: found-elements to json output (#852) closes #845 --- matchers/consist_of.go | 7 +++++++ matchers/contain_elements_matcher.go | 7 +++++++ matchers/have_patterns.go | 7 +++++-- matchers/matchers.go | 15 ++++++++------- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/matchers/consist_of.go b/matchers/consist_of.go index 94a114011..7ad5ead3d 100644 --- a/matchers/consist_of.go +++ b/matchers/consist_of.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/onsi/gomega/matchers" + "github.com/samber/lo" ) type ConsistOfMatcher struct { @@ -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, } } diff --git a/matchers/contain_elements_matcher.go b/matchers/contain_elements_matcher.go index aa20b9d4e..9bdd5c5ba 100644 --- a/matchers/contain_elements_matcher.go +++ b/matchers/contain_elements_matcher.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/onsi/gomega/matchers" + "github.com/samber/lo" ) type ContainElementsMatcher struct { @@ -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, } } diff --git a/matchers/have_patterns.go b/matchers/have_patterns.go index cff7032f1..fd7c92897 100644 --- a/matchers/have_patterns.go +++ b/matchers/have_patterns.go @@ -20,6 +20,7 @@ type HavePatternsMatcher struct { Elements interface{} missingElements []string + foundElements []string } func HavePatterns(elements interface{}) GossMatcher { @@ -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 @@ -128,6 +130,7 @@ func (m *HavePatternsMatcher) FailureResult(actual interface{}) MatcherResult { Message: "to have patterns", Expected: m.Elements, MissingElements: m.missingElements, + FoundElements: m.foundElements, } } diff --git a/matchers/matchers.go b/matchers/matchers.go index 93990f564..76e51ebb8 100644 --- a/matchers/matchers.go +++ b/matchers/matchers.go @@ -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{} {