initial commit
[govpp.git] / vendor / github.com / onsi / gomega / gbytes / say_matcher_test.go
1 package gbytes_test
2
3 import (
4         . "github.com/onsi/gomega/gbytes"
5         "time"
6
7         . "github.com/onsi/ginkgo"
8         . "github.com/onsi/gomega"
9 )
10
11 type speaker struct {
12         buffer *Buffer
13 }
14
15 func (s *speaker) Buffer() *Buffer {
16         return s.buffer
17 }
18
19 var _ = Describe("SayMatcher", func() {
20         var buffer *Buffer
21
22         BeforeEach(func() {
23                 buffer = NewBuffer()
24                 buffer.Write([]byte("abc"))
25         })
26
27         Context("when actual is not a gexec Buffer, or a BufferProvider", func() {
28                 It("should error", func() {
29                         failures := InterceptGomegaFailures(func() {
30                                 Ω("foo").Should(Say("foo"))
31                         })
32                         Ω(failures[0]).Should(ContainSubstring("*gbytes.Buffer"))
33                 })
34         })
35
36         Context("when a match is found", func() {
37                 It("should succeed", func() {
38                         Ω(buffer).Should(Say("abc"))
39                 })
40
41                 It("should support printf-like formatting", func() {
42                         Ω(buffer).Should(Say("a%sc", "b"))
43                 })
44
45                 It("should use a regular expression", func() {
46                         Ω(buffer).Should(Say("a.c"))
47                 })
48
49                 It("should fastforward the buffer", func() {
50                         buffer.Write([]byte("def"))
51                         Ω(buffer).Should(Say("abcd"))
52                         Ω(buffer).Should(Say("ef"))
53                         Ω(buffer).ShouldNot(Say("[a-z]"))
54                 })
55         })
56
57         Context("when no match is found", func() {
58                 It("should not error", func() {
59                         Ω(buffer).ShouldNot(Say("def"))
60                 })
61
62                 Context("when the buffer is closed", func() {
63                         BeforeEach(func() {
64                                 buffer.Close()
65                         })
66
67                         It("should abort an eventually", func() {
68                                 t := time.Now()
69                                 failures := InterceptGomegaFailures(func() {
70                                         Eventually(buffer).Should(Say("def"))
71                                 })
72                                 Eventually(buffer).ShouldNot(Say("def"))
73                                 Ω(time.Since(t)).Should(BeNumerically("<", 500*time.Millisecond))
74                                 Ω(failures).Should(HaveLen(1))
75
76                                 t = time.Now()
77                                 Eventually(buffer).Should(Say("abc"))
78                                 Ω(time.Since(t)).Should(BeNumerically("<", 500*time.Millisecond))
79                         })
80
81                         It("should abort a consistently", func() {
82                                 t := time.Now()
83                                 Consistently(buffer, 2.0).ShouldNot(Say("def"))
84                                 Ω(time.Since(t)).Should(BeNumerically("<", 500*time.Millisecond))
85                         })
86
87                         It("should not error with a synchronous matcher", func() {
88                                 Ω(buffer).ShouldNot(Say("def"))
89                                 Ω(buffer).Should(Say("abc"))
90                         })
91                 })
92         })
93
94         Context("when a positive match fails", func() {
95                 It("should report where it got stuck", func() {
96                         Ω(buffer).Should(Say("abc"))
97                         buffer.Write([]byte("def"))
98                         failures := InterceptGomegaFailures(func() {
99                                 Ω(buffer).Should(Say("abc"))
100                         })
101                         Ω(failures[0]).Should(ContainSubstring("Got stuck at:"))
102                         Ω(failures[0]).Should(ContainSubstring("def"))
103                 })
104         })
105
106         Context("when a negative match fails", func() {
107                 It("should report where it got stuck", func() {
108                         failures := InterceptGomegaFailures(func() {
109                                 Ω(buffer).ShouldNot(Say("abc"))
110                         })
111                         Ω(failures[0]).Should(ContainSubstring("Saw:"))
112                         Ω(failures[0]).Should(ContainSubstring("Which matches the unexpected:"))
113                         Ω(failures[0]).Should(ContainSubstring("abc"))
114                 })
115         })
116
117         Context("when a match is not found", func() {
118                 It("should not fastforward the buffer", func() {
119                         Ω(buffer).ShouldNot(Say("def"))
120                         Ω(buffer).Should(Say("abc"))
121                 })
122         })
123
124         Context("a nice real-life example", func() {
125                 It("should behave well", func() {
126                         Ω(buffer).Should(Say("abc"))
127                         go func() {
128                                 time.Sleep(10 * time.Millisecond)
129                                 buffer.Write([]byte("def"))
130                         }()
131                         Ω(buffer).ShouldNot(Say("def"))
132                         Eventually(buffer).Should(Say("def"))
133                 })
134         })
135
136         Context("when actual is a BufferProvider", func() {
137                 It("should use actual's buffer", func() {
138                         s := &speaker{
139                                 buffer: NewBuffer(),
140                         }
141
142                         Ω(s).ShouldNot(Say("abc"))
143
144                         s.Buffer().Write([]byte("abc"))
145                         Ω(s).Should(Say("abc"))
146                 })
147
148                 It("should abort an eventually", func() {
149                         s := &speaker{
150                                 buffer: NewBuffer(),
151                         }
152
153                         s.buffer.Close()
154
155                         t := time.Now()
156                         failures := InterceptGomegaFailures(func() {
157                                 Eventually(s).Should(Say("def"))
158                         })
159                         Ω(failures).Should(HaveLen(1))
160                         Ω(time.Since(t)).Should(BeNumerically("<", 500*time.Millisecond))
161                 })
162         })
163 })