Codebase list golang-gomega / 7fb3200
Fix for #175. Printing the map keys incorrectly set the indentation back to 0 Ergin Babani 7 years ago
3 changed file(s) with 32 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
55 import (
66 "fmt"
77 "reflect"
8 "strconv"
89 "strings"
9 "strconv"
1010 )
1111
1212 // Use MaxDepth to set the maximum recursion depth when printing deeply nested objects
186186 }
187187
188188 func formatSlice(v reflect.Value, indentation uint) string {
189 if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 && isPrintableString(string(v.Bytes())){
189 if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 && isPrintableString(string(v.Bytes())) {
190190 return formatString(v.Bytes(), indentation)
191191 }
192192
215215 longest := 0
216216 for i, key := range v.MapKeys() {
217217 value := v.MapIndex(key)
218 result[i] = fmt.Sprintf("%s: %s", formatValue(key, 0), formatValue(value, indentation+1))
218 result[i] = fmt.Sprintf("%s: %s", formatValue(key, indentation+1), formatValue(value, indentation+1))
219219 if len(result[i]) > longest {
220220 longest = len(result[i])
221221 }
11
22 import (
33 "fmt"
4 "strings"
5
46 . "github.com/onsi/ginkgo"
57 . "github.com/onsi/gomega"
68 . "github.com/onsi/gomega/format"
79 "github.com/onsi/gomega/types"
8 "strings"
910 )
1011
1112 //recursive struct
161162 })
162163
163164 Describe("formatting []byte slices", func() {
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 })
165 Context("when the slice is made of printable bytes", func() {
166 It("should present it as string", func() {
167 b := []byte("a b c")
168 Ω(Object(b, 1)).Should(matchRegexp(`\[\]uint8 \| len:5, cap:\d+`, `a b c`))
169 })
170 })
171 Context("when the slice contains non-printable bytes", func() {
172 It("should present it as slice", func() {
173 b := []byte("a b c\n\x01\x02\x03\xff\x1bH")
174 Ω(Object(b, 1)).Should(matchRegexp(`\[\]uint8 \| len:12, cap:\d+`, `\[97, 32, 98, 32, 99, 10, 1, 2, 3, 255, 27, 72\]`))
175 })
176 })
177 })
177178
178179 Describe("formatting functions", func() {
179180 It("should give the type and format values correctly", func() {
428429 m["map"] = m
429430 Ω(Object(m, 1)).Should(ContainSubstring("..."))
430431 })
432
433 It("really should not go crazy...", func() {
434 type complexKey struct {
435 Value map[interface{}]int
436 }
437
438 complexObject := complexKey{}
439 complexObject.Value = make(map[interface{}]int)
440
441 complexObject.Value[&complexObject] = 2
442 Ω(Object(complexObject, 1)).Should(ContainSubstring("..."))
443 })
431444 })
432445
433446 Describe("When instructed to use the Stringer representation", func() {
11
22 import (
33 "fmt"
4 "math"
5
46 "github.com/onsi/gomega/format"
5 "math"
67 )
78
89 type BeNumericallyMatcher struct {