Codebase list golang-gomega / f4a44a4
Merge remote-tracking branch 'upstream/master' into kill-rogue-processes Jatin Naik 7 years ago
7 changed file(s) with 45 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
99
1010 Learn more about Ginkgo [here](http://onsi.github.io/ginkgo/)
1111
12 ## Community Matchers
13
14 A collection of community matchers is available on the [wiki](https://github.com/onsi/gomega/wiki).
15
1216 ## License
1317
1418 Gomega is MIT-Licensed
66 "fmt"
77 "reflect"
88 "strings"
9 "strconv"
910 )
1011
1112 // Use MaxDepth to set the maximum recursion depth when printing deeply nested objects
142143 case reflect.Ptr:
143144 return formatValue(value.Elem(), indentation)
144145 case reflect.Slice:
145 if value.Type().Elem().Kind() == reflect.Uint8 {
146 return formatString(value.Bytes(), indentation)
147 }
148146 return formatSlice(value, indentation)
149147 case reflect.String:
150148 return formatString(value.String(), indentation)
188186 }
189187
190188 func formatSlice(v reflect.Value, indentation uint) string {
189 if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 && isPrintableString(string(v.Bytes())){
190 return formatString(v.Bytes(), indentation)
191 }
192
191193 l := v.Len()
192194 result := make([]string, l)
193195 longest := 0
261263 return false
262264 }
263265
264 func isNil(a interface{}) bool {
265 if a == nil {
266 return true
267 }
268
269 switch reflect.TypeOf(a).Kind() {
270 case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
271 return reflect.ValueOf(a).IsNil()
272 }
273
274 return false
275 }
266 /*
267 Returns true when the string is entirely made of printable runes, false otherwise.
268 */
269 func isPrintableString(str string) bool {
270 for _, runeValue := range str {
271 if !strconv.IsPrint(runeValue) {
272 return false
273 }
274 }
275 return true
276 }
161161 })
162162
163163 Describe("formatting []byte slices", func() {
164 It("should present them as strings", func() {
165 b := []byte("a\nb\nc")
166 Ω(Object(b, 1)).Should(matchRegexp(`\[\]uint8 \| len:5, cap:\d+`, `a
167 b
168 c`))
169 })
170 })
164 Context("when the slice is made of printable bytes", func () {
165 It("should present it as string", func() {
166 b := []byte("a b c")
167 Ω(Object(b, 1)).Should(matchRegexp(`\[\]uint8 \| len:5, cap:\d+`, `a b c`))
168 })
169 })
170 Context("when the slice contains non-printable bytes", func () {
171 It("should present it as slice", func() {
172 b := []byte("a b c\n\x01\x02\x03\xff\x1bH")
173 Ω(Object(b, 1)).Should(matchRegexp(`\[\]uint8 \| len:12, cap:\d+`, `\[97, 32, 98, 32, 99, 10, 1, 2, 3, 255, 27, 72\]`))
174 })
175 })
176 })
171177
172178 Describe("formatting functions", func() {
173179 It("should give the type and format values correctly", func() {
2121 When Say succeeds, it fast forwards the gbytes.Buffer's read cursor to just after the succesful match.
2222 Thus, subsequent calls to Say will only match against the unread portion of the buffer
2323
24 Say pairs very well with Eventually. To asser that a buffer eventually receives data matching "[123]-star" within 3 seconds you can:
24 Say pairs very well with Eventually. To assert that a buffer eventually receives data matching "[123]-star" within 3 seconds you can:
2525
2626 Eventually(buffer, 3).Should(Say("[123]-star"))
2727
5959 Ω(session).Should(gexec.Exit())
6060
6161 When the session exits it closes the stdout and stderr gbytes buffers. This will short circuit any
62 Eventuallys waiting fo the buffers to Say something.
62 Eventuallys waiting for the buffers to Say something.
6363 */
6464 func Start(command *exec.Cmd, outWriter io.Writer, errWriter io.Writer) (*Session, error) {
6565 exited := make(chan struct{})
201201
202202 server := s.HTTPTestServer
203203 s.HTTPTestServer = nil
204 server.Close()
204 if server != nil {
205 server.Close()
206 }
205207 }
206208
207209 //ServeHTTP() makes Server an http.Handler
7777 })
7878 })
7979
80 Describe("closing server mulitple times", func() {
81 It("should not fail", func() {
82 s.Close()
83 Ω(s.Close).ShouldNot(Panic())
84 })
85 })
86
8087 Describe("allowing unhandled requests", func() {
8188 Context("when true", func() {
8289 BeforeEach(func() {