util.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package testutil
  2. // Copied from https://github.com/benbjohnson/testing
  3. import (
  4. "fmt"
  5. "path/filepath"
  6. "reflect"
  7. "runtime"
  8. "testing"
  9. )
  10. // Assert fails the test if the condition is false.
  11. func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
  12. if !condition {
  13. _, file, line, _ := runtime.Caller(1)
  14. fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
  15. tb.FailNow()
  16. }
  17. }
  18. // Ok fails the test if an err is not nil.
  19. func Ok(tb testing.TB, err error) {
  20. if err != nil {
  21. _, file, line, _ := runtime.Caller(1)
  22. fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
  23. tb.FailNow()
  24. }
  25. }
  26. // Equals fails the test if exp is not equal to act.
  27. func Equals(tb testing.TB, exp, act interface{}) {
  28. if !reflect.DeepEqual(exp, act) {
  29. _, file, line, _ := runtime.Caller(1)
  30. fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
  31. tb.FailNow()
  32. }
  33. }