expect.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. func ExpectEqualValuesWithOffset(offset int, actual interface{}, extra interface{}, explain ...interface{}) {
  14. gomega.ExpectWithOffset(1+offset, actual).To(gomega.BeEquivalentTo(extra), explain...)
  15. }
  16. // ExpectNotEqual expects the specified two are not the same, otherwise an exception raises
  17. func ExpectNotEqual(actual interface{}, extra interface{}, explain ...interface{}) {
  18. gomega.ExpectWithOffset(1, actual).NotTo(gomega.Equal(extra), explain...)
  19. }
  20. // ExpectError expects an error happens, otherwise an exception raises
  21. func ExpectError(err error, explain ...interface{}) {
  22. gomega.ExpectWithOffset(1, err).To(gomega.HaveOccurred(), explain...)
  23. }
  24. func ExpectErrorWithOffset(offset int, err error, explain ...interface{}) {
  25. gomega.ExpectWithOffset(1+offset, err).To(gomega.HaveOccurred(), explain...)
  26. }
  27. // ExpectNoError checks if "err" is set, and if so, fails assertion while logging the error.
  28. func ExpectNoError(err error, explain ...interface{}) {
  29. ExpectNoErrorWithOffset(1, err, explain...)
  30. }
  31. // ExpectNoErrorWithOffset checks if "err" is set, and if so, fails assertion while logging the error at "offset" levels above its caller
  32. // (for example, for call chain f -> g -> ExpectNoErrorWithOffset(1, ...) error would be logged for "f").
  33. func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) {
  34. gomega.ExpectWithOffset(1+offset, err).NotTo(gomega.HaveOccurred(), explain...)
  35. }
  36. // ExpectConsistOf expects actual contains precisely the extra elements. The ordering of the elements does not matter.
  37. func ExpectConsistOf(actual interface{}, extra interface{}, explain ...interface{}) {
  38. gomega.ExpectWithOffset(1, actual).To(gomega.ConsistOf(extra), explain...)
  39. }
  40. func ExpectContainElements(actual interface{}, extra interface{}, explain ...interface{}) {
  41. gomega.ExpectWithOffset(1, actual).To(gomega.ContainElements(extra), explain...)
  42. }
  43. func ExpectNotContainElements(actual interface{}, extra interface{}, explain ...interface{}) {
  44. gomega.ExpectWithOffset(1, actual).NotTo(gomega.ContainElements(extra), explain...)
  45. }
  46. // ExpectHaveKey expects the actual map has the key in the keyset
  47. func ExpectHaveKey(actual interface{}, key interface{}, explain ...interface{}) {
  48. gomega.ExpectWithOffset(1, actual).To(gomega.HaveKey(key), explain...)
  49. }
  50. // ExpectEmpty expects actual is empty
  51. func ExpectEmpty(actual interface{}, explain ...interface{}) {
  52. gomega.ExpectWithOffset(1, actual).To(gomega.BeEmpty(), explain...)
  53. }
  54. func ExpectTrue(actual interface{}, explain ...interface{}) {
  55. gomega.ExpectWithOffset(1, actual).Should(gomega.BeTrue(), explain...)
  56. }
  57. func ExpectTrueWithOffset(offset int, actual interface{}, explain ...interface{}) {
  58. gomega.ExpectWithOffset(1+offset, actual).Should(gomega.BeTrue(), explain...)
  59. }