| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or more contributor license agreements. See the NOTICE file |
| 4 |
distributed with this work for additional information |
| 5 |
regarding copyright ownership. The ASF licenses this file |
| 6 |
to you under the Apache License, Version 2.0 (the |
| 7 |
"License"); you may not use this file except in compliance |
| 8 |
with the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, |
| 13 |
software distributed under the License is distributed on an |
| 14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
KIND, either express or implied. See the License for the |
| 16 |
specific language governing permissions and limitations |
| 17 |
under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
// Utilities to supplement the standard Go testing package |
| 21 |
package test |
| 22 |
|
| 23 |
import ( |
| 24 |
"fmt" |
| 25 |
"path" |
| 26 |
"reflect" |
| 27 |
"runtime" |
| 28 |
"testing" |
| 29 |
) |
| 30 |
|
| 31 |
// The testing.TB.Helper() is only available from Go 1.9. |
| 32 |
// Decorate messages with the correct file:line at callDepth before calling |
| 33 |
// before calling a testing.TB method. |
| 34 |
func decorate(method func(arg ...interface{}), callDepth int, msg string) { |
| 35 |
_, file, line, _ := runtime.Caller(callDepth + 1) |
| 36 |
_, file = path.Split(file) |
| 37 |
method(fmt.Sprintf("\n%s:%d: %v", file, line, msg)) |
| 38 |
} |
| 39 |
|
| 40 |
// Format a message and call method if err != nil |
| 41 |
func checkErr(method func(arg ...interface{}), callDepth int, err error, format ...interface{}) error { |
| 42 |
if err != nil { |
| 43 |
var msg string |
| 44 |
if len(format) > 0 { |
| 45 |
msg = fmt.Sprintf("%v: %v", fmt.Sprintf(format[0].(string), format[1:]...), err) |
| 46 |
} else { |
| 47 |
msg = err.Error() |
| 48 |
} |
| 49 |
decorate(method, callDepth+1, msg) |
| 50 |
} |
| 51 |
return err |
| 52 |
} |
| 53 |
|
| 54 |
func ErrorIfN(callDepth int, t testing.TB, err error, format ...interface{}) error { |
| 55 |
return checkErr(t.Error, callDepth+1, err, format...) |
| 56 |
} |
| 57 |
|
| 58 |
func ErrorIf(t testing.TB, err error, format ...interface{}) error { |
| 59 |
return ErrorIfN(1, t, err, format...) |
| 60 |
} |
| 61 |
|
| 62 |
func FatalIfN(callDepth int, t testing.TB, err error, format ...interface{}) { |
| 63 |
checkErr(t.Fatal, callDepth+1, err, format...) |
| 64 |
} |
| 65 |
|
| 66 |
func FatalIf(t testing.TB, err error, format ...interface{}) { |
| 67 |
FatalIfN(1, t, err, format...) |
| 68 |
} |
| 69 |
|
| 70 |
// if reflect.DeepEqual(want, got) { return nil } else { return error("want != got" }) |
| 71 |
func Differ(want interface{}, got interface{}) error { |
| 72 |
if !reflect.DeepEqual(want, got) { |
| 73 |
return fmt.Errorf("(%T)%#v != (%T)%#v)", want, want, got, got) |
| 74 |
} |
| 75 |
return nil |
| 76 |
} |