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