1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 type InternalExample
struct {
22 // RunExamples is an internal function but exported because it is cross-package;
23 // it is part of the implementation of the "go test" command.
24 func RunExamples(matchString
func(pat
, str
string) (bool, error
), examples
[]InternalExample
) (ok
bool) {
25 _
, ok
= runExamples(matchString
, examples
)
29 func runExamples(matchString
func(pat
, str
string) (bool, error
), examples
[]InternalExample
) (ran
, ok
bool) {
32 var eg InternalExample
34 for _
, eg
= range examples
{
35 matched
, err
:= matchString(*match
, eg
.Name
)
37 fmt
.Fprintf(os
.Stderr
, "testing: invalid regexp for -test.run: %s\n", err
)
52 func sortLines(output
string) string {
53 lines
:= strings
.Split(output
, "\n")
55 return strings
.Join(lines
, "\n")
58 // processRunResult computes a summary and status of the result of running an example test.
59 // stdout is the captured output from stdout of the test.
60 // recovered is the result of invoking recover after running the test, in case it panicked.
62 // If stdout doesn't match the expected output or if recovered is non-nil, it'll print the cause of failure to stdout.
63 // If the test is chatty/verbose, it'll print a success message to stdout.
64 // If recovered is non-nil, it'll panic with that value.
65 // If the test panicked with nil, or invoked runtime.Goexit, it'll be
66 // made to fail and panic with errNilPanicOrGoexit
67 func (eg
*InternalExample
) processRunResult(stdout
string, timeSpent time
.Duration
, finished
bool, recovered any
) (passed
bool) {
69 dstr
:= fmtDuration(timeSpent
)
71 got
:= strings
.TrimSpace(stdout
)
72 want
:= strings
.TrimSpace(eg
.Output
)
74 if sortLines(got
) != sortLines(want
) && recovered
== nil {
75 fail
= fmt
.Sprintf("got:\n%s\nwant (unordered):\n%s\n", stdout
, eg
.Output
)
78 if got
!= want
&& recovered
== nil {
79 fail
= fmt
.Sprintf("got:\n%s\nwant:\n%s\n", got
, want
)
82 if fail
!= "" ||
!finished || recovered
!= nil {
83 fmt
.Printf("--- FAIL: %s (%s)\n%s", eg
.Name
, dstr
, fail
)
86 fmt
.Printf("--- PASS: %s (%s)\n", eg
.Name
, dstr
)
90 // Propagate the previously recovered result, by panicking.
93 if !finished
&& recovered
== nil {
94 panic(errNilPanicOrGoexit
)