expect.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package framework
  14. import (
  15. "github.com/onsi/gomega"
  16. )
  17. // ExpectEqual expects the specified two are the same, otherwise an exception raises
  18. func ExpectEqual(actual interface{}, extra interface{}, explain ...interface{}) {
  19. gomega.ExpectWithOffset(1, actual).To(gomega.Equal(extra), explain...)
  20. }
  21. // ExpectNotEqual expects the specified two are not the same, otherwise an exception raises
  22. func ExpectNotEqual(actual interface{}, extra interface{}, explain ...interface{}) {
  23. gomega.ExpectWithOffset(1, actual).NotTo(gomega.Equal(extra), explain...)
  24. }
  25. // ExpectError expects an error happens, otherwise an exception raises
  26. func ExpectError(err error, explain ...interface{}) {
  27. gomega.ExpectWithOffset(1, err).To(gomega.HaveOccurred(), explain...)
  28. }
  29. // ExpectNoError checks if "err" is set, and if so, fails assertion while logging the error.
  30. func ExpectNoError(err error, explain ...interface{}) {
  31. ExpectNoErrorWithOffset(1, err, explain...)
  32. }
  33. // ExpectNoErrorWithOffset checks if "err" is set, and if so, fails assertion while logging the error at "offset" levels above its caller
  34. // (for example, for call chain f -> g -> ExpectNoErrorWithOffset(1, ...) error would be logged for "f").
  35. func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) {
  36. gomega.ExpectWithOffset(1+offset, err).NotTo(gomega.HaveOccurred(), explain...)
  37. }
  38. // ExpectConsistOf expects actual contains precisely the extra elements. The ordering of the elements does not matter.
  39. func ExpectConsistOf(actual interface{}, extra interface{}, explain ...interface{}) {
  40. gomega.ExpectWithOffset(1, actual).To(gomega.ConsistOf(extra), explain...)
  41. }
  42. // ExpectHaveKey expects the actual map has the key in the keyset
  43. func ExpectHaveKey(actual interface{}, key interface{}, explain ...interface{}) {
  44. gomega.ExpectWithOffset(1, actual).To(gomega.HaveKey(key), explain...)
  45. }
  46. // ExpectEmpty expects actual is empty
  47. func ExpectEmpty(actual interface{}, explain ...interface{}) {
  48. gomega.ExpectWithOffset(1, actual).To(gomega.BeEmpty(), explain...)
  49. }