assert.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package testutil
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. )
  20. func AssertEqual(t *testing.T, e, a interface{}, msg ...string) {
  21. if (e == nil || a == nil) && (isNil(e) && isNil(a)) {
  22. return
  23. }
  24. if reflect.DeepEqual(e, a) {
  25. return
  26. }
  27. s := ""
  28. if len(msg) > 1 {
  29. s = msg[0] + ": "
  30. }
  31. s = fmt.Sprintf("%sexpected %+v, got %+v", s, e, a)
  32. FatalStack(t, s)
  33. }
  34. func AssertNil(t *testing.T, v interface{}) {
  35. AssertEqual(t, nil, v)
  36. }
  37. func AssertNotNil(t *testing.T, v interface{}) {
  38. if v == nil {
  39. t.Fatalf("expected non-nil, got %+v", v)
  40. }
  41. }
  42. func AssertTrue(t *testing.T, v bool, msg ...string) {
  43. AssertEqual(t, true, v, msg...)
  44. }
  45. func AssertFalse(t *testing.T, v bool, msg ...string) {
  46. AssertEqual(t, false, v, msg...)
  47. }
  48. func isNil(v interface{}) bool {
  49. if v == nil {
  50. return true
  51. }
  52. rv := reflect.ValueOf(v)
  53. return rv.Kind() != reflect.Struct && rv.IsNil()
  54. }