mirror of
https://github.com/golang/go
synced 2024-11-20 04:34:41 -07:00
testing: add file:line stamps to messages.
A single-line error looks like this: --- FAIL: foo_test.TestFoo (0.00 seconds) foo_test.go:123: Foo(8) = "10" want "100" A multi-line error looks like this: --- FAIL: foo_test.TestFoo (0.00 seconds) foo_test.go:456: Foo(88) = "100" want "1000" R=rsc, bradfitz CC=golang-dev https://golang.org/cl/5376057
This commit is contained in:
parent
85b7419211
commit
2c39ca08cd
@ -75,8 +75,25 @@ func Short() bool {
|
|||||||
return *short
|
return *short
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert final newline if needed and tabs after internal newlines.
|
// decorate inserts the a final newline if needed and indentation tabs for formatting.
|
||||||
func tabify(s string) string {
|
// If addFileLine is true, it also prefixes the string with the file and line of the call site.
|
||||||
|
func decorate(s string, addFileLine bool) string {
|
||||||
|
if addFileLine {
|
||||||
|
_, file, line, ok := runtime.Caller(3) // decorate + log + public function.
|
||||||
|
if ok {
|
||||||
|
// Truncate file name at last file name separator.
|
||||||
|
if index := strings.LastIndex(file, "/"); index >= 0 {
|
||||||
|
file = file[index+1:]
|
||||||
|
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
|
||||||
|
file = file[index+1:]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
file = "???"
|
||||||
|
line = 1
|
||||||
|
}
|
||||||
|
s = fmt.Sprintf("%s:%d: %s", file, line, s)
|
||||||
|
}
|
||||||
|
s = "\t" + s // Every line is indented at least one tab.
|
||||||
n := len(s)
|
n := len(s)
|
||||||
if n > 0 && s[n-1] != '\n' {
|
if n > 0 && s[n-1] != '\n' {
|
||||||
s += "\n"
|
s += "\n"
|
||||||
@ -84,7 +101,8 @@ func tabify(s string) string {
|
|||||||
}
|
}
|
||||||
for i := 0; i < n-1; i++ { // -1 to avoid final newline
|
for i := 0; i < n-1; i++ { // -1 to avoid final newline
|
||||||
if s[i] == '\n' {
|
if s[i] == '\n' {
|
||||||
return s[0:i+1] + "\t" + tabify(s[i+1:n])
|
// Second and subsequent lines are indented an extra tab.
|
||||||
|
return s[0:i+1] + "\t" + decorate(s[i+1:n], false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
@ -116,37 +134,38 @@ func (t *T) FailNow() {
|
|||||||
runtime.Goexit()
|
runtime.Goexit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// log generates the output. It's always at the same stack depth.
|
||||||
|
func (t *T) log(s string) { t.errors += decorate(s, true) }
|
||||||
|
|
||||||
// Log formats its arguments using default formatting, analogous to Print(),
|
// Log formats its arguments using default formatting, analogous to Print(),
|
||||||
// and records the text in the error log.
|
// and records the text in the error log.
|
||||||
func (t *T) Log(args ...interface{}) { t.errors += "\t" + tabify(fmt.Sprintln(args...)) }
|
func (t *T) Log(args ...interface{}) { t.log(fmt.Sprintln(args...)) }
|
||||||
|
|
||||||
// Logf formats its arguments according to the format, analogous to Printf(),
|
// Logf formats its arguments according to the format, analogous to Printf(),
|
||||||
// and records the text in the error log.
|
// and records the text in the error log.
|
||||||
func (t *T) Logf(format string, args ...interface{}) {
|
func (t *T) Logf(format string, args ...interface{}) { t.log(fmt.Sprintf(format, args...)) }
|
||||||
t.errors += "\t" + tabify(fmt.Sprintf(format, args...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error is equivalent to Log() followed by Fail().
|
// Error is equivalent to Log() followed by Fail().
|
||||||
func (t *T) Error(args ...interface{}) {
|
func (t *T) Error(args ...interface{}) {
|
||||||
t.Log(args...)
|
t.log(fmt.Sprintln(args...))
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorf is equivalent to Logf() followed by Fail().
|
// Errorf is equivalent to Logf() followed by Fail().
|
||||||
func (t *T) Errorf(format string, args ...interface{}) {
|
func (t *T) Errorf(format string, args ...interface{}) {
|
||||||
t.Logf(format, args...)
|
t.log(fmt.Sprintf(format, args...))
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal is equivalent to Log() followed by FailNow().
|
// Fatal is equivalent to Log() followed by FailNow().
|
||||||
func (t *T) Fatal(args ...interface{}) {
|
func (t *T) Fatal(args ...interface{}) {
|
||||||
t.Log(args...)
|
t.log(fmt.Sprintln(args...))
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatalf is equivalent to Logf() followed by FailNow().
|
// Fatalf is equivalent to Logf() followed by FailNow().
|
||||||
func (t *T) Fatalf(format string, args ...interface{}) {
|
func (t *T) Fatalf(format string, args ...interface{}) {
|
||||||
t.Logf(format, args...)
|
t.log(fmt.Sprintf(format, args...))
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user