mirror of
https://github.com/golang/go.git
synced 2025-10-19 19:13:18 +00:00
testing: optimize got,want equal comparison for unordered example output
Introduce sortedLinesEqual function which avoids strings.Join calls, and replace strings equal comparation with slices.Equal.
This commit is contained in:
parent
b199d9766a
commit
a71aa58c58
1 changed files with 3 additions and 7 deletions
|
@ -46,12 +46,6 @@ func runExamples(matchString func(pat, str string) (bool, error), examples []Int
|
||||||
return ran, ok
|
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.
|
// processRunResult computes a summary and status of the result of running an example test.
|
||||||
// stdout is the captured output from stdout of the 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.
|
// 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")
|
want = strings.ReplaceAll(want, "\r\n", "\n")
|
||||||
}
|
}
|
||||||
if eg.Unordered {
|
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)
|
fail = fmt.Sprintf("got:\n%s\nwant (unordered):\n%s\n", stdout, eg.Output)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue