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
|
|
|
"flag";
|
2009-04-16 21:52:37 -06:00
|
|
|
"fmt";
|
2009-05-08 16:21:41 -06:00
|
|
|
"os";
|
|
|
|
"runtime";
|
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-06-04 16:40:28 -06:00
|
|
|
var chatty = flag.Bool("v", false, "verbose: print additional output")
|
|
|
|
var match = flag.String("match", "", "regular expression to select tests to run")
|
|
|
|
|
2008-11-18 18:52:05 -07:00
|
|
|
|
2009-05-19 12:00:55 -06:00
|
|
|
// Insert final newline if needed and tabs after internal newlines.
|
2009-01-16 11:34:21 -07:00
|
|
|
func tabify(s string) string {
|
2009-05-19 12:00:55 -06:00
|
|
|
n := len(s);
|
|
|
|
if n > 0 && s[n-1] != '\n' {
|
|
|
|
s += "\n";
|
|
|
|
n++;
|
|
|
|
}
|
2009-10-07 12:55:06 -06:00
|
|
|
for i := 0; i < n-1; i++ { // -1 to avoid final newline
|
2008-11-20 19:10:46 -07:00
|
|
|
if s[i] == '\n' {
|
2009-10-07 12:55:06 -06:00
|
|
|
return s[0 : i+1]+"\t"+tabify(s[i+1 : n]);
|
2008-11-20 19:10:46 -07:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 12:55:06 -06:00
|
|
|
return s;
|
2008-11-20 19:10:46 -07:00
|
|
|
}
|
|
|
|
|
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() {
|
2009-10-07 12:55:06 -06:00
|
|
|
t.failed = true;
|
2008-11-19 15:38:05 -07:00
|
|
|
}
|
|
|
|
|
2009-05-19 12:00:55 -06:00
|
|
|
// Failed returns whether the Test function has failed.
|
|
|
|
func (t *T) Failed() bool {
|
2009-10-07 12:55:06 -06:00
|
|
|
return t.failed;
|
2009-05-19 12:00:55 -06:00
|
|
|
}
|
|
|
|
|
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-05-08 16:21:41 -06:00
|
|
|
runtime.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-05-19 12:00:55 -06:00
|
|
|
t.errors += "\t" + tabify(fmt.Sprintf(format, args));
|
2008-11-19 15:38:05 -07:00
|
|
|
}
|
|
|
|
|
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-10-07 12:55:06 -06:00
|
|
|
Name string;
|
|
|
|
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
|
|
|
}
|
2009-08-11 14:54:47 -06:00
|
|
|
re, err := CompileRegexp(*match);
|
|
|
|
if err != "" {
|
|
|
|
println("invalid regexp for -match:", err);
|
2009-06-04 16:40:28 -06:00
|
|
|
os.Exit(1);
|
|
|
|
}
|
2008-11-18 16:29:10 -07:00
|
|
|
for i := 0; i < len(tests); i++ {
|
2009-08-05 16:44:45 -06:00
|
|
|
if !re.MatchString(tests[i].Name) {
|
2009-06-04 16:40:28 -06:00
|
|
|
continue;
|
|
|
|
}
|
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-05-08 16:21:41 -06:00
|
|
|
os.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
|
|
|
}
|