expect.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. func ExpectContainSubstring(actual, substr string, explain ...interface{}) {
  37. gomega.ExpectWithOffset(1, actual).To(gomega.ContainSubstring(substr), explain...)
  38. }
  39. // ExpectConsistOf expects actual contains precisely the extra elements. The ordering of the elements does not matter.
  40. func ExpectConsistOf(actual interface{}, extra interface{}, explain ...interface{}) {
  41. gomega.ExpectWithOffset(1, actual).To(gomega.ConsistOf(extra), explain...)
  42. }
  43. func ExpectContainElements(actual interface{}, extra interface{}, explain ...interface{}) {
  44. gomega.ExpectWithOffset(1, actual).To(gomega.ContainElements(extra), explain...)
  45. }
  46. func ExpectNotContainElements(actual interface{}, extra interface{}, explain ...interface{}) {
  47. gomega.ExpectWithOffset(1, actual).NotTo(gomega.ContainElements(extra), explain...)
  48. }
  49. // ExpectHaveKey expects the actual map has the key in the keyset
  50. func ExpectHaveKey(actual interface{}, key interface{}, explain ...interface{}) {
  51. gomega.ExpectWithOffset(1, actual).To(gomega.HaveKey(key), explain...)
  52. }
  53. // ExpectEmpty expects actual is empty
  54. func ExpectEmpty(actual interface{}, explain ...interface{}) {
  55. gomega.ExpectWithOffset(1, actual).To(gomega.BeEmpty(), explain...)
  56. }
  57. func ExpectTrue(actual interface{}, explain ...interface{}) {
  58. gomega.ExpectWithOffset(1, actual).Should(gomega.BeTrue(), explain...)
  59. }
  60. func ExpectTrueWithOffset(offset int, actual interface{}, explain ...interface{}) {
  61. gomega.ExpectWithOffset(1+offset, actual).Should(gomega.BeTrue(), explain...)
  62. }