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.
17 type InternalExample
struct {
24 // An internal function but exported because it is cross-package; part of the implementation
25 // of the "go test" command.
26 func RunExamples(matchString
func(pat
, str
string) (bool, error
), examples
[]InternalExample
) (ok
bool) {
27 _
, ok
= runExamples(matchString
, examples
)
31 func runExamples(matchString
func(pat
, str
string) (bool, error
), examples
[]InternalExample
) (ran
, ok
bool) {
34 var eg InternalExample
36 for _
, eg
= range examples
{
37 matched
, err
:= matchString(*match
, eg
.Name
)
39 fmt
.Fprintf(os
.Stderr
, "testing: invalid regexp for -test.run: %s\n", err
)
54 func sortLines(output
string) string {
55 lines
:= strings
.Split(output
, "\n")
57 return strings
.Join(lines
, "\n")
60 func runExample(eg InternalExample
) (ok
bool) {
62 fmt
.Printf("=== RUN %s\n", eg
.Name
)
67 r
, w
, err
:= os
.Pipe()
69 fmt
.Fprintln(os
.Stderr
, err
)
73 outC
:= make(chan string)
76 _
, err
:= io
.Copy(&buf
, r
)
79 fmt
.Fprintf(os
.Stderr
, "testing: copying pipe: %v\n", err
)
88 // Clean up in a deferred call so we can recover if the example panics.
90 dstr
:= fmtDuration(time
.Since(start
))
92 // Close pipe, restore stdout, get output.
99 got
:= strings
.TrimSpace(out
)
100 want
:= strings
.TrimSpace(eg
.Output
)
102 if sortLines(got
) != sortLines(want
) && err
== nil {
103 fail
= fmt
.Sprintf("got:\n%s\nwant (unordered):\n%s\n", out
, eg
.Output
)
106 if got
!= want
&& err
== nil {
107 fail
= fmt
.Sprintf("got:\n%s\nwant:\n%s\n", got
, want
)
110 if fail
!= "" || err
!= nil {
111 fmt
.Printf("--- FAIL: %s (%s)\n%s", eg
.Name
, dstr
, fail
)
114 fmt
.Printf("--- PASS: %s (%s)\n", eg
.Name
, dstr
)