Commit a7f1e10d authored by Russ Cox's avatar Russ Cox

fmt: distinguish empty vs nil slice/map in %#v

Also update Scanf tests to cope with DeepEqual
distinguishing empty vs nil slice.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5375091
parent 53523f6a
...@@ -357,6 +357,10 @@ var fmttests = []struct { ...@@ -357,6 +357,10 @@ var fmttests = []struct {
{"%#v", map[string]B{"a": {1, 2}}, `map[string] fmt_test.B{"a":fmt_test.B{I:1, j:2}}`}, {"%#v", map[string]B{"a": {1, 2}}, `map[string] fmt_test.B{"a":fmt_test.B{I:1, j:2}}`},
{"%#v", []string{"a", "b"}, `[]string{"a", "b"}`}, {"%#v", []string{"a", "b"}, `[]string{"a", "b"}`},
{"%#v", SI{}, `fmt_test.SI{I:interface {}(nil)}`}, {"%#v", SI{}, `fmt_test.SI{I:interface {}(nil)}`},
{"%#v", []int(nil), `[]int(nil)`},
{"%#v", []int{}, `[]int{}`},
{"%#v", map[int]byte(nil), `map[int] uint8(nil)`},
{"%#v", map[int]byte{}, `map[int] uint8{}`},
// slices with other formats // slices with other formats
{"%#x", []int{1, 2, 15}, `[0x1 0x2 0xf]`}, {"%#x", []int{1, 2, 15}, `[0x1 0x2 0xf]`},
......
...@@ -795,6 +795,10 @@ BigSwitch: ...@@ -795,6 +795,10 @@ BigSwitch:
case reflect.Map: case reflect.Map:
if goSyntax { if goSyntax {
p.buf.WriteString(f.Type().String()) p.buf.WriteString(f.Type().String())
if f.IsNil() {
p.buf.WriteString("(nil)")
break
}
p.buf.WriteByte('{') p.buf.WriteByte('{')
} else { } else {
p.buf.Write(mapBytes) p.buf.Write(mapBytes)
...@@ -873,6 +877,10 @@ BigSwitch: ...@@ -873,6 +877,10 @@ BigSwitch:
} }
if goSyntax { if goSyntax {
p.buf.WriteString(value.Type().String()) p.buf.WriteString(value.Type().String())
if f.IsNil() {
p.buf.WriteString("(nil)")
break
}
p.buf.WriteByte('{') p.buf.WriteByte('{')
} else { } else {
p.buf.WriteByte('[') p.buf.WriteByte('[')
......
...@@ -324,7 +324,7 @@ var x, y Xs ...@@ -324,7 +324,7 @@ var x, y Xs
var z IntString var z IntString
var multiTests = []ScanfMultiTest{ var multiTests = []ScanfMultiTest{
{"", "", nil, nil, ""}, {"", "", []interface{}{}, []interface{}{}, ""},
{"%d", "23", args(&i), args(23), ""}, {"%d", "23", args(&i), args(23), ""},
{"%2s%3s", "22333", args(&s, &t), args("22", "333"), ""}, {"%2s%3s", "22333", args(&s, &t), args("22", "333"), ""},
{"%2d%3d", "44555", args(&i, &j), args(44, 555), ""}, {"%2d%3d", "44555", args(&i, &j), args(44, 555), ""},
...@@ -378,7 +378,7 @@ func testScan(name string, t *testing.T, scan func(r io.Reader, a ...interface{} ...@@ -378,7 +378,7 @@ func testScan(name string, t *testing.T, scan func(r io.Reader, a ...interface{}
} }
val := v.Interface() val := v.Interface()
if !reflect.DeepEqual(val, test.out) { if !reflect.DeepEqual(val, test.out) {
t.Errorf("%s scanning %q: expected %v got %v, type %T", name, test.text, test.out, val, val) t.Errorf("%s scanning %q: expected %#v got %#v, type %T", name, test.text, test.out, val, val)
} }
} }
} }
...@@ -417,7 +417,7 @@ func TestScanf(t *testing.T) { ...@@ -417,7 +417,7 @@ func TestScanf(t *testing.T) {
} }
val := v.Interface() val := v.Interface()
if !reflect.DeepEqual(val, test.out) { if !reflect.DeepEqual(val, test.out) {
t.Errorf("scanning (%q, %q): expected %v got %v, type %T", test.format, test.text, test.out, val, val) t.Errorf("scanning (%q, %q): expected %#v got %#v, type %T", test.format, test.text, test.out, val, val)
} }
} }
} }
...@@ -520,7 +520,7 @@ func testScanfMulti(name string, t *testing.T) { ...@@ -520,7 +520,7 @@ func testScanfMulti(name string, t *testing.T) {
} }
result := resultVal.Interface() result := resultVal.Interface()
if !reflect.DeepEqual(result, test.out) { if !reflect.DeepEqual(result, test.out) {
t.Errorf("scanning (%q, %q): expected %v got %v", test.format, test.text, test.out, result) t.Errorf("scanning (%q, %q): expected %#v got %#v", test.format, test.text, test.out, result)
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment