2011-10-27 01:57:58 +02:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2012-01-13 06:11:45 +01:00
|
|
|
"strings"
|
2011-10-27 01:57:58 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InternalExample struct {
|
|
|
|
Name string
|
|
|
|
F func()
|
|
|
|
Output string
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool) {
|
2011-10-27 01:57:58 +02:00
|
|
|
ok = true
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
var eg InternalExample
|
|
|
|
|
|
|
|
for _, eg = range examples {
|
2012-03-06 18:57:23 +01:00
|
|
|
matched, err := matchString(*match, eg.Name)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.run: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if !matched {
|
|
|
|
continue
|
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
if !runExample(eg) {
|
|
|
|
ok = false
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func runExample(eg InternalExample) (ok bool) {
|
|
|
|
if *chatty {
|
|
|
|
fmt.Printf("=== RUN: %s\n", eg.Name)
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// Capture stdout.
|
|
|
|
stdout := os.Stdout
|
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Stdout = w
|
|
|
|
outC := make(chan string)
|
|
|
|
go func() {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err := io.Copy(buf, r)
|
|
|
|
r.Close()
|
2011-10-27 01:57:58 +02:00
|
|
|
if err != nil {
|
2013-01-29 21:52:43 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "testing: copying pipe: %v\n", err)
|
2011-10-27 01:57:58 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
outC <- buf.String()
|
|
|
|
}()
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
ok = true
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// Clean up in a deferred call so we can recover if the example panics.
|
|
|
|
defer func() {
|
2015-01-15 01:27:56 +01:00
|
|
|
dstr := fmtDuration(time.Now().Sub(start))
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// Close pipe, restore stdout, get output.
|
2011-10-27 01:57:58 +02:00
|
|
|
w.Close()
|
2012-12-22 02:15:33 +01:00
|
|
|
os.Stdout = stdout
|
2011-10-27 01:57:58 +02:00
|
|
|
out := <-outC
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
var fail string
|
|
|
|
err := recover()
|
|
|
|
if g, e := strings.TrimSpace(out), strings.TrimSpace(eg.Output); g != e && err == nil {
|
|
|
|
fail = fmt.Sprintf("got:\n%s\nwant:\n%s\n", g, e)
|
|
|
|
}
|
|
|
|
if fail != "" || err != nil {
|
2015-01-15 01:27:56 +01:00
|
|
|
fmt.Printf("--- FAIL: %s (%s)\n%s", eg.Name, dstr, fail)
|
2011-10-27 01:57:58 +02:00
|
|
|
ok = false
|
|
|
|
} else if *chatty {
|
2015-01-15 01:27:56 +01:00
|
|
|
fmt.Printf("--- PASS: %s (%s)\n", eg.Name, dstr)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// Run example.
|
|
|
|
eg.F()
|
2011-10-27 01:57:58 +02:00
|
|
|
return
|
|
|
|
}
|