From dd725333f3de28fceb3b209630c580fb03144d54 Mon Sep 17 00:00:00 2001 From: Mike Auclair Date: Mon, 24 Jun 2024 20:29:26 +0000 Subject: [PATCH 1/4] wip --- mock/mock.go | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/mock/mock.go b/mock/mock.go index d5eb1ef55..a28f686c4 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -923,6 +923,8 @@ func (args Arguments) Is(objects ...interface{}) bool { return true } +type outputRenderer interface{} + // Diff gets a string describing the differences between the arguments // and the specified objects. // @@ -930,7 +932,7 @@ func (args Arguments) Is(objects ...interface{}) bool { func (args Arguments) Diff(objects []interface{}) (string, int) { // TODO: could return string as error and nil for No difference - output := "\n" + var outputBuilder strings.Builder var differences int maxArgCount := len(args) @@ -938,7 +940,10 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { maxArgCount = len(objects) } - for i := 0; i < maxArgCount; i++ { + outputRenderers := []outputRenderer{} + + for j := 0; j < maxArgCount; j++ { + i := j var actual, expected interface{} var actualFmt, expectedFmt string @@ -969,10 +974,12 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { matches = matcher.Matches(actual) }() if matches { - output = fmt.Sprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actualFmt, matcher) + outputRenderers = append(outputRenderers, func() string { + return fmt.Sprintf("\t%d: PASS: %s matched by %s\n", i, actualFmt, matcher) + }) } else { differences++ - output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s not matched by %s\n", i, actualFmt, matcher)) } } else { switch expected := expected.(type) { @@ -981,13 +988,13 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { if reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected) { // not match differences++ - output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected, reflect.TypeOf(actual).Name(), actualFmt)) } case *IsTypeArgument: actualT := reflect.TypeOf(actual) if actualT != expected.t { differences++ - output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected.t.Name(), actualT.Name(), actualFmt) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected.t.Name(), actualT.Name(), actualFmt)) } case *FunctionalOptionsArgument: t := expected.value @@ -1001,26 +1008,30 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { tName := reflect.TypeOf(t).Name() if name != reflect.TypeOf(actual).String() && tValue.Len() != 0 { differences++ - output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, reflect.TypeOf(actual).Name(), actualFmt) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, tName, reflect.TypeOf(actual).Name(), actualFmt)) } else { if ef, af := assertOpts(t, actual); ef == "" && af == "" { // match - output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, tName, tName) + outputRenderers = append(outputRenderers, func() string { + return fmt.Sprintf("\t%d: PASS: %s == %s\n", i, tName, tName) + }) } else { // not match differences++ - output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, af, ef) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s != %s\n", i, af, ef)) } } default: - if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) { + if expected == Anything || assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) { // match - output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt) + outputRenderers = append(outputRenderers, func() string { + return fmt.Sprintf("\t%d: PASS: %s == %s\n", i, actualFmt, expectedFmt) + }) } else { // not match differences++ - output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s != %s\n", i, actualFmt, expectedFmt)) } } } @@ -1031,7 +1042,19 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { return "No differences.", differences } - return output, differences + outputBuilder.WriteString("\n") + for _, renderer := range outputRenderers { + switch r := renderer.(type) { + case string: + outputBuilder.WriteString(r) + case func() string: + outputBuilder.WriteString(r()) + default: + panic("Invalid Output Renderer") + } + } + + return outputBuilder.String(), differences } // Assert compares the arguments with the specified objects and fails if From 2b53603313959458632f3becbb6ad50c0a2061c3 Mon Sep 17 00:00:00 2001 From: Mike Auclair Date: Tue, 25 Jun 2024 14:59:20 +0000 Subject: [PATCH 2/4] wip --- mock/mock.go | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/mock/mock.go b/mock/mock.go index a28f686c4..c8ed892bd 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -945,22 +945,30 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { for j := 0; j < maxArgCount; j++ { i := j var actual, expected interface{} - var actualFmt, expectedFmt string + var actualFmt, expectedFmt func() string if len(objects) <= i { actual = "(Missing)" - actualFmt = "(Missing)" + actualFmt = func() string { + return "(Missing)" + } } else { actual = objects[i] - actualFmt = fmt.Sprintf("(%[1]T=%[1]v)", actual) + actualFmt = func() string { + return fmt.Sprintf("(%[1]T=%[1]v)", actual) + } } if len(args) <= i { expected = "(Missing)" - expectedFmt = "(Missing)" + expectedFmt = func() string { + return "(Missing)" + } } else { expected = args[i] - expectedFmt = fmt.Sprintf("(%[1]T=%[1]v)", expected) + expectedFmt = func() string { + return fmt.Sprintf("(%[1]T=%[1]v)", expected) + } } if matcher, ok := expected.(argumentMatcher); ok { @@ -968,18 +976,20 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { func() { defer func() { if r := recover(); r != nil { - actualFmt = fmt.Sprintf("panic in argument matcher: %v", r) + actualFmt = func() string { + return fmt.Sprintf("panic in argument matcher: %v", r) + } } }() matches = matcher.Matches(actual) }() if matches { outputRenderers = append(outputRenderers, func() string { - return fmt.Sprintf("\t%d: PASS: %s matched by %s\n", i, actualFmt, matcher) + return fmt.Sprintf("\t%d: PASS: %s matched by %s\n", i, actualFmt(), matcher) }) } else { differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s not matched by %s\n", i, actualFmt, matcher)) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s not matched by %s\n", i, actualFmt(), matcher)) } } else { switch expected := expected.(type) { @@ -988,13 +998,13 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { if reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected) { // not match differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected, reflect.TypeOf(actual).Name(), actualFmt)) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected, reflect.TypeOf(actual).Name(), actualFmt())) } case *IsTypeArgument: actualT := reflect.TypeOf(actual) if actualT != expected.t { differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected.t.Name(), actualT.Name(), actualFmt)) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected.t.Name(), actualT.Name(), actualFmt())) } case *FunctionalOptionsArgument: t := expected.value @@ -1008,7 +1018,7 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { tName := reflect.TypeOf(t).Name() if name != reflect.TypeOf(actual).String() && tValue.Len() != 0 { differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, tName, reflect.TypeOf(actual).Name(), actualFmt)) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, tName, reflect.TypeOf(actual).Name(), actualFmt())) } else { if ef, af := assertOpts(t, actual); ef == "" && af == "" { // match @@ -1026,12 +1036,12 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { if expected == Anything || assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) { // match outputRenderers = append(outputRenderers, func() string { - return fmt.Sprintf("\t%d: PASS: %s == %s\n", i, actualFmt, expectedFmt) + return fmt.Sprintf("\t%d: PASS: %s == %s\n", i, actualFmt(), expectedFmt()) }) } else { // not match differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s != %s\n", i, actualFmt, expectedFmt)) + outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s != %s\n", i, actualFmt(), expectedFmt())) } } } From 3d98e693c74692c3a66c6cb55cb6f4cac9b52516 Mon Sep 17 00:00:00 2001 From: Mike Auclair Date: Tue, 25 Jun 2024 16:12:07 +0000 Subject: [PATCH 3/4] cleanup --- mock/mock.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mock/mock.go b/mock/mock.go index c8ed892bd..ba66ad5e6 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -1033,7 +1033,7 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { } default: - if expected == Anything || assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) { + if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) { // match outputRenderers = append(outputRenderers, func() string { return fmt.Sprintf("\t%d: PASS: %s == %s\n", i, actualFmt(), expectedFmt()) From cbf6e73c7e22a394e39dd30ca0d1368813ceb1e7 Mon Sep 17 00:00:00 2001 From: Mike Auclair Date: Tue, 17 Dec 2024 18:30:48 +0000 Subject: [PATCH 4/4] simplified renderers --- mock/mock.go | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/mock/mock.go b/mock/mock.go index ba66ad5e6..0feb48218 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -923,7 +923,7 @@ func (args Arguments) Is(objects ...interface{}) bool { return true } -type outputRenderer interface{} +type outputRenderer func() string // Diff gets a string describing the differences between the arguments // and the specified objects. @@ -989,7 +989,9 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { }) } else { differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s not matched by %s\n", i, actualFmt(), matcher)) + outputRenderers = append(outputRenderers, func() string { + return fmt.Sprintf("\t%d: FAIL: %s not matched by %s\n", i, actualFmt(), matcher) + }) } } else { switch expected := expected.(type) { @@ -998,13 +1000,17 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { if reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected) { // not match differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected, reflect.TypeOf(actual).Name(), actualFmt())) + outputRenderers = append(outputRenderers, func() string { + return fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected, reflect.TypeOf(actual).Name(), actualFmt()) + }) } case *IsTypeArgument: actualT := reflect.TypeOf(actual) if actualT != expected.t { differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected.t.Name(), actualT.Name(), actualFmt())) + outputRenderers = append(outputRenderers, func() string { + return fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected.t.Name(), actualT.Name(), actualFmt()) + }) } case *FunctionalOptionsArgument: t := expected.value @@ -1018,7 +1024,9 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { tName := reflect.TypeOf(t).Name() if name != reflect.TypeOf(actual).String() && tValue.Len() != 0 { differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, tName, reflect.TypeOf(actual).Name(), actualFmt())) + outputRenderers = append(outputRenderers, func() string { + return fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, tName, reflect.TypeOf(actual).Name(), actualFmt()) + }) } else { if ef, af := assertOpts(t, actual); ef == "" && af == "" { // match @@ -1028,7 +1036,9 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { } else { // not match differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s != %s\n", i, af, ef)) + outputRenderers = append(outputRenderers, func() string { + return fmt.Sprintf("\t%d: FAIL: %s != %s\n", i, af, ef) + }) } } @@ -1041,7 +1051,9 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { } else { // not match differences++ - outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s != %s\n", i, actualFmt(), expectedFmt())) + outputRenderers = append(outputRenderers, func() string { + return fmt.Sprintf("\t%d: FAIL: %s != %s\n", i, actualFmt(), expectedFmt()) + }) } } } @@ -1053,15 +1065,8 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { } outputBuilder.WriteString("\n") - for _, renderer := range outputRenderers { - switch r := renderer.(type) { - case string: - outputBuilder.WriteString(r) - case func() string: - outputBuilder.WriteString(r()) - default: - panic("Invalid Output Renderer") - } + for _, r := range outputRenderers { + outputBuilder.WriteString(r()) } return outputBuilder.String(), differences