7 . "github.com/onsi/gomega/gbytes"
9 . "github.com/onsi/ginkgo"
10 . "github.com/onsi/gomega"
13 var _ = Describe("Buffer", func() {
20 Describe("dumping the entire contents of the buffer", func() {
21 It("should return everything that's been written", func() {
22 buffer.Write([]byte("abc"))
23 buffer.Write([]byte("def"))
24 Ω(buffer.Contents()).Should(Equal([]byte("abcdef")))
26 Ω(buffer).Should(Say("bcd"))
27 Ω(buffer.Contents()).Should(Equal([]byte("abcdef")))
31 Describe("creating a buffer with bytes", func() {
32 It("should create the buffer with the cursor set to the beginning", func() {
33 buffer := BufferWithBytes([]byte("abcdef"))
34 Ω(buffer.Contents()).Should(Equal([]byte("abcdef")))
35 Ω(buffer).Should(Say("abc"))
36 Ω(buffer).ShouldNot(Say("abc"))
37 Ω(buffer).Should(Say("def"))
41 Describe("reading from a buffer", func() {
42 It("should read the current contents of the buffer", func() {
43 buffer := BufferWithBytes([]byte("abcde"))
45 dest := make([]byte, 3)
46 n, err := buffer.Read(dest)
47 Ω(err).ShouldNot(HaveOccurred())
49 Ω(string(dest)).Should(Equal("abc"))
51 dest = make([]byte, 3)
52 n, err = buffer.Read(dest)
53 Ω(err).ShouldNot(HaveOccurred())
55 Ω(string(dest[:n])).Should(Equal("de"))
57 n, err = buffer.Read(dest)
58 Ω(err).Should(Equal(io.EOF))
62 Context("after the buffer has been closed", func() {
63 It("returns an error", func() {
64 buffer := BufferWithBytes([]byte("abcde"))
68 dest := make([]byte, 3)
69 n, err := buffer.Read(dest)
70 Ω(err).Should(HaveOccurred())
76 Describe("detecting regular expressions", func() {
77 It("should fire the appropriate channel when the passed in pattern matches, then close it", func(done Done) {
79 time.Sleep(10 * time.Millisecond)
80 buffer.Write([]byte("abcde"))
83 A := buffer.Detect("%s", "a.c")
84 B := buffer.Detect("def")
90 Fail("should not have gotten here")
93 Ω(gotIt).Should(BeTrue())
94 Eventually(A).Should(BeClosed())
96 buffer.Write([]byte("f"))
97 Eventually(B).Should(Receive())
98 Eventually(B).Should(BeClosed())
103 It("should fast-forward the buffer upon detection", func(done Done) {
104 buffer.Write([]byte("abcde"))
105 <-buffer.Detect("abc")
106 Ω(buffer).ShouldNot(Say("abc"))
107 Ω(buffer).Should(Say("de"))
111 It("should only fast-forward the buffer when the channel is read, and only if doing so would not rewind it", func(done Done) {
112 buffer.Write([]byte("abcde"))
113 A := buffer.Detect("abc")
114 time.Sleep(20 * time.Millisecond) //give the goroutine a chance to detect and write to the channel
115 Ω(buffer).Should(Say("abcd"))
117 Ω(buffer).ShouldNot(Say("d"))
118 Ω(buffer).Should(Say("e"))
119 Eventually(A).Should(BeClosed())
123 It("should be possible to cancel a detection", func(done Done) {
124 A := buffer.Detect("abc")
125 B := buffer.Detect("def")
126 buffer.CancelDetects()
127 buffer.Write([]byte("abcdef"))
128 Eventually(A).Should(BeClosed())
129 Eventually(B).Should(BeClosed())
131 Ω(buffer).Should(Say("bcde"))
137 Describe("closing the buffer", func() {
138 It("should error when further write attempts are made", func() {
139 _, err := buffer.Write([]byte("abc"))
140 Ω(err).ShouldNot(HaveOccurred())
144 _, err = buffer.Write([]byte("def"))
145 Ω(err).Should(HaveOccurred())
147 Ω(buffer.Contents()).Should(Equal([]byte("abc")))
150 It("should be closed", func() {
151 Ω(buffer.Closed()).Should(BeFalse())
155 Ω(buffer.Closed()).Should(BeTrue())