diff options
Diffstat (limited to 'libgo/go/testing/testing.go')
-rw-r--r-- | libgo/go/testing/testing.go | 119 |
1 files changed, 65 insertions, 54 deletions
diff --git a/libgo/go/testing/testing.go b/libgo/go/testing/testing.go index c1917f8..312d287 100644 --- a/libgo/go/testing/testing.go +++ b/libgo/go/testing/testing.go @@ -10,7 +10,7 @@ // [a-z]) and serves to identify the test routine. // These TestXxx routines should be declared within the package they are testing. // -// Tests may be skipped if not applicable like this: +// Tests and benchmarks may be skipped if not applicable like this: // func TestTimeConsuming(t *testing.T) { // if testing.Short() { // t.Skip("skipping test in short mode.") @@ -18,6 +18,8 @@ // ... // } // +// Benchmarks +// // Functions of the form // func BenchmarkXxx(*testing.B) // are considered benchmarks, and are executed by the "go test" command when @@ -33,6 +35,7 @@ // } // } // +// The benchmark function must run the target code b.N times. // The benchmark package will vary b.N until the benchmark function lasts // long enough to be timed reliably. The output // BenchmarkHello 10000000 282 ns/op @@ -48,8 +51,10 @@ // } // } // +// Examples +// // The package also runs and verifies example code. Example functions may -// include a concluding comment that begins with "Output:" and is compared with +// include a concluding line comment that begins with "Output:" and is compared with // the standard output of the function when the tests are run. (The comparison // ignores leading and trailing space.) These are examples of an example: // @@ -129,9 +134,10 @@ var ( // common holds the elements common between T and B and // captures common methods such as Errorf. type common struct { - mu sync.RWMutex // guards output and failed - output []byte // Output generated by test or benchmark. - failed bool // Test or benchmark has failed. + mu sync.RWMutex // guards output and failed + output []byte // Output generated by test or benchmark. + failed bool // Test or benchmark has failed. + skipped bool // Test of benchmark has been skipped. start time.Time // Time test or benchmark started duration time.Duration @@ -165,21 +171,17 @@ func decorate(s string) string { line = 1 } buf := new(bytes.Buffer) + // Every line is indented at least one tab. + buf.WriteByte('\t') fmt.Fprintf(buf, "%s:%d: ", file, line) - lines := strings.Split(s, "\n") if l := len(lines); l > 1 && lines[l-1] == "" { lines = lines[:l-1] } for i, line := range lines { if i > 0 { - buf.WriteByte('\n') - } - // Every line is indented at least one tab. - buf.WriteByte('\t') - if i > 0 { // Second and subsequent lines are indented an extra tab. - buf.WriteByte('\t') + buf.WriteString("\n\t\t") } buf.WriteString(line) } @@ -193,7 +195,6 @@ type T struct { common name string // Name of test. startParallel chan bool // Parallel tests will wait on this. - skipped bool // Test has been skipped. } // Fail marks the function as having failed but continues execution. @@ -212,6 +213,10 @@ func (c *common) Failed() bool { // FailNow marks the function as having failed and stops its execution. // Execution will continue at the next test or benchmark. +// FailNow must be called from the goroutine running the +// test or benchmark function, not from other goroutines +// created during the test. Calling FailNow does not stop +// those other goroutines. func (c *common) FailNow() { c.Fail() @@ -244,40 +249,77 @@ func (c *common) log(s string) { c.output = append(c.output, decorate(s)...) } -// Log formats its arguments using default formatting, analogous to Println(), -// and records the text in the error log. +// Log formats its arguments using default formatting, analogous to Println, +// and records the text in the error log. The text will be printed only if +// the test fails or the -test.v flag is set. func (c *common) Log(args ...interface{}) { c.log(fmt.Sprintln(args...)) } -// Logf formats its arguments according to the format, analogous to Printf(), -// and records the text in the error log. +// Logf formats its arguments according to the format, analogous to Printf, +// and records the text in the error log. The text will be printed only if +// the test fails or the -test.v flag is set. func (c *common) Logf(format string, args ...interface{}) { c.log(fmt.Sprintf(format, args...)) } -// Error is equivalent to Log() followed by Fail(). +// Error is equivalent to Log followed by Fail. func (c *common) Error(args ...interface{}) { c.log(fmt.Sprintln(args...)) c.Fail() } -// Errorf is equivalent to Logf() followed by Fail(). +// Errorf is equivalent to Logf followed by Fail. func (c *common) Errorf(format string, args ...interface{}) { c.log(fmt.Sprintf(format, args...)) c.Fail() } -// Fatal is equivalent to Log() followed by FailNow(). +// Fatal is equivalent to Log followed by FailNow. func (c *common) Fatal(args ...interface{}) { c.log(fmt.Sprintln(args...)) c.FailNow() } -// Fatalf is equivalent to Logf() followed by FailNow(). +// Fatalf is equivalent to Logf followed by FailNow. func (c *common) Fatalf(format string, args ...interface{}) { c.log(fmt.Sprintf(format, args...)) c.FailNow() } +// Skip is equivalent to Log followed by SkipNow. +func (c *common) Skip(args ...interface{}) { + c.log(fmt.Sprintln(args...)) + c.SkipNow() +} + +// Skipf is equivalent to Logf followed by SkipNow. +func (c *common) Skipf(format string, args ...interface{}) { + c.log(fmt.Sprintf(format, args...)) + c.SkipNow() +} + +// SkipNow marks the test as having been skipped and stops its execution. +// Execution will continue at the next test or benchmark. See also FailNow. +// SkipNow must be called from the goroutine running the test, not from +// other goroutines created during the test. Calling SkipNow does not stop +// those other goroutines. +func (c *common) SkipNow() { + c.skip() + runtime.Goexit() +} + +func (c *common) skip() { + c.mu.Lock() + defer c.mu.Unlock() + c.skipped = true +} + +// Skipped reports whether the test was skipped. +func (c *common) Skipped() bool { + c.mu.RLock() + defer c.mu.RUnlock() + return c.skipped +} + // Parallel signals that this test is to be run in parallel with (and only with) -// other parallel tests in this CPU group. +// other parallel tests. func (t *T) Parallel() { t.signal <- (*T)(nil) // Release main testing loop <-t.startParallel // Wait for serial tests to finish @@ -301,6 +343,7 @@ func tRunner(t *T, test *InternalTest) { t.duration = time.Now().Sub(t.start) // If the test panicked, print any test output before dying. if err := recover(); err != nil { + t.Fail() t.report() panic(err) } @@ -345,38 +388,6 @@ func (t *T) report() { } } -// Skip is equivalent to Log() followed by SkipNow(). -func (t *T) Skip(args ...interface{}) { - t.log(fmt.Sprintln(args...)) - t.SkipNow() -} - -// Skipf is equivalent to Logf() followed by SkipNow(). -func (t *T) Skipf(format string, args ...interface{}) { - t.log(fmt.Sprintf(format, args...)) - t.SkipNow() -} - -// SkipNow marks the function as having been skipped and stops its execution. -// Execution will continue at the next test or benchmark. See also, t.FailNow. -func (t *T) SkipNow() { - t.skip() - runtime.Goexit() -} - -func (t *T) skip() { - t.mu.Lock() - defer t.mu.Unlock() - t.skipped = true -} - -// Skipped reports whether the function was skipped. -func (t *T) Skipped() bool { - t.mu.RLock() - defer t.mu.RUnlock() - return t.skipped -} - func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool) { ok = true if len(tests) == 0 && !haveExamples { |