1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:44:42 -07:00

fmt: adjust formatting of invalid reflect.Value, add more tests

Repeat of CL 8951.

Change-Id: I5430e4a9eb5d8b7d0e3963657092bede67439056
Reviewed-on: https://go-review.googlesource.com/9003
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Russ Cox 2015-04-16 15:18:27 -04:00
parent c8aba85e4a
commit a2ef54b51e
2 changed files with 10 additions and 0 deletions

View 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

View 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: