testing: gather duplicate code into tRunner
No change in behavior, just preparing for next commit, and gently nudging code closer to upstream.
Этот коммит содержится в:
родитель
798085e866
коммит
0b939f93bc
1 изменённых файлов: 32 добавлений и 33 удалений
|
@ -12,6 +12,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -43,6 +44,7 @@ func Init() {
|
||||||
// captures common methods such as Errorf.
|
// captures common methods such as Errorf.
|
||||||
type common struct {
|
type common struct {
|
||||||
output bytes.Buffer
|
output bytes.Buffer
|
||||||
|
w io.Writer // either &output, or at top level, os.Stdout
|
||||||
indent string
|
indent string
|
||||||
|
|
||||||
failed bool // Test or benchmark has failed.
|
failed bool // Test or benchmark has failed.
|
||||||
|
@ -50,6 +52,7 @@ type common struct {
|
||||||
finished bool // Test function has completed.
|
finished bool // Test function has completed.
|
||||||
level int // Nesting depth of test or benchmark.
|
level int // Nesting depth of test or benchmark.
|
||||||
name string // Name of test or benchmark.
|
name string // Name of test or benchmark.
|
||||||
|
parent *common
|
||||||
}
|
}
|
||||||
|
|
||||||
// TB is the interface common to T and B.
|
// TB is the interface common to T and B.
|
||||||
|
@ -205,7 +208,30 @@ func (c *common) Parallel() {
|
||||||
// Unimplemented.
|
// Unimplemented.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run runs a subtest of f t called name. It waits until the subtest is finished
|
func tRunner(t *T, fn func(t *T)) {
|
||||||
|
// Run the test.
|
||||||
|
if flagVerbose {
|
||||||
|
fmt.Fprintf(t.w, "=== RUN %s\n", t.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn(t)
|
||||||
|
|
||||||
|
// Process the result (pass or fail).
|
||||||
|
if t.failed {
|
||||||
|
if t.parent != nil {
|
||||||
|
t.parent.failed = true
|
||||||
|
}
|
||||||
|
fmt.Fprintf(t.w, t.indent+"--- FAIL: %s\n", t.name)
|
||||||
|
t.w.Write(t.output.Bytes())
|
||||||
|
} else {
|
||||||
|
if flagVerbose {
|
||||||
|
fmt.Fprintf(t.w, t.indent+"--- PASS: %s\n", t.name)
|
||||||
|
t.w.Write(t.output.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run runs f as a subtest of t called name. It waits until the subtest is finished
|
||||||
// and returns whether the subtest succeeded.
|
// and returns whether the subtest succeeded.
|
||||||
func (t *T) Run(name string, f func(t *T)) bool {
|
func (t *T) Run(name string, f func(t *T)) bool {
|
||||||
// Create a subtest.
|
// Create a subtest.
|
||||||
|
@ -213,27 +239,12 @@ func (t *T) Run(name string, f func(t *T)) bool {
|
||||||
common: common{
|
common: common{
|
||||||
name: t.name + "/" + rewrite(name),
|
name: t.name + "/" + rewrite(name),
|
||||||
indent: t.indent + " ",
|
indent: t.indent + " ",
|
||||||
|
w: &t.output,
|
||||||
|
parent: &t.common,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the test.
|
tRunner(&sub, f)
|
||||||
if flagVerbose {
|
|
||||||
fmt.Fprintf(&t.output, "=== RUN %s\n", sub.name)
|
|
||||||
|
|
||||||
}
|
|
||||||
f(&sub)
|
|
||||||
|
|
||||||
// Process the result (pass or fail).
|
|
||||||
if sub.failed {
|
|
||||||
t.failed = true
|
|
||||||
fmt.Fprintf(&t.output, sub.indent+"--- FAIL: %s\n", sub.name)
|
|
||||||
t.output.Write(sub.output.Bytes())
|
|
||||||
} else {
|
|
||||||
if flagVerbose {
|
|
||||||
fmt.Fprintf(&t.output, sub.indent+"--- PASS: %s\n", sub.name)
|
|
||||||
t.output.Write(sub.output.Bytes())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return !sub.failed
|
return !sub.failed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,23 +324,11 @@ func (m *M) Run() int {
|
||||||
t := &T{
|
t := &T{
|
||||||
common: common{
|
common: common{
|
||||||
name: test.Name,
|
name: test.Name,
|
||||||
|
w: os.Stdout,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if flagVerbose {
|
tRunner(t, test.F)
|
||||||
fmt.Printf("=== RUN %s\n", test.Name)
|
|
||||||
}
|
|
||||||
test.F(t)
|
|
||||||
|
|
||||||
if t.failed {
|
|
||||||
fmt.Printf("--- FAIL: %s\n", test.Name)
|
|
||||||
os.Stdout.Write(t.output.Bytes())
|
|
||||||
} else {
|
|
||||||
if flagVerbose {
|
|
||||||
fmt.Printf("--- PASS: %s\n", test.Name)
|
|
||||||
os.Stdout.Write(t.output.Bytes())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if t.failed {
|
if t.failed {
|
||||||
failures++
|
failures++
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче