initial commit
[govpp.git] / vendor / github.com / onsi / gomega / gbytes / buffer_test.go
1 package gbytes_test
2
3 import (
4         "io"
5         "time"
6
7         . "github.com/onsi/gomega/gbytes"
8
9         . "github.com/onsi/ginkgo"
10         . "github.com/onsi/gomega"
11 )
12
13 var _ = Describe("Buffer", func() {
14         var buffer *Buffer
15
16         BeforeEach(func() {
17                 buffer = NewBuffer()
18         })
19
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")))
25
26                         Ω(buffer).Should(Say("bcd"))
27                         Ω(buffer.Contents()).Should(Equal([]byte("abcdef")))
28                 })
29         })
30
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"))
38                 })
39         })
40
41         Describe("reading from a buffer", func() {
42                 It("should read the current contents of the buffer", func() {
43                         buffer := BufferWithBytes([]byte("abcde"))
44
45                         dest := make([]byte, 3)
46                         n, err := buffer.Read(dest)
47                         Ω(err).ShouldNot(HaveOccurred())
48                         Ω(n).Should(Equal(3))
49                         Ω(string(dest)).Should(Equal("abc"))
50
51                         dest = make([]byte, 3)
52                         n, err = buffer.Read(dest)
53                         Ω(err).ShouldNot(HaveOccurred())
54                         Ω(n).Should(Equal(2))
55                         Ω(string(dest[:n])).Should(Equal("de"))
56
57                         n, err = buffer.Read(dest)
58                         Ω(err).Should(Equal(io.EOF))
59                         Ω(n).Should(Equal(0))
60                 })
61
62                 Context("after the buffer has been closed", func() {
63                         It("returns an error", func() {
64                                 buffer := BufferWithBytes([]byte("abcde"))
65
66                                 buffer.Close()
67
68                                 dest := make([]byte, 3)
69                                 n, err := buffer.Read(dest)
70                                 Ω(err).Should(HaveOccurred())
71                                 Ω(n).Should(Equal(0))
72                         })
73                 })
74         })
75
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) {
78                         go func() {
79                                 time.Sleep(10 * time.Millisecond)
80                                 buffer.Write([]byte("abcde"))
81                         }()
82
83                         A := buffer.Detect("%s", "a.c")
84                         B := buffer.Detect("def")
85
86                         var gotIt bool
87                         select {
88                         case gotIt = <-A:
89                         case <-B:
90                                 Fail("should not have gotten here")
91                         }
92
93                         Ω(gotIt).Should(BeTrue())
94                         Eventually(A).Should(BeClosed())
95
96                         buffer.Write([]byte("f"))
97                         Eventually(B).Should(Receive())
98                         Eventually(B).Should(BeClosed())
99
100                         close(done)
101                 })
102
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"))
108                         close(done)
109                 })
110
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"))
116                         <-A
117                         Ω(buffer).ShouldNot(Say("d"))
118                         Ω(buffer).Should(Say("e"))
119                         Eventually(A).Should(BeClosed())
120                         close(done)
121                 })
122
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())
130
131                         Ω(buffer).Should(Say("bcde"))
132                         <-buffer.Detect("f")
133                         close(done)
134                 })
135         })
136
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())
141
142                         buffer.Close()
143
144                         _, err = buffer.Write([]byte("def"))
145                         Ω(err).Should(HaveOccurred())
146
147                         Ω(buffer.Contents()).Should(Equal([]byte("abc")))
148                 })
149
150                 It("should be closed", func() {
151                         Ω(buffer.Closed()).Should(BeFalse())
152
153                         buffer.Close()
154
155                         Ω(buffer.Closed()).Should(BeTrue())
156                 })
157         })
158 })