testutil: update to go1.9 testing.Helper

This commit is contained in:
Alberto Cortés 2017-10-16 17:27:52 +02:00
parent 8f6a9f7833
commit 29da2fb9cd
3 changed files with 16 additions and 20 deletions

View file

@ -63,7 +63,7 @@ func TestComputeExternalURL(t *testing.T) {
if test.valid { if test.valid {
testutil.Ok(t, err) testutil.Ok(t, err)
} else { } else {
testutil.NotOk(t, err, "input=%q: ", test.input) testutil.NotOk(t, err, "input=%q", test.input)
} }
} }
} }

View file

@ -673,7 +673,7 @@ var expectedErrors = []struct {
func TestBadConfigs(t *testing.T) { func TestBadConfigs(t *testing.T) {
for _, ee := range expectedErrors { for _, ee := range expectedErrors {
_, err := LoadFile("testdata/" + ee.filename) _, err := LoadFile("testdata/" + ee.filename)
testutil.NotOk(t, err, "%s: ", ee.filename) testutil.NotOk(t, err, "%s", ee.filename)
testutil.Assert(t, strings.Contains(err.Error(), ee.errMsg), testutil.Assert(t, strings.Contains(err.Error(), ee.errMsg),
"Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err) "Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err)
} }
@ -684,7 +684,7 @@ func TestBadStaticConfigs(t *testing.T) {
testutil.Ok(t, err) testutil.Ok(t, err)
var tg TargetGroup var tg TargetGroup
err = json.Unmarshal(content, &tg) err = json.Unmarshal(content, &tg)
testutil.NotOk(t, err, "", nil) testutil.NotOk(t, err, "")
} }
func TestEmptyConfig(t *testing.T) { func TestEmptyConfig(t *testing.T) {

View file

@ -23,45 +23,41 @@
package testutil package testutil
import ( import (
"fmt"
"path/filepath"
"reflect" "reflect"
"runtime"
"testing" "testing"
) )
// Assert fails the test if the condition is false. // Assert fails the test if the condition is false.
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) { func Assert(tb testing.TB, condition bool, format string, a ...interface{}) {
tb.Helper()
if !condition { if !condition {
_, file, line, _ := runtime.Caller(1) tb.Fatalf("\033[31m"+format+"\033[39m\n", a...)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
} }
} }
// Ok fails the test if an err is not nil. // Ok fails the test if an err is not nil.
func Ok(tb testing.TB, err error) { func Ok(tb testing.TB, err error) {
tb.Helper()
if err != nil { if err != nil {
_, file, line, _ := runtime.Caller(1) tb.Fatalf("\033[31munexpected error: %v\033[39m\n", err)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
} }
} }
// NotOk fails the test if an err is nil. // NotOk fails the test if an err is nil.
func NotOk(tb testing.TB, err error, msg string, v ...interface{}) { func NotOk(tb testing.TB, err error, format string, a ...interface{}) {
tb.Helper()
if err == nil { if err == nil {
_, file, line, _ := runtime.Caller(1) if len(a) != 0 {
fmt.Printf("\033[31m%s:%d: "+msg+"expected error, got nothing\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...) tb.Fatalf("\033[31m"+format+": expected error, got none\033[39m", a...)
tb.FailNow() }
tb.Fatalf("\033[31mexpected error, got none\033[39m")
} }
} }
// Equals fails the test if exp is not equal to act. // Equals fails the test if exp is not equal to act.
func Equals(tb testing.TB, exp, act interface{}) { func Equals(tb testing.TB, exp, act interface{}) {
tb.Helper()
if !reflect.DeepEqual(exp, act) { if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1) tb.Fatalf("\033[31m\nexp: %#v\n\ngot: %#v\033[39m\n", exp, act)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
} }
} }