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