From c2da98ebb2daca65484559c3ba8605f33d3a2796 Mon Sep 17 00:00:00 2001 From: jhr22 Date: Wed, 16 Aug 2023 13:59:51 -0400 Subject: [PATCH] Return actual in output of field matcher --- gstruct/fields.go | 80 ++++++++++++++++++++++-------------------- gstruct/fields_test.go | 4 +-- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/gstruct/fields.go b/gstruct/fields.go index faf07b1a2..ac5276ace 100644 --- a/gstruct/fields.go +++ b/gstruct/fields.go @@ -14,51 +14,53 @@ import ( "github.com/onsi/gomega/types" ) -//MatchAllFields succeeds if every field of a struct matches the field matcher associated with -//it, and every element matcher is matched. -// actual := struct{ -// A int -// B []bool -// C string -// }{ -// A: 5, -// B: []bool{true, false}, -// C: "foo", -// } +// MatchAllFields succeeds if every field of a struct matches the field matcher associated with +// it, and every element matcher is matched. // -// Expect(actual).To(MatchAllFields(Fields{ -// "A": Equal(5), -// "B": ConsistOf(true, false), -// "C": Equal("foo"), -// })) +// actual := struct{ +// A int +// B []bool +// C string +// }{ +// A: 5, +// B: []bool{true, false}, +// C: "foo", +// } +// +// Expect(actual).To(MatchAllFields(Fields{ +// "A": Equal(5), +// "B": ConsistOf(true, false), +// "C": Equal("foo"), +// })) func MatchAllFields(fields Fields) types.GomegaMatcher { return &FieldsMatcher{ Fields: fields, } } -//MatchFields succeeds if each element of a struct matches the field matcher associated with -//it. It can ignore extra fields and/or missing fields. -// actual := struct{ -// A int -// B []bool -// C string -// }{ -// A: 5, -// B: []bool{true, false}, -// C: "foo", -// } +// MatchFields succeeds if each element of a struct matches the field matcher associated with +// it. It can ignore extra fields and/or missing fields. +// +// actual := struct{ +// A int +// B []bool +// C string +// }{ +// A: 5, +// B: []bool{true, false}, +// C: "foo", +// } // -// Expect(actual).To(MatchFields(IgnoreExtras, Fields{ -// "A": Equal(5), -// "B": ConsistOf(true, false), -// })) -// Expect(actual).To(MatchFields(IgnoreMissing, Fields{ -// "A": Equal(5), -// "B": ConsistOf(true, false), -// "C": Equal("foo"), -// "D": Equal("extra"), -// })) +// Expect(actual).To(MatchFields(IgnoreExtras, Fields{ +// "A": Equal(5), +// "B": ConsistOf(true, false), +// })) +// Expect(actual).To(MatchFields(IgnoreMissing, Fields{ +// "A": Equal(5), +// "B": ConsistOf(true, false), +// "C": Equal("foo"), +// "D": Equal("extra"), +// })) func MatchFields(options Options, fields Fields) types.GomegaMatcher { return &FieldsMatcher{ Fields: fields, @@ -129,7 +131,7 @@ func (m *FieldsMatcher) matchFields(actual interface{}) (errs []error) { if nesting, ok := matcher.(errorsutil.NestingMatcher); ok { return errorsutil.AggregateError(nesting.Failures()) } - return errors.New(matcher.FailureMessage(field)) + return errors.New(matcher.FailureMessage(actual)) } return nil }() @@ -152,7 +154,7 @@ func (m *FieldsMatcher) FailureMessage(actual interface{}) (message string) { for i := range m.failures { failures[i] = m.failures[i].Error() } - return format.Message(reflect.TypeOf(actual).Name(), + return format.Message(actual, fmt.Sprintf("to match fields: {\n%v\n}\n", strings.Join(failures, "\n"))) } diff --git a/gstruct/fields_test.go b/gstruct/fields_test.go index 5c31c7e4e..a212d4365 100644 --- a/gstruct/fields_test.go +++ b/gstruct/fields_test.go @@ -86,10 +86,10 @@ var _ = Describe("Struct", func() { //we do a number of checks to make sure everything's included m.Match(actual) Expect(m.FailureMessage(actual)).Should(HavePrefix( - "Expected\n : \nto match fields: {\n", + "Expected\n : \nto match fields: {\n", )) Expect(m.FailureMessage(actual)).Should(ContainSubstring( - ".A:\n Expected\n : b\n to equal\n : a\n", + ".A:\n Expected\n : b\n to equal\n : a\n", )) Expect(m.FailureMessage(actual)).Should(ContainSubstring( "missing expected field B\n",