test: let TLog take a testing.B too. add tests.

Change-Id: I25c47960ef63be85994a133da85072005ca47a85
This commit is contained in:
Brad Fitzpatrick 2013-12-03 21:12:06 -08:00
parent cad54aa249
commit 4c8d79e170
2 changed files with 53 additions and 2 deletions

View File

@ -34,9 +34,27 @@ func BrokenTest(t *testing.T) {
} }
} }
// TB is a copy of Go 1.2's testing.TB.
type TB interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fail()
FailNow()
Failed() bool
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Log(args ...interface{})
Logf(format string, args ...interface{})
Skip(args ...interface{})
SkipNow()
Skipf(format string, args ...interface{})
Skipped() bool
}
// TLog changes the log package's output to log to t and returns a function // TLog changes the log package's output to log to t and returns a function
// to reset it back to stderr. // to reset it back to stderr.
func TLog(t *testing.T) func() { func TLog(t TB) func() {
// TODO(bradfitz): once we rely on Go 1.2, change this to take a testing.TB.
log.SetOutput(&twriter{t: t}) log.SetOutput(&twriter{t: t})
return func() { return func() {
log.SetOutput(os.Stderr) log.SetOutput(os.Stderr)
@ -44,7 +62,7 @@ func TLog(t *testing.T) func() {
} }
type twriter struct { type twriter struct {
t *testing.T t TB
buf bytes.Buffer buf bytes.Buffer
} }

View File

@ -17,8 +17,41 @@ limitations under the License.
package test_test package test_test
import ( import (
"log"
"reflect"
"testing"
"camlistore.org/pkg/index" "camlistore.org/pkg/index"
. "camlistore.org/pkg/test" . "camlistore.org/pkg/test"
) )
var _ index.Interface = (*FakeIndex)(nil) var _ index.Interface = (*FakeIndex)(nil)
type tbLogger struct {
TB
log []string
}
func (l *tbLogger) Log(args ...interface{}) {
l.log = append(l.log, args[0].(string))
}
func TestTLog(t *testing.T) {
tb := new(tbLogger)
defer TLog(tb)()
defer log.SetFlags(log.Flags())
log.SetFlags(0)
log.Printf("hello")
log.Printf("hello\n")
log.Printf("some text\nand more text\n")
want := []string{
"hello",
"hello",
"some text",
"and more text",
}
if !reflect.DeepEqual(tb.log, want) {
t.Errorf("Got %q; want %q", tb.log, want)
}
}