Path: blob/main/vendor/github.com/onsi/gomega/matchers/be_sent_matcher.go
2880 views
// untested sections: 312package matchers34import (5"fmt"6"reflect"78"github.com/onsi/gomega/format"9)1011type BeSentMatcher struct {12Arg any13channelClosed bool14}1516func (matcher *BeSentMatcher) Match(actual any) (success bool, err error) {17if !isChan(actual) {18return false, fmt.Errorf("BeSent expects a channel. Got:\n%s", format.Object(actual, 1))19}2021channelType := reflect.TypeOf(actual)22channelValue := reflect.ValueOf(actual)2324if channelType.ChanDir() == reflect.RecvDir {25return false, fmt.Errorf("BeSent matcher cannot be passed a receive-only channel. Got:\n%s", format.Object(actual, 1))26}2728argType := reflect.TypeOf(matcher.Arg)29assignable := argType.AssignableTo(channelType.Elem())3031if !assignable {32return 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))33}3435argValue := reflect.ValueOf(matcher.Arg)3637defer func() {38if e := recover(); e != nil {39success = false40err = fmt.Errorf("Cannot send to a closed channel")41matcher.channelClosed = true42}43}()4445winnerIndex, _, _ := reflect.Select([]reflect.SelectCase{46{Dir: reflect.SelectSend, Chan: channelValue, Send: argValue},47{Dir: reflect.SelectDefault},48})4950var didSend bool51if winnerIndex == 0 {52didSend = true53}5455return didSend, nil56}5758func (matcher *BeSentMatcher) FailureMessage(actual any) (message string) {59return format.Message(actual, "to send:", matcher.Arg)60}6162func (matcher *BeSentMatcher) NegatedFailureMessage(actual any) (message string) {63return format.Message(actual, "not to send:", matcher.Arg)64}6566func (matcher *BeSentMatcher) MatchMayChangeInTheFuture(actual any) bool {67if !isChan(actual) {68return false69}7071return !matcher.channelClosed72}737475