be_sent_matcher.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // untested sections: 3
  2. package matchers
  3. import (
  4. "fmt"
  5. "reflect"
  6. "github.com/onsi/gomega/format"
  7. )
  8. type BeSentMatcher struct {
  9. Arg interface{}
  10. channelClosed bool
  11. }
  12. func (matcher *BeSentMatcher) Match(actual interface{}) (success bool, err error) {
  13. if !isChan(actual) {
  14. return false, fmt.Errorf("BeSent expects a channel. Got:\n%s", format.Object(actual, 1))
  15. }
  16. channelType := reflect.TypeOf(actual)
  17. channelValue := reflect.ValueOf(actual)
  18. if channelType.ChanDir() == reflect.RecvDir {
  19. return false, fmt.Errorf("BeSent matcher cannot be passed a receive-only channel. Got:\n%s", format.Object(actual, 1))
  20. }
  21. argType := reflect.TypeOf(matcher.Arg)
  22. assignable := argType.AssignableTo(channelType.Elem())
  23. if !assignable {
  24. return false, fmt.Errorf("Cannot pass:\n%s to the channel:\n%s\nThe types don't match.", format.Object(matcher.Arg, 1), format.Object(actual, 1))
  25. }
  26. argValue := reflect.ValueOf(matcher.Arg)
  27. defer func() {
  28. if e := recover(); e != nil {
  29. success = false
  30. err = fmt.Errorf("Cannot send to a closed channel")
  31. matcher.channelClosed = true
  32. }
  33. }()
  34. winnerIndex, _, _ := reflect.Select([]reflect.SelectCase{
  35. {Dir: reflect.SelectSend, Chan: channelValue, Send: argValue},
  36. {Dir: reflect.SelectDefault},
  37. })
  38. var didSend bool
  39. if winnerIndex == 0 {
  40. didSend = true
  41. }
  42. return didSend, nil
  43. }
  44. func (matcher *BeSentMatcher) FailureMessage(actual interface{}) (message string) {
  45. return format.Message(actual, "to send:", matcher.Arg)
  46. }
  47. func (matcher *BeSentMatcher) NegatedFailureMessage(actual interface{}) (message string) {
  48. return format.Message(actual, "not to send:", matcher.Arg)
  49. }
  50. func (matcher *BeSentMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
  51. if !isChan(actual) {
  52. return false
  53. }
  54. return !matcher.channelClosed
  55. }