panics.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2011 Aaron Jacobs. All Rights Reserved.
  2. // Author: aaronjjacobs@gmail.com (Aaron Jacobs)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package oglematchers
  16. import (
  17. "errors"
  18. "fmt"
  19. "reflect"
  20. )
  21. // Panics matches zero-arg functions which, when invoked, panic with an error
  22. // that matches the supplied matcher.
  23. //
  24. // NOTE(jacobsa): This matcher cannot detect the case where the function panics
  25. // using panic(nil), by design of the language. See here for more info:
  26. //
  27. // http://goo.gl/9aIQL
  28. //
  29. func Panics(m Matcher) Matcher {
  30. return &panicsMatcher{m}
  31. }
  32. type panicsMatcher struct {
  33. wrappedMatcher Matcher
  34. }
  35. func (m *panicsMatcher) Description() string {
  36. return "panics with: " + m.wrappedMatcher.Description()
  37. }
  38. func (m *panicsMatcher) Matches(c interface{}) (err error) {
  39. // Make sure c is a zero-arg function.
  40. v := reflect.ValueOf(c)
  41. if v.Kind() != reflect.Func || v.Type().NumIn() != 0 {
  42. err = NewFatalError("which is not a zero-arg function")
  43. return
  44. }
  45. // Call the function and check its panic error.
  46. defer func() {
  47. if e := recover(); e != nil {
  48. err = m.wrappedMatcher.Matches(e)
  49. // Set a clearer error message if the matcher said no.
  50. if err != nil {
  51. wrappedClause := ""
  52. if err.Error() != "" {
  53. wrappedClause = ", " + err.Error()
  54. }
  55. err = errors.New(fmt.Sprintf("which panicked with: %v%s", e, wrappedClause))
  56. }
  57. }
  58. }()
  59. v.Call([]reflect.Value{})
  60. // If we get here, the function didn't panic.
  61. err = errors.New("which didn't panic")
  62. return
  63. }