panic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package assertions
  2. import "fmt"
  3. // ShouldPanic receives a void, niladic function and expects to recover a panic.
  4. func ShouldPanic(actual interface{}, expected ...interface{}) (message string) {
  5. if fail := need(0, expected); fail != success {
  6. return fail
  7. }
  8. action, _ := actual.(func())
  9. if action == nil {
  10. message = shouldUseVoidNiladicFunction
  11. return
  12. }
  13. defer func() {
  14. recovered := recover()
  15. if recovered == nil {
  16. message = shouldHavePanicked
  17. } else {
  18. message = success
  19. }
  20. }()
  21. action()
  22. return
  23. }
  24. // ShouldNotPanic receives a void, niladic function and expects to execute the function without any panic.
  25. func ShouldNotPanic(actual interface{}, expected ...interface{}) (message string) {
  26. if fail := need(0, expected); fail != success {
  27. return fail
  28. }
  29. action, _ := actual.(func())
  30. if action == nil {
  31. message = shouldUseVoidNiladicFunction
  32. return
  33. }
  34. defer func() {
  35. recovered := recover()
  36. if recovered != nil {
  37. message = fmt.Sprintf(shouldNotHavePanicked, recovered)
  38. } else {
  39. message = success
  40. }
  41. }()
  42. action()
  43. return
  44. }
  45. // ShouldPanicWith receives a void, niladic function and expects to recover a panic with the second argument as the content.
  46. func ShouldPanicWith(actual interface{}, expected ...interface{}) (message string) {
  47. if fail := need(1, expected); fail != success {
  48. return fail
  49. }
  50. action, _ := actual.(func())
  51. if action == nil {
  52. message = shouldUseVoidNiladicFunction
  53. return
  54. }
  55. defer func() {
  56. recovered := recover()
  57. if recovered == nil {
  58. message = shouldHavePanicked
  59. } else {
  60. if equal := ShouldEqual(recovered, expected[0]); equal != success {
  61. message = serializer.serialize(expected[0], recovered, fmt.Sprintf(shouldHavePanickedWith, expected[0], recovered))
  62. } else {
  63. message = success
  64. }
  65. }
  66. }()
  67. action()
  68. return
  69. }
  70. // ShouldNotPanicWith receives a void, niladic function and expects to recover a panic whose content differs from the second argument.
  71. func ShouldNotPanicWith(actual interface{}, expected ...interface{}) (message string) {
  72. if fail := need(1, expected); fail != success {
  73. return fail
  74. }
  75. action, _ := actual.(func())
  76. if action == nil {
  77. message = shouldUseVoidNiladicFunction
  78. return
  79. }
  80. defer func() {
  81. recovered := recover()
  82. if recovered == nil {
  83. message = success
  84. } else {
  85. if equal := ShouldEqual(recovered, expected[0]); equal == success {
  86. message = fmt.Sprintf(shouldNotHavePanickedWith, expected[0])
  87. } else {
  88. message = success
  89. }
  90. }
  91. }()
  92. action()
  93. return
  94. }