9
"github.com/onsi/gomega/format"
12
//Objects satisfying the BufferProvider can be used with the Say matcher.
13
type BufferProvider interface {
18
Say is a Gomega matcher that operates on gbytes.Buffers:
20
Expect(buffer).Should(Say("something"))
22
will succeed if the unread portion of the buffer matches the regular expression "something".
24
When Say succeeds, it fast forwards the gbytes.Buffer's read cursor to just after the successful match.
25
Thus, subsequent calls to Say will only match against the unread portion of the buffer
27
Say pairs very well with Eventually. To assert that a buffer eventually receives data matching "[123]-star" within 3 seconds you can:
29
Eventually(buffer, 3).Should(Say("[123]-star"))
31
Ditto with consistently. To assert that a buffer does not receive data matching "never-see-this" for 1 second you can:
33
Consistently(buffer, 1).ShouldNot(Say("never-see-this"))
35
In addition to bytes.Buffers, Say can operate on objects that implement the gbytes.BufferProvider interface.
36
In such cases, Say simply operates on the *gbytes.Buffer returned by Buffer()
38
If the buffer is closed, the Say matcher will tell Eventually to abort.
40
func Say(expected string, args ...interface{}) *sayMatcher {
42
expected = fmt.Sprintf(expected, args...)
45
re: regexp.MustCompile(expected),
49
type sayMatcher struct {
51
receivedSayings []byte
54
func (m *sayMatcher) buffer(actual interface{}) (*Buffer, bool) {
57
switch x := actual.(type) {
69
func (m *sayMatcher) Match(actual interface{}) (success bool, err error) {
70
buffer, ok := m.buffer(actual)
72
return false, fmt.Errorf("Say must be passed a *gbytes.Buffer or BufferProvider. Got:\n%s", format.Object(actual, 1))
75
didSay, sayings := buffer.didSay(m.re)
76
m.receivedSayings = sayings
81
func (m *sayMatcher) FailureMessage(actual interface{}) (message string) {
83
"Got stuck at:\n%s\nWaiting for:\n%s",
84
format.IndentString(string(m.receivedSayings), 1),
85
format.IndentString(m.re.String(), 1),
89
func (m *sayMatcher) NegatedFailureMessage(actual interface{}) (message string) {
91
"Saw:\n%s\nWhich matches the unexpected:\n%s",
92
format.IndentString(string(m.receivedSayings), 1),
93
format.IndentString(m.re.String(), 1),
97
func (m *sayMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
98
switch x := actual.(type) {
102
return !x.Buffer().Closed()