2018-01-29 19:44:18 +00:00
|
|
|
package gbytes_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
. "github.com/onsi/gomega/gbytes"
|
|
|
|
|
2018-01-29 19:44:18 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
type speaker struct {
|
|
|
|
buffer *Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *speaker) Buffer() *Buffer {
|
|
|
|
return s.buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Describe("SayMatcher", func() {
|
|
|
|
var buffer *Buffer
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
buffer = NewBuffer()
|
|
|
|
buffer.Write([]byte("abc"))
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("when actual is not a gexec Buffer, or a BufferProvider", func() {
|
|
|
|
It("should error", func() {
|
|
|
|
failures := InterceptGomegaFailures(func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect("foo").Should(Say("foo"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures[0]).Should(ContainSubstring("*gbytes.Buffer"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("when a match is found", func() {
|
|
|
|
It("should succeed", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).Should(Say("abc"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should support printf-like formatting", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).Should(Say("a%sc", "b"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should match literal %", func() {
|
|
|
|
buffer.Write([]byte("%"))
|
|
|
|
Expect(buffer).Should(Say("abc%"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should use a regular expression", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).Should(Say("a.c"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should fastforward the buffer", func() {
|
|
|
|
buffer.Write([]byte("def"))
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).Should(Say("abcd"))
|
|
|
|
Expect(buffer).Should(Say("ef"))
|
|
|
|
Expect(buffer).ShouldNot(Say("[a-z]"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("when no match is found", func() {
|
|
|
|
It("should not error", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).ShouldNot(Say("def"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Context("when the buffer is closed", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
buffer.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should abort an eventually", func() {
|
|
|
|
t := time.Now()
|
|
|
|
failures := InterceptGomegaFailures(func() {
|
|
|
|
Eventually(buffer).Should(Say("def"))
|
|
|
|
})
|
|
|
|
Eventually(buffer).ShouldNot(Say("def"))
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(time.Since(t)).Should(BeNumerically("<", 500*time.Millisecond))
|
|
|
|
Expect(failures).Should(HaveLen(1))
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
t = time.Now()
|
|
|
|
Eventually(buffer).Should(Say("abc"))
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(time.Since(t)).Should(BeNumerically("<", 500*time.Millisecond))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should abort a consistently", func() {
|
|
|
|
t := time.Now()
|
|
|
|
Consistently(buffer, 2.0).ShouldNot(Say("def"))
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(time.Since(t)).Should(BeNumerically("<", 500*time.Millisecond))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should not error with a synchronous matcher", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).ShouldNot(Say("def"))
|
|
|
|
Expect(buffer).Should(Say("abc"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("when a positive match fails", func() {
|
|
|
|
It("should report where it got stuck", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).Should(Say("abc"))
|
2018-01-29 19:44:18 +00:00
|
|
|
buffer.Write([]byte("def"))
|
|
|
|
failures := InterceptGomegaFailures(func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).Should(Say("abc"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures[0]).Should(ContainSubstring("Got stuck at:"))
|
|
|
|
Expect(failures[0]).Should(ContainSubstring("def"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("when a negative match fails", func() {
|
|
|
|
It("should report where it got stuck", func() {
|
|
|
|
failures := InterceptGomegaFailures(func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).ShouldNot(Say("abc"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures[0]).Should(ContainSubstring("Saw:"))
|
|
|
|
Expect(failures[0]).Should(ContainSubstring("Which matches the unexpected:"))
|
|
|
|
Expect(failures[0]).Should(ContainSubstring("abc"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("when a match is not found", func() {
|
|
|
|
It("should not fastforward the buffer", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).ShouldNot(Say("def"))
|
|
|
|
Expect(buffer).Should(Say("abc"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("a nice real-life example", func() {
|
|
|
|
It("should behave well", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).Should(Say("abc"))
|
2018-01-29 19:44:18 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
buffer.Write([]byte("def"))
|
|
|
|
}()
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(buffer).ShouldNot(Say("def"))
|
2018-01-29 19:44:18 +00:00
|
|
|
Eventually(buffer).Should(Say("def"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("when actual is a BufferProvider", func() {
|
|
|
|
It("should use actual's buffer", func() {
|
|
|
|
s := &speaker{
|
|
|
|
buffer: NewBuffer(),
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(s).ShouldNot(Say("abc"))
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
s.Buffer().Write([]byte("abc"))
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(s).Should(Say("abc"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should abort an eventually", func() {
|
|
|
|
s := &speaker{
|
|
|
|
buffer: NewBuffer(),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.buffer.Close()
|
|
|
|
|
|
|
|
t := time.Now()
|
|
|
|
failures := InterceptGomegaFailures(func() {
|
|
|
|
Eventually(s).Should(Say("def"))
|
|
|
|
})
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures).Should(HaveLen(1))
|
|
|
|
Expect(time.Since(t)).Should(BeNumerically("<", 500*time.Millisecond))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|