]> Cypherpunks repositories - gostls13.git/commitdiff
testing: modify got,want equal comparison for unordered example output
authorJes Cok <xigua67damn@gmail.com>
Mon, 3 Mar 2025 13:34:15 +0000 (13:34 +0000)
committerGopher Robot <gobot@golang.org>
Wed, 5 Mar 2025 15:44:41 +0000 (07:44 -0800)
This change eliminates sortLines function to avoid strings.Join calls.

It's not a performance problem, this change tries to make the comparison
more straightforward.

Change-Id: I3a7ae877c9fc927833ab9f143205f7e007197f60
GitHub-Last-Rev: a71aa58c58533fed24ba9c101664b977a094caf9
GitHub-Pull-Request: golang/go#72025
Reviewed-on: https://go-review.googlesource.com/c/go/+/653556
Reviewed-by: Junyang Shao <shaojunyang@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/testing/example.go

index c343ae2aa2f39336c1d881449af602ff0fa079f0..58c36f2edb187f69e07a1eb4343a217265852c5f 100644 (file)
@@ -46,12 +46,6 @@ func runExamples(matchString func(pat, str string) (bool, error), examples []Int
        return ran, ok
 }
 
-func sortLines(output string) string {
-       lines := strings.Split(output, "\n")
-       slices.Sort(lines)
-       return strings.Join(lines, "\n")
-}
-
 // processRunResult computes a summary and status of the result of running an example test.
 // stdout is the captured output from stdout of the test.
 // recovered is the result of invoking recover after running the test, in case it panicked.
@@ -72,7 +66,9 @@ func (eg *InternalExample) processRunResult(stdout string, timeSpent time.Durati
                want = strings.ReplaceAll(want, "\r\n", "\n")
        }
        if eg.Unordered {
-               if sortLines(got) != sortLines(want) && recovered == nil {
+               gotLines := slices.Sorted(strings.SplitSeq(got, "\n"))
+               wantLines := slices.Sorted(strings.SplitSeq(want, "\n"))
+               if !slices.Equal(gotLines, wantLines) && recovered == nil {
                        fail = fmt.Sprintf("got:\n%s\nwant (unordered):\n%s\n", stdout, eg.Output)
                }
        } else {