assert.go 896 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package expect // import "github.com/anacrolix/missinggo/expect"
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "reflect"
  6. )
  7. func Nil(x interface{}) {
  8. if x != nil {
  9. panic(fmt.Sprintf("expected nil; got %v", x))
  10. }
  11. }
  12. func NotNil(x interface{}) {
  13. if x == nil {
  14. panic(x)
  15. }
  16. }
  17. func Equal(x, y interface{}) {
  18. if x == y {
  19. return
  20. }
  21. yAsXType := reflect.ValueOf(y).Convert(reflect.TypeOf(x)).Interface()
  22. if !reflect.DeepEqual(x, yAsXType) {
  23. panic(fmt.Sprintf("%v != %v", x, y))
  24. }
  25. }
  26. func StrictlyEqual(x, y interface{}) {
  27. if x != y {
  28. panic(fmt.Sprintf("%s != %s", x, y))
  29. }
  30. }
  31. func OneRowAffected(r sql.Result) {
  32. count, err := r.RowsAffected()
  33. Nil(err)
  34. if count != 1 {
  35. panic(count)
  36. }
  37. }
  38. func True(b bool) {
  39. if !b {
  40. panic(b)
  41. }
  42. }
  43. var Ok = True
  44. func False(b bool) {
  45. if b {
  46. panic(b)
  47. }
  48. }
  49. func Zero(x interface{}) {
  50. if x != reflect.Zero(reflect.TypeOf(x)).Interface() {
  51. panic(x)
  52. }
  53. }