Fix for #175. Printing the map keys incorrectly set the indentation back to 0
diff --git a/format/format.go b/format/format.go index 06355d9..952726e 100644 --- a/format/format.go +++ b/format/format.go
@@ -6,8 +6,8 @@ import ( "fmt" "reflect" - "strings" "strconv" + "strings" ) // Use MaxDepth to set the maximum recursion depth when printing deeply nested objects @@ -187,7 +187,7 @@ } func formatSlice(v reflect.Value, indentation uint) string { - if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 && isPrintableString(string(v.Bytes())){ + if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 && isPrintableString(string(v.Bytes())) { return formatString(v.Bytes(), indentation) } @@ -216,7 +216,7 @@ longest := 0 for i, key := range v.MapKeys() { value := v.MapIndex(key) - result[i] = fmt.Sprintf("%s: %s", formatValue(key, 0), formatValue(value, indentation+1)) + result[i] = fmt.Sprintf("%s: %s", formatValue(key, indentation+1), formatValue(value, indentation+1)) if len(result[i]) > longest { longest = len(result[i]) }
diff --git a/format/format_test.go b/format/format_test.go index 1391fe7..b27df8e 100644 --- a/format/format_test.go +++ b/format/format_test.go
@@ -2,11 +2,12 @@ import ( "fmt" + "strings" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "github.com/onsi/gomega/format" "github.com/onsi/gomega/types" - "strings" ) //recursive struct @@ -162,19 +163,19 @@ }) Describe("formatting []byte slices", func() { - Context("when the slice is made of printable bytes", func () { - It("should present it as string", func() { - b := []byte("a b c") - Ω(Object(b, 1)).Should(matchRegexp(`\[\]uint8 \| len:5, cap:\d+`, `a b c`)) - }) - }) - Context("when the slice contains non-printable bytes", func () { - It("should present it as slice", func() { - b := []byte("a b c\n\x01\x02\x03\xff\x1bH") - Ω(Object(b, 1)).Should(matchRegexp(`\[\]uint8 \| len:12, cap:\d+`, `\[97, 32, 98, 32, 99, 10, 1, 2, 3, 255, 27, 72\]`)) - }) - }) - }) + Context("when the slice is made of printable bytes", func() { + It("should present it as string", func() { + b := []byte("a b c") + Ω(Object(b, 1)).Should(matchRegexp(`\[\]uint8 \| len:5, cap:\d+`, `a b c`)) + }) + }) + Context("when the slice contains non-printable bytes", func() { + It("should present it as slice", func() { + b := []byte("a b c\n\x01\x02\x03\xff\x1bH") + Ω(Object(b, 1)).Should(matchRegexp(`\[\]uint8 \| len:12, cap:\d+`, `\[97, 32, 98, 32, 99, 10, 1, 2, 3, 255, 27, 72\]`)) + }) + }) + }) Describe("formatting functions", func() { It("should give the type and format values correctly", func() { @@ -429,6 +430,18 @@ m["map"] = m Ω(Object(m, 1)).Should(ContainSubstring("...")) }) + + It("really should not go crazy...", func() { + type complexKey struct { + Value map[interface{}]int + } + + complexObject := complexKey{} + complexObject.Value = make(map[interface{}]int) + + complexObject.Value[&complexObject] = 2 + Ω(Object(complexObject, 1)).Should(ContainSubstring("...")) + }) }) Describe("When instructed to use the Stringer representation", func() {
diff --git a/matchers/be_numerically_matcher.go b/matchers/be_numerically_matcher.go index 52f83fe..0c157f6 100644 --- a/matchers/be_numerically_matcher.go +++ b/matchers/be_numerically_matcher.go
@@ -2,8 +2,9 @@ import ( "fmt" - "github.com/onsi/gomega/format" "math" + + "github.com/onsi/gomega/format" ) type BeNumericallyMatcher struct {