]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: adjust formatting of invalid reflect.Value, add more tests
authorRuss Cox <rsc@golang.org>
Thu, 16 Apr 2015 19:18:27 +0000 (15:18 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 17 Apr 2015 14:14:39 +0000 (14:14 +0000)
Repeat of CL 8951.

Change-Id: I5430e4a9eb5d8b7d0e3963657092bede67439056
Reviewed-on: https://go-review.googlesource.com/9003
Reviewed-by: Rob Pike <r@golang.org>
src/fmt/fmt_test.go
src/fmt/print.go

index 96505b82716abc926aaf72641ad3ca7e56735aa4..c06f9a1fcfce422ec5dd4efc95f352cc3e1a5512 100644 (file)
@@ -686,6 +686,14 @@ var fmtTests = []struct {
        // Issue 8965.
        {"%v", reflect.ValueOf(A{}).Field(0).String(), "<int Value>"}, // Equivalent to the old way.
        {"%v", reflect.ValueOf(A{}).Field(0), "0"},                    // Sees inside the field.
+
+       // verbs apply to the extracted value too.
+       {"%s", reflect.ValueOf("hello"), "hello"},
+       {"%q", reflect.ValueOf("hello"), `"hello"`},
+       {"%#04x", reflect.ValueOf(256), "0x0100"},
+
+       // invalid reflect.Value doesn't crash.
+       {"%v", reflect.Value{}, "<invalid reflect.Value>"},
 }
 
 // zeroFill generates zero-filled strings of the specified width. The length
index c8038f09a8818b7ed0457a6387d1f3531aeabb7d..8e35a890ec9f2c95740232081aae4fc9bdc1d2ee 100644 (file)
@@ -847,6 +847,8 @@ func (p *pp) printReflectValue(value reflect.Value, verb rune, depth int) (wasSt
        p.value = value
 BigSwitch:
        switch f := value; f.Kind() {
+       case reflect.Invalid:
+               p.buf.WriteString("<invalid reflect.Value>")
        case reflect.Bool:
                p.fmtBool(f.Bool(), verb)
        case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: