Package gbytes
Overview ▹
Index ▹
func Say ¶
func Say(expected string, args ...interface{}) *sayMatcher
Say is a Gomega matcher that operates on gbytes.Buffers:
Ω(buffer).Should(Say("something"))
will succeed if the unread portion of the buffer matches the regular expression "something".
When Say succeeds, it fast forwards the gbytes.Buffer's read cursor to just after the succesful match. Thus, subsequent calls to Say will only match against the unread portion of the buffer
Say pairs very well with Eventually. To assert that a buffer eventually receives data matching "[123]-star" within 3 seconds you can:
Eventually(buffer, 3).Should(Say("[123]-star"))
Ditto with consistently. To assert that a buffer does not receive data matching "never-see-this" for 1 second you can:
Consistently(buffer, 1).ShouldNot(Say("never-see-this"))
In addition to bytes.Buffers, Say can operate on objects that implement the gbytes.BufferProvider interface. In such cases, Say simply operates on the *gbytes.Buffer returned by Buffer()
If the buffer is closed, the Say matcher will tell Eventually to abort.
type Buffer ¶
gbytes.Buffer implements an io.Writer and can be used with the gbytes.Say matcher.
You should only use a gbytes.Buffer in test code. It stores all writes in an in-memory buffer - behavior that is inappropriate for production code!
type Buffer struct {
// contains filtered or unexported fields
}
func BufferWithBytes ¶
func BufferWithBytes(bytes []byte) *Buffer
BufferWithBytes returns a new gbytes.Buffer seeded with the passed in bytes
func NewBuffer ¶
func NewBuffer() *Buffer
NewBuffer returns a new gbytes.Buffer
func (*Buffer) CancelDetects ¶
func (b *Buffer) CancelDetects()
CancelDetects cancels any pending detects and cleans up their goroutines. You should always call this when you're done with a set of Detect channels.
func (*Buffer) Close ¶
func (b *Buffer) Close() error
Close signifies that the buffer will no longer be written to
func (*Buffer) Closed ¶
func (b *Buffer) Closed() bool
Closed returns true if the buffer has been closed
func (*Buffer) Contents ¶
func (b *Buffer) Contents() []byte
Contents returns all data ever written to the buffer.
func (*Buffer) Detect ¶
func (b *Buffer) Detect(desired string, args ...interface{}) chan bool
Detect takes a regular expression and returns a channel.
The channel will receive true the first time data matching the regular expression is written to the buffer. The channel is subsequently closed and the buffer's read-cursor is fast-forwarded to just after the matching region.
You typically don't need to use Detect and should use the ghttp.Say matcher instead. Detect is useful, however, in cases where your code must be branch and handle different outputs written to the buffer.
For example, consider a buffer hooked up to the stdout of a client library. You may (or may not, depending on state outside of your control) need to authenticate the client library.
You could do something like:
select { case <-buffer.Detect("You are not logged in"):
//log in
case <-buffer.Detect("Success"):
//carry on
case <-time.After(time.Second):
//welp
} buffer.CancelDetects()
You should always call CancelDetects after using Detect. This will close any channels that have not detected and clean up the goroutines that were spawned to support them.
Finally, you can pass detect a format string followed by variadic arguments. This will construct the regexp using fmt.Sprintf.
func (*Buffer) Read ¶
func (b *Buffer) Read(d []byte) (int, error)
Read implements the io.Reader interface. It advances the cursor as it reads.
Returns an error if called after Close.
func (*Buffer) Write ¶
func (b *Buffer) Write(p []byte) (n int, err error)
Write implements the io.Writer interface
type BufferProvider ¶
Objects satisfying the BufferProvider can be used with the Say matcher.
type BufferProvider interface { Buffer() *Buffer }