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.
16 type InternalExample
struct {
22 func RunExamples(matchString
func(pat
, str
string) (bool, error
), examples
[]InternalExample
) (ok
bool) {
25 var eg InternalExample
27 for _
, eg
= range examples
{
28 matched
, err
:= matchString(*match
, eg
.Name
)
30 fmt
.Fprintf(os
.Stderr
, "testing: invalid regexp for -test.run: %s\n", err
)
44 func runExample(eg InternalExample
) (ok
bool) {
46 fmt
.Printf("=== RUN: %s\n", eg
.Name
)
51 r
, w
, err
:= os
.Pipe()
53 fmt
.Fprintln(os
.Stderr
, err
)
57 outC
:= make(chan string)
59 buf
:= new(bytes
.Buffer
)
60 _
, err
:= io
.Copy(buf
, r
)
63 fmt
.Fprintf(os
.Stderr
, "testing: copying pipe: %v\n", err
)
72 // Clean up in a deferred call so we can recover if the example panics.
74 d
:= time
.Now().Sub(start
)
76 // Close pipe, restore stdout, get output.
83 if g
, e
:= strings
.TrimSpace(out
), strings
.TrimSpace(eg
.Output
); g
!= e
&& err
== nil {
84 fail
= fmt
.Sprintf("got:\n%s\nwant:\n%s\n", g
, e
)
86 if fail
!= "" || err
!= nil {
87 fmt
.Printf("--- FAIL: %s (%v)\n%s", eg
.Name
, d
, fail
)
90 fmt
.Printf("--- PASS: %s (%v)\n", eg
.Name
, d
)