]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgo/go/testing/example.go
libgo: update to Go1.14beta1
[thirdparty/gcc.git] / libgo / go / testing / example.go
CommitLineData
d8f41257
ILT
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.
4
5package testing
6
7import (
d8f41257 8 "fmt"
d8f41257 9 "os"
22b955cc 10 "sort"
df4aa89a 11 "strings"
d8f41257
ILT
12 "time"
13)
14
15type InternalExample struct {
22b955cc
ILT
16 Name string
17 F func()
18 Output string
19 Unordered bool
d8f41257
ILT
20}
21
5a8ea165
ILT
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.
593f74bb 24func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool) {
c2047754
ILT
25 _, ok = runExamples(matchString, examples)
26 return ok
27}
28
29func runExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ran, ok bool) {
d8f41257
ILT
30 ok = true
31
ab61e9c4
ILT
32 var eg InternalExample
33
ab61e9c4 34 for _, eg = range examples {
593f74bb
ILT
35 matched, err := matchString(*match, eg.Name)
36 if err != nil {
37 fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.run: %s\n", err)
38 os.Exit(1)
39 }
40 if !matched {
41 continue
42 }
c2047754 43 ran = true
d6f2922e
ILT
44 if !runExample(eg) {
45 ok = false
d8f41257 46 }
d6f2922e
ILT
47 }
48
c2047754 49 return ran, ok
d6f2922e
ILT
50}
51
22b955cc
ILT
52func sortLines(output string) string {
53 lines := strings.Split(output, "\n")
54 sort.Strings(lines)
55 return strings.Join(lines, "\n")
56}
57
aa8901e9
ILT
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.
61//
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.
65func (eg *InternalExample) processRunResult(stdout string, timeSpent time.Duration, recovered interface{}) (passed bool) {
66 passed = true
67
68 dstr := fmtDuration(timeSpent)
69 var fail string
70 got := strings.TrimSpace(stdout)
71 want := strings.TrimSpace(eg.Output)
72 if eg.Unordered {
73 if sortLines(got) != sortLines(want) && recovered == nil {
74 fail = fmt.Sprintf("got:\n%s\nwant (unordered):\n%s\n", stdout, eg.Output)
d8f41257 75 }
aa8901e9
ILT
76 } else {
77 if got != want && recovered == nil {
78 fail = fmt.Sprintf("got:\n%s\nwant:\n%s\n", got, want)
d6f2922e 79 }
aa8901e9
ILT
80 }
81 if fail != "" || recovered != nil {
82 fmt.Printf("--- FAIL: %s (%s)\n%s", eg.Name, dstr, fail)
83 passed = false
84 } else if *chatty {
85 fmt.Printf("--- PASS: %s (%s)\n", eg.Name, dstr)
86 }
87 if recovered != nil {
88 // Propagate the previously recovered result, by panicking.
89 panic(recovered)
90 }
d8f41257
ILT
91
92 return
93}