7 "github.com/onsi/gomega/format"
10 //Objects satisfying the BufferProvider can be used with the Say matcher.
11 type BufferProvider interface {
16 Say is a Gomega matcher that operates on gbytes.Buffers:
18 Ω(buffer).Should(Say("something"))
20 will succeed if the unread portion of the buffer matches the regular expression "something".
22 When Say succeeds, it fast forwards the gbytes.Buffer's read cursor to just after the succesful match.
23 Thus, subsequent calls to Say will only match against the unread portion of the buffer
25 Say pairs very well with Eventually. To assert that a buffer eventually receives data matching "[123]-star" within 3 seconds you can:
27 Eventually(buffer, 3).Should(Say("[123]-star"))
29 Ditto with consistently. To assert that a buffer does not receive data matching "never-see-this" for 1 second you can:
31 Consistently(buffer, 1).ShouldNot(Say("never-see-this"))
33 In addition to bytes.Buffers, Say can operate on objects that implement the gbytes.BufferProvider interface.
34 In such cases, Say simply operates on the *gbytes.Buffer returned by Buffer()
36 If the buffer is closed, the Say matcher will tell Eventually to abort.
38 func Say(expected string, args ...interface{}) *sayMatcher {
39 formattedRegexp := expected
41 formattedRegexp = fmt.Sprintf(expected, args...)
44 re: regexp.MustCompile(formattedRegexp),
48 type sayMatcher struct {
50 receivedSayings []byte
53 func (m *sayMatcher) buffer(actual interface{}) (*Buffer, bool) {
56 switch x := actual.(type) {
68 func (m *sayMatcher) Match(actual interface{}) (success bool, err error) {
69 buffer, ok := m.buffer(actual)
71 return false, fmt.Errorf("Say must be passed a *gbytes.Buffer or BufferProvider. Got:\n%s", format.Object(actual, 1))
74 didSay, sayings := buffer.didSay(m.re)
75 m.receivedSayings = sayings
80 func (m *sayMatcher) FailureMessage(actual interface{}) (message string) {
82 "Got stuck at:\n%s\nWaiting for:\n%s",
83 format.IndentString(string(m.receivedSayings), 1),
84 format.IndentString(m.re.String(), 1),
88 func (m *sayMatcher) NegatedFailureMessage(actual interface{}) (message string) {
90 "Saw:\n%s\nWhich matches the unexpected:\n%s",
91 format.IndentString(string(m.receivedSayings), 1),
92 format.IndentString(m.re.String(), 1),
96 func (m *sayMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
97 switch x := actual.(type) {
101 return !x.Buffer().Closed()