]> Cypherpunks repositories - gostls13.git/commitdiff
expvar: quote StringFunc output, same as String output.
authorDavid Symonds <dsymonds@golang.org>
Mon, 20 Dec 2010 06:35:21 +0000 (17:35 +1100)
committerAndrew Gerrand <adg@golang.org>
Mon, 20 Dec 2010 06:35:21 +0000 (17:35 +1100)
R=adg
CC=golang-dev
https://golang.org/cl/3797041

src/pkg/expvar/expvar.go
src/pkg/expvar/expvar_test.go

index 6068fbb4ded38a314692a94dd4e58b5523d35bd3..fb20b25b2e02151d54404cfb2fb07b8c76e0a3f2 100644 (file)
@@ -152,7 +152,7 @@ func (v IntFunc) String() string { return strconv.Itoa64(v()) }
 // The function will be called each time the Var is evaluated.
 type StringFunc func() string
 
-func (f StringFunc) String() string { return f() }
+func (f StringFunc) String() string { return strconv.Quote(f()) }
 
 
 // All published variables.
index 3dfc55af36406b18f1cad8580433491c2b9c0023..009f24d1a7772253a3f924bc246b21bb09a9ef93 100644 (file)
@@ -86,8 +86,8 @@ func TestMapCounter(t *testing.T) {
 }
 
 func TestIntFunc(t *testing.T) {
-       x := int(4)
-       ix := IntFunc(func() int64 { return int64(x) })
+       x := int64(4)
+       ix := IntFunc(func() int64 { return x })
        if s := ix.String(); s != "4" {
                t.Errorf("ix.String() = %v, want 4", s)
        }
@@ -97,3 +97,16 @@ func TestIntFunc(t *testing.T) {
                t.Errorf("ix.String() = %v, want 5", s)
        }
 }
+
+func TestStringFunc(t *testing.T) {
+       x := "hello"
+       sx := StringFunc(func() string { return x })
+       if s, exp := sx.String(), `"hello"`; s != exp {
+               t.Errorf(`sx.String() = %q, want %q`, s, exp)
+       }
+
+       x = "goodbye"
+       if s, exp := sx.String(), `"goodbye"`; s != exp {
+               t.Errorf(`sx.String() = %q, want %q`, s, exp)
+       }
+}