2017-10-20 03:12:49 -07:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
|
|
|
// Copyright (c) 2014 Ben Johnson
|
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
package testutil
|
2017-10-20 03:12:49 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Assert fails the test if the condition is false.
|
|
|
|
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
|
|
|
|
if !condition {
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
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.
|
|
|
|
func Ok(tb testing.TB, err error) {
|
|
|
|
if err != nil {
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
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.
|
|
|
|
func NotOk(tb testing.TB, err error) {
|
|
|
|
if err == nil {
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
fmt.Printf("\033[31m%s:%d: expected error, got nothing \033[39m\n\n", filepath.Base(file), line)
|
|
|
|
tb.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals fails the test if exp is not equal to act.
|
2018-10-03 01:08:31 -07:00
|
|
|
func Equals(tb testing.TB, exp, act interface{}, msgAndArgs ...interface{}) {
|
2017-10-20 03:12:49 -07:00
|
|
|
if !reflect.DeepEqual(exp, act) {
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
2018-10-03 01:08:31 -07:00
|
|
|
fmt.Printf("\033[31m%s:%d:%s\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, formatMessage(msgAndArgs), exp, act)
|
2017-10-20 03:12:49 -07:00
|
|
|
tb.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 01:08:31 -07:00
|
|
|
|
2019-01-08 09:08:41 -08:00
|
|
|
// NotEquals fails the test if exp is equal to act.
|
|
|
|
func NotEquals(tb testing.TB, exp, act interface{}) {
|
|
|
|
if reflect.DeepEqual(exp, act) {
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
fmt.Printf("\033[31m%s:%d: Expected different exp and got\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
|
|
|
|
tb.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 01:08:31 -07:00
|
|
|
func formatMessage(msgAndArgs []interface{}) string {
|
|
|
|
if len(msgAndArgs) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg, ok := msgAndArgs[0].(string); ok {
|
|
|
|
return fmt.Sprintf("\n\nmsg: "+msg, msgAndArgs[1:]...)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|