1
0
mirror of https://github.com/golang/go synced 2024-11-12 09:50:21 -07:00

expvar: quote StringFunc output, same as String output.

R=adg
CC=golang-dev
https://golang.org/cl/3797041
This commit is contained in:
David Symonds 2010-12-20 17:35:21 +11:00 committed by Andrew Gerrand
parent 2662b99f39
commit 8bfa9175a3
2 changed files with 16 additions and 3 deletions

View 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. // The function will be called each time the Var is evaluated.
type StringFunc func() string type StringFunc func() string
func (f StringFunc) String() string { return f() } func (f StringFunc) String() string { return strconv.Quote(f()) }
// All published variables. // All published variables.

View File

@ -86,8 +86,8 @@ func TestMapCounter(t *testing.T) {
} }
func TestIntFunc(t *testing.T) { func TestIntFunc(t *testing.T) {
x := int(4) x := int64(4)
ix := IntFunc(func() int64 { return int64(x) }) ix := IntFunc(func() int64 { return x })
if s := ix.String(); s != "4" { if s := ix.String(); s != "4" {
t.Errorf("ix.String() = %v, want 4", s) 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) 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)
}
}