2008-11-18 16:29:10 -07: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.
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// The testing package provides support for automated testing of Go packages.
|
|
|
|
// It is intended to be used in concert with the ``gotest'' utility, which automates
|
|
|
|
// execution of any function of the form
|
|
|
|
// func TestXxx(*testing.T)
|
|
|
|
// where Xxx can by any alphanumeric string (but the first letter must not be in
|
|
|
|
// [a-z]) and serves to identify the test routine.
|
|
|
|
// These TestXxx routines should be declared within the package they are testing.
|
2008-11-18 16:29:10 -07:00
|
|
|
package testing
|
|
|
|
|
2008-11-18 18:52:05 -07:00
|
|
|
import (
|
2008-11-19 15:38:05 -07:00
|
|
|
"fmt";
|
|
|
|
"flag";
|
2008-11-18 18:52:05 -07:00
|
|
|
)
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// Report as tests are run; default is silent for success.
|
2009-01-09 14:42:46 -07:00
|
|
|
var chatty = flag.Bool("chatty", false, "chatty")
|
2008-11-18 18:52:05 -07:00
|
|
|
|
2008-11-20 19:10:46 -07:00
|
|
|
// Insert tabs after newlines - but not the last one
|
2009-01-16 11:34:21 -07:00
|
|
|
func tabify(s string) string {
|
2008-11-20 19:10:46 -07:00
|
|
|
for i := 0; i < len(s) - 1; i++ { // -1 because if last char is newline, don't bother
|
|
|
|
if s[i] == '\n' {
|
2009-01-16 11:34:21 -07:00
|
|
|
return s[0:i+1] + "\t" + tabify(s[i+1:len(s)]);
|
2008-11-20 19:10:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// T is a type passed to Test functions to manage test state and support formatted test logs.
|
|
|
|
// Logs are accumulated during execution and dumped to standard error when done.
|
2009-01-20 15:40:40 -07:00
|
|
|
type T struct {
|
2008-11-19 15:38:05 -07:00
|
|
|
errors string;
|
|
|
|
failed bool;
|
2008-12-19 04:05:37 -07:00
|
|
|
ch chan *T;
|
2008-11-19 15:38:05 -07:00
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// Fail marks the Test function as having failed but continues execution.
|
2008-11-19 15:38:05 -07:00
|
|
|
func (t *T) Fail() {
|
|
|
|
t.failed = true
|
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// FailNow marks the Test function as having failed and stops its execution.
|
|
|
|
// Execution will continue at the next Test.
|
2008-11-19 15:38:05 -07:00
|
|
|
func (t *T) FailNow() {
|
|
|
|
t.Fail();
|
|
|
|
t.ch <- t;
|
2009-01-16 15:58:14 -07:00
|
|
|
sys.Goexit();
|
2008-11-19 15:38:05 -07:00
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// Log formats its arguments using default formatting, analogous to Print(),
|
|
|
|
// and records the text in the error log.
|
2008-11-19 15:38:05 -07:00
|
|
|
func (t *T) Log(args ...) {
|
2009-01-16 11:34:21 -07:00
|
|
|
t.errors += "\t" + tabify(fmt.Sprintln(args));
|
2008-11-19 15:38:05 -07:00
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// Log formats its arguments according to the format, analogous to Printf(),
|
|
|
|
// and records the text in the error log.
|
2008-11-19 15:38:05 -07:00
|
|
|
func (t *T) Logf(format string, args ...) {
|
2009-01-16 11:34:21 -07:00
|
|
|
t.errors += tabify(fmt.Sprintf("\t" + format, args));
|
2008-11-19 15:38:05 -07:00
|
|
|
l := len(t.errors);
|
|
|
|
if l > 0 && t.errors[l-1] != '\n' {
|
|
|
|
t.errors += "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// Error is equivalent to Log() followed by Fail().
|
2008-11-19 15:38:05 -07:00
|
|
|
func (t *T) Error(args ...) {
|
|
|
|
t.Log(args);
|
|
|
|
t.Fail();
|
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// Errorf is equivalent to Logf() followed by Fail().
|
2008-11-19 15:38:05 -07:00
|
|
|
func (t *T) Errorf(format string, args ...) {
|
|
|
|
t.Logf(format, args);
|
|
|
|
t.Fail();
|
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// Fatal is equivalent to Log() followed by FailNow().
|
2008-11-19 15:38:05 -07:00
|
|
|
func (t *T) Fatal(args ...) {
|
|
|
|
t.Log(args);
|
|
|
|
t.FailNow();
|
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// Fatalf is equivalent to Logf() followed by FailNow().
|
2008-11-19 15:38:05 -07:00
|
|
|
func (t *T) Fatalf(format string, args ...) {
|
|
|
|
t.Logf(format, args);
|
|
|
|
t.FailNow();
|
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// An internal type but exported because it is cross-package; part of the implementation
|
|
|
|
// of gotest.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Test struct {
|
2009-01-16 16:28:33 -07:00
|
|
|
Name string;
|
2009-01-30 15:39:31 -07:00
|
|
|
F func(*T);
|
2008-11-19 15:38:05 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:21 -07:00
|
|
|
func tRunner(t *T, test *Test) {
|
2009-01-16 16:28:33 -07:00
|
|
|
test.F(t);
|
2008-11-19 15:38:05 -07:00
|
|
|
t.ch <- t;
|
2008-11-18 16:29:10 -07:00
|
|
|
}
|
|
|
|
|
2009-03-05 18:50:36 -07:00
|
|
|
// An internal function but exported because it is cross-package; part of the implementation
|
|
|
|
// of gotest.
|
2009-01-20 15:40:40 -07:00
|
|
|
func Main(tests []Test) {
|
2008-11-21 17:13:31 -07:00
|
|
|
flag.Parse();
|
2008-11-18 16:29:10 -07:00
|
|
|
ok := true;
|
2008-11-19 20:11:01 -07:00
|
|
|
if len(tests) == 0 {
|
2008-11-25 13:49:17 -07:00
|
|
|
println("testing: warning: no tests to run");
|
2008-11-19 20:11:01 -07:00
|
|
|
}
|
2008-11-18 16:29:10 -07:00
|
|
|
for i := 0; i < len(tests); i++ {
|
2009-01-09 14:42:46 -07:00
|
|
|
if *chatty {
|
2009-01-16 16:28:33 -07:00
|
|
|
println("=== RUN ", tests[i].Name);
|
2008-11-18 18:52:05 -07:00
|
|
|
}
|
2009-01-06 16:19:02 -07:00
|
|
|
t := new(T);
|
|
|
|
t.ch = make(chan *T);
|
2009-01-16 11:34:21 -07:00
|
|
|
go tRunner(t, &tests[i]);
|
2008-11-19 15:38:05 -07:00
|
|
|
<-t.ch;
|
|
|
|
if t.failed {
|
2009-01-16 16:28:33 -07:00
|
|
|
println("--- FAIL:", tests[i].Name);
|
2008-11-19 15:38:05 -07:00
|
|
|
print(t.errors);
|
2008-11-18 18:52:05 -07:00
|
|
|
ok = false;
|
2009-01-09 14:42:46 -07:00
|
|
|
} else if *chatty {
|
2009-01-16 16:28:33 -07:00
|
|
|
println("--- PASS:", tests[i].Name);
|
2008-11-19 15:38:05 -07:00
|
|
|
print(t.errors);
|
2008-11-18 16:29:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
2008-11-19 15:38:05 -07:00
|
|
|
println("FAIL");
|
2009-01-16 15:58:14 -07:00
|
|
|
sys.Exit(1);
|
2008-11-18 16:29:10 -07:00
|
|
|
}
|
2008-11-18 18:52:05 -07:00
|
|
|
println("PASS");
|
2008-11-18 16:29:10 -07:00
|
|
|
}
|