expect.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package framework
  2. import (
  3. "github.com/onsi/gomega"
  4. )
  5. // ExpectEqual expects the specified two are the same, otherwise an exception raises
  6. func ExpectEqual(actual interface{}, extra interface{}, explain ...interface{}) {
  7. gomega.ExpectWithOffset(1, actual).To(gomega.Equal(extra), explain...)
  8. }
  9. // ExpectEqualValues expects the specified two are the same, it not strict about type
  10. func ExpectEqualValues(actual interface{}, extra interface{}, explain ...interface{}) {
  11. gomega.ExpectWithOffset(1, actual).To(gomega.BeEquivalentTo(extra), explain...)
  12. }
  13. // ExpectNotEqual expects the specified two are not the same, otherwise an exception raises
  14. func ExpectNotEqual(actual interface{}, extra interface{}, explain ...interface{}) {
  15. gomega.ExpectWithOffset(1, actual).NotTo(gomega.Equal(extra), explain...)
  16. }
  17. // ExpectError expects an error happens, otherwise an exception raises
  18. func ExpectError(err error, explain ...interface{}) {
  19. gomega.ExpectWithOffset(1, err).To(gomega.HaveOccurred(), explain...)
  20. }
  21. // ExpectNoError checks if "err" is set, and if so, fails assertion while logging the error.
  22. func ExpectNoError(err error, explain ...interface{}) {
  23. ExpectNoErrorWithOffset(1, err, explain...)
  24. }
  25. // ExpectNoErrorWithOffset checks if "err" is set, and if so, fails assertion while logging the error at "offset" levels above its caller
  26. // (for example, for call chain f -> g -> ExpectNoErrorWithOffset(1, ...) error would be logged for "f").
  27. func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) {
  28. gomega.ExpectWithOffset(1+offset, err).NotTo(gomega.HaveOccurred(), explain...)
  29. }
  30. // ExpectConsistOf expects actual contains precisely the extra elements. The ordering of the elements does not matter.
  31. func ExpectConsistOf(actual interface{}, extra interface{}, explain ...interface{}) {
  32. gomega.ExpectWithOffset(1, actual).To(gomega.ConsistOf(extra), explain...)
  33. }
  34. // ExpectHaveKey expects the actual map has the key in the keyset
  35. func ExpectHaveKey(actual interface{}, key interface{}, explain ...interface{}) {
  36. gomega.ExpectWithOffset(1, actual).To(gomega.HaveKey(key), explain...)
  37. }
  38. // ExpectEmpty expects actual is empty
  39. func ExpectEmpty(actual interface{}, explain ...interface{}) {
  40. gomega.ExpectWithOffset(1, actual).To(gomega.BeEmpty(), explain...)
  41. }
  42. func ExpectTrue(actual interface{}, explain ...interface{}) {
  43. gomega.ExpectWithOffset(1, actual).Should(gomega.BeTrue(), explain...)
  44. }