mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
add a type testing.T and use it in tests.
update uses of gotest. minor tweak to testing structure for protobuf. R=rsc DELTA=276 (71 added, 75 deleted, 130 changed) OCL=19614 CL=19621
This commit is contained in:
parent
165d78717d
commit
6d30efc772
7 changed files with 182 additions and 160 deletions
|
|
@ -5,7 +5,8 @@
|
|||
package testing
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt";
|
||||
"flag";
|
||||
)
|
||||
|
||||
var chatty bool;
|
||||
|
|
@ -13,32 +14,85 @@ func init() {
|
|||
flag.Bool("chatty", false, &chatty, "chatty");
|
||||
}
|
||||
|
||||
export type T struct {
|
||||
errors string;
|
||||
failed bool;
|
||||
ch *chan *T;
|
||||
}
|
||||
|
||||
func (t *T) Fail() {
|
||||
t.failed = true
|
||||
}
|
||||
|
||||
func (t *T) FailNow() {
|
||||
t.Fail();
|
||||
t.ch <- t;
|
||||
sys.goexit();
|
||||
}
|
||||
|
||||
func (t *T) Log(args ...) {
|
||||
t.errors += "\t" + fmt.sprintln(args);
|
||||
}
|
||||
|
||||
func (t *T) Logf(format string, args ...) {
|
||||
t.errors += fmt.sprintf("\t" + format, args);
|
||||
l := len(t.errors);
|
||||
if l > 0 && t.errors[l-1] != '\n' {
|
||||
t.errors += "\n"
|
||||
}
|
||||
}
|
||||
|
||||
func (t *T) Error(args ...) {
|
||||
t.Log(args);
|
||||
t.Fail();
|
||||
}
|
||||
|
||||
func (t *T) Errorf(format string, args ...) {
|
||||
t.Logf(format, args);
|
||||
t.Fail();
|
||||
}
|
||||
|
||||
func (t *T) Fatal(args ...) {
|
||||
t.Log(args);
|
||||
t.FailNow();
|
||||
}
|
||||
|
||||
func (t *T) Fatalf(format string, args ...) {
|
||||
t.Logf(format, args);
|
||||
t.FailNow();
|
||||
}
|
||||
|
||||
export type Test struct {
|
||||
name string;
|
||||
f *() bool;
|
||||
f *(*T);
|
||||
}
|
||||
|
||||
func TRunner(t *T, test *Test) {
|
||||
test.f(t);
|
||||
t.ch <- t;
|
||||
}
|
||||
|
||||
export func Main(tests *[]Test) {
|
||||
flag.Parse();
|
||||
ok := true;
|
||||
if len(tests) == 0 {
|
||||
println("warning: no tests available");
|
||||
} else if chatty {
|
||||
println(len(tests), "tests to run");
|
||||
}
|
||||
for i := 0; i < len(tests); i++ {
|
||||
if chatty {
|
||||
println("=== RUN ", tests[i].name);
|
||||
}
|
||||
ok1 := tests[i].f();
|
||||
if !ok1 {
|
||||
t := new(T);
|
||||
t.ch = new(chan *T);
|
||||
go TRunner(t, &tests[i]);
|
||||
<-t.ch;
|
||||
if t.failed {
|
||||
println("--- FAIL:", tests[i].name);
|
||||
print(t.errors);
|
||||
ok = false;
|
||||
println("--- FAIL", tests[i].name);
|
||||
} else if chatty {
|
||||
println("--- PASS", tests[i].name);
|
||||
println("--- PASS:", tests[i].name);
|
||||
print(t.errors);
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
println("FAIL");
|
||||
sys.exit(1);
|
||||
}
|
||||
println("PASS");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue