Skip to content

Commit 3bdbc4e

Browse files
committedOct 29, 2024
stop memoizing result of HaveField
fixes #787
1 parent e35358d commit 3bdbc4e

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed
 

‎matchers/have_field.go

+23-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (e missingFieldError) Error() string {
1717
return string(e)
1818
}
1919

20-
func extractField(actual interface{}, field string, matchername string) (interface{}, error) {
20+
func extractField(actual interface{}, field string, matchername string) (any, error) {
2121
fields := strings.SplitN(field, ".", 2)
2222
actualValue := reflect.ValueOf(actual)
2323

@@ -64,36 +64,46 @@ func extractField(actual interface{}, field string, matchername string) (interfa
6464
type HaveFieldMatcher struct {
6565
Field string
6666
Expected interface{}
67+
}
6768

68-
extractedField interface{}
69-
expectedMatcher omegaMatcher
69+
func (matcher *HaveFieldMatcher) expectedMatcher() omegaMatcher {
70+
var isMatcher bool
71+
expectedMatcher, isMatcher := matcher.Expected.(omegaMatcher)
72+
if !isMatcher {
73+
expectedMatcher = &EqualMatcher{Expected: matcher.Expected}
74+
}
75+
return expectedMatcher
7076
}
7177

7278
func (matcher *HaveFieldMatcher) Match(actual interface{}) (success bool, err error) {
73-
matcher.extractedField, err = extractField(actual, matcher.Field, "HaveField")
79+
extractedField, err := extractField(actual, matcher.Field, "HaveField")
7480
if err != nil {
7581
return false, err
7682
}
7783

78-
var isMatcher bool
79-
matcher.expectedMatcher, isMatcher = matcher.Expected.(omegaMatcher)
80-
if !isMatcher {
81-
matcher.expectedMatcher = &EqualMatcher{Expected: matcher.Expected}
82-
}
83-
84-
return matcher.expectedMatcher.Match(matcher.extractedField)
84+
return matcher.expectedMatcher().Match(extractedField)
8585
}
8686

8787
func (matcher *HaveFieldMatcher) FailureMessage(actual interface{}) (message string) {
88+
extractedField, err := extractField(actual, matcher.Field, "HaveField")
89+
if err != nil {
90+
// this really shouldn't happen
91+
return fmt.Sprintf("Failed to extract field '%s': %s", matcher.Field, err)
92+
}
8893
message = fmt.Sprintf("Value for field '%s' failed to satisfy matcher.\n", matcher.Field)
89-
message += matcher.expectedMatcher.FailureMessage(matcher.extractedField)
94+
message += matcher.expectedMatcher().FailureMessage(extractedField)
9095

9196
return message
9297
}
9398

9499
func (matcher *HaveFieldMatcher) NegatedFailureMessage(actual interface{}) (message string) {
100+
extractedField, err := extractField(actual, matcher.Field, "HaveField")
101+
if err != nil {
102+
// this really shouldn't happen
103+
return fmt.Sprintf("Failed to extract field '%s': %s", matcher.Field, err)
104+
}
95105
message = fmt.Sprintf("Value for field '%s' satisfied matcher, but should not have.\n", matcher.Field)
96-
message += matcher.expectedMatcher.NegatedFailureMessage(matcher.extractedField)
106+
message += matcher.expectedMatcher().NegatedFailureMessage(extractedField)
97107

98108
return message
99109
}

‎matchers/have_field_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,17 @@ var _ = Describe("HaveField", func() {
140140
})
141141

142142
Describe("Failure Messages", func() {
143-
It("renders the underlying matcher failure", func() {
143+
It("renders the underlying matcher failure without caching the object", func() {
144144
matcher := HaveField("Title", "Les Mis")
145145
success, err := matcher.Match(book)
146146
Ω(success).Should(BeFalse())
147147
Ω(err).ShouldNot(HaveOccurred())
148148

149+
book.Title = "Les Miser"
149150
msg := matcher.FailureMessage(book)
150-
Ω(msg).Should(Equal("Value for field 'Title' failed to satisfy matcher.\nExpected\n <string>: Les Miserables\nto equal\n <string>: Les Mis"))
151+
Ω(msg).Should(Equal("Value for field 'Title' failed to satisfy matcher.\nExpected\n <string>: Les Miser\nto equal\n <string>: Les Mis"))
151152

153+
book.Title = "Les Miserables"
152154
matcher = HaveField("Title", "Les Miserables")
153155
success, err = matcher.Match(book)
154156
Ω(success).Should(BeTrue())

0 commit comments

Comments
 (0)
Please sign in to comment.