aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/testing/example.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/testing/example.go')
-rw-r--r--libgo/go/testing/example.go92
1 files changed, 31 insertions, 61 deletions
diff --git a/libgo/go/testing/example.go b/libgo/go/testing/example.go
index f4beb76..c122121 100644
--- a/libgo/go/testing/example.go
+++ b/libgo/go/testing/example.go
@@ -6,7 +6,6 @@ package testing
import (
"fmt"
- "io"
"os"
"sort"
"strings"
@@ -56,68 +55,39 @@ func sortLines(output string) string {
return strings.Join(lines, "\n")
}
-func runExample(eg InternalExample) (ok bool) {
- if *chatty {
- fmt.Printf("=== RUN %s\n", eg.Name)
- }
-
- // 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() {
- var buf strings.Builder
- _, err := io.Copy(&buf, r)
- r.Close()
- if err != nil {
- fmt.Fprintf(os.Stderr, "testing: copying pipe: %v\n", err)
- os.Exit(1)
+// processRunResult computes a summary and status of the result of running an example 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.
+//
+// If stdout doesn't match the expected output or if recovered is non-nil, it'll print the cause of failure to stdout.
+// If the test is chatty/verbose, it'll print a success message to stdout.
+// If recovered is non-nil, it'll panic with that value.
+func (eg *InternalExample) processRunResult(stdout string, timeSpent time.Duration, recovered interface{}) (passed bool) {
+ passed = true
+
+ dstr := fmtDuration(timeSpent)
+ var fail string
+ got := strings.TrimSpace(stdout)
+ want := strings.TrimSpace(eg.Output)
+ if eg.Unordered {
+ if sortLines(got) != sortLines(want) && recovered == nil {
+ fail = fmt.Sprintf("got:\n%s\nwant (unordered):\n%s\n", stdout, eg.Output)
}
- outC <- buf.String()
- }()
-
- start := time.Now()
- ok = true
-
- // Clean up in a deferred call so we can recover if the example panics.
- defer func() {
- dstr := fmtDuration(time.Since(start))
-
- // Close pipe, restore stdout, get output.
- w.Close()
- os.Stdout = stdout
- out := <-outC
-
- var fail string
- err := recover()
- got := strings.TrimSpace(out)
- want := strings.TrimSpace(eg.Output)
- if eg.Unordered {
- if sortLines(got) != sortLines(want) && err == nil {
- fail = fmt.Sprintf("got:\n%s\nwant (unordered):\n%s\n", out, eg.Output)
- }
- } else {
- if got != want && err == nil {
- fail = fmt.Sprintf("got:\n%s\nwant:\n%s\n", got, want)
- }
- }
- if fail != "" || err != nil {
- fmt.Printf("--- FAIL: %s (%s)\n%s", eg.Name, dstr, fail)
- ok = false
- } else if *chatty {
- fmt.Printf("--- PASS: %s (%s)\n", eg.Name, dstr)
- }
- if err != nil {
- panic(err)
+ } else {
+ if got != want && recovered == nil {
+ fail = fmt.Sprintf("got:\n%s\nwant:\n%s\n", got, want)
}
- }()
+ }
+ if fail != "" || recovered != nil {
+ fmt.Printf("--- FAIL: %s (%s)\n%s", eg.Name, dstr, fail)
+ passed = false
+ } else if *chatty {
+ fmt.Printf("--- PASS: %s (%s)\n", eg.Name, dstr)
+ }
+ if recovered != nil {
+ // Propagate the previously recovered result, by panicking.
+ panic(recovered)
+ }
- // Run example.
- eg.F()
return
}