github.com/onsi/gomega@v1.32.0/matchers/be_closed_matcher.go (about) 1 // untested sections: 2 2 3 package matchers 4 5 import ( 6 "fmt" 7 "reflect" 8 9 "github.com/onsi/gomega/format" 10 ) 11 12 type BeClosedMatcher struct { 13 } 14 15 func (matcher *BeClosedMatcher) Match(actual interface{}) (success bool, err error) { 16 if !isChan(actual) { 17 return false, fmt.Errorf("BeClosed matcher expects a channel. Got:\n%s", format.Object(actual, 1)) 18 } 19 20 channelType := reflect.TypeOf(actual) 21 channelValue := reflect.ValueOf(actual) 22 23 if channelType.ChanDir() == reflect.SendDir { 24 return false, fmt.Errorf("BeClosed matcher cannot determine if a send-only channel is closed or open. Got:\n%s", format.Object(actual, 1)) 25 } 26 27 winnerIndex, _, open := reflect.Select([]reflect.SelectCase{ 28 {Dir: reflect.SelectRecv, Chan: channelValue}, 29 {Dir: reflect.SelectDefault}, 30 }) 31 32 var closed bool 33 if winnerIndex == 0 { 34 closed = !open 35 } else if winnerIndex == 1 { 36 closed = false 37 } 38 39 return closed, nil 40 } 41 42 func (matcher *BeClosedMatcher) FailureMessage(actual interface{}) (message string) { 43 return format.Message(actual, "to be closed") 44 } 45 46 func (matcher *BeClosedMatcher) NegatedFailureMessage(actual interface{}) (message string) { 47 return format.Message(actual, "to be open") 48 }