Skip to content

Commit

Permalink
Fix passthrough handling (#37). (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer authored and kr committed Aug 23, 2016
1 parent 737b74a commit cfb55aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Formatter(x interface{}) (f fmt.Formatter) {
}

func (fo formatter) String() string {
return fmt.Sprint(fo.v) // unwrap it
return fmt.Sprint(fo.v.Interface()) // unwrap it
}

func (fo formatter) passThrough(f fmt.State, c rune) {
Expand All @@ -47,7 +47,7 @@ func (fo formatter) passThrough(f fmt.State, c rune) {
s += fmt.Sprintf(".%d", p)
}
s += string(c)
fmt.Fprintf(f, s, fo.v)
fmt.Fprintf(f, s, fo.v.Interface())
}

func (fo formatter) Format(f fmt.State, c rune) {
Expand Down
27 changes: 27 additions & 0 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type test struct {
s string
}

type passtest struct {
v interface{}
f, s string
}

type LongStructTypeName struct {
longFieldName interface{}
otherLongFieldName interface{}
Expand All @@ -33,8 +38,30 @@ func (f F) Format(s fmt.State, c rune) {
fmt.Fprintf(s, "F(%d)", int(f))
}

type Stringer struct { i int }

func (s *Stringer) String() string { return "foo" }

var long = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

var passthrough = []passtest{
{1, "%d", "1"},
{"a", "%s", "a"},
{&Stringer{}, "%s", "foo"},
}

func TestPassthrough(t *testing.T) {
for _, tt := range passthrough {
s := fmt.Sprintf(tt.f, Formatter(tt.v))
if tt.s != s {
t.Errorf("expected %q", tt.s)
t.Errorf("got %q", s)
t.Errorf("expraw\n%s", tt.s)
t.Errorf("gotraw\n%s", s)
}
}
}

var gosyntax = []test{
{nil, `nil`},
{"", `""`},
Expand Down

0 comments on commit cfb55aa

Please sign in to comment.