Fix %v formatting (#181)

* Fix %v formatting

* Added Bytes

* Fixed tests.
This commit is contained in:
earncef 2019-04-10 08:15:31 +02:00 committed by Daniel
parent 09009d4040
commit 3eebecb2a4
2 changed files with 16 additions and 4 deletions

View file

@ -46,7 +46,12 @@ func fmtPrintf(args ...objects.Object) (ret objects.Object, err error) {
formatArgs := make([]interface{}, numArgs-1, numArgs-1)
for idx, arg := range args[1:] {
switch arg := arg.(type) {
case *objects.Int, *objects.Float, *objects.Bool, *objects.Char, *objects.String, *objects.Bytes:
formatArgs[idx] = objects.ToInterface(arg)
default:
formatArgs[idx] = arg
}
}
fmt.Printf(format.Value, formatArgs...)
@ -86,7 +91,12 @@ func fmtSprintf(args ...objects.Object) (ret objects.Object, err error) {
formatArgs := make([]interface{}, numArgs-1, numArgs-1)
for idx, arg := range args[1:] {
switch arg := arg.(type) {
case *objects.Int, *objects.Float, *objects.Bool, *objects.Char, *objects.String, *objects.Bytes:
formatArgs[idx] = objects.ToInterface(arg)
default:
formatArgs[idx] = arg
}
}
s := fmt.Sprintf(format.Value, formatArgs...)

View file

@ -6,6 +6,8 @@ func TestFmtSprintf(t *testing.T) {
module(t, `fmt`).call("sprintf", "").expect("")
module(t, `fmt`).call("sprintf", "foo").expect("foo")
module(t, `fmt`).call("sprintf", `foo %d %v %s`, 1, 2, "bar").expect("foo 1 2 bar")
module(t, `fmt`).call("sprintf", "foo %v", `[1, "bar", true]`).expect(`foo [1, "bar", true]`)
module(t, `fmt`).call("sprintf", "foo %v %d", `[1, "bar", true]`, 19).expect(`foo [1, "bar", true] 19`)
module(t, `fmt`).call("sprintf", "foo %v", ARR{1, "bar", true}).expect(`foo [1, "bar", true]`)
module(t, `fmt`).call("sprintf", "foo %v %d", ARR{1, "bar", true}, 19).expect(`foo [1, "bar", true] 19`)
module(t, `fmt`).call("sprintf", "foo %v", MAP{"a": IMAP{"b": IMAP{"c": ARR{1, 2, 3}}}}).expect(`foo {a: {b: {c: [1, 2, 3]}}}`)
module(t, `fmt`).call("sprintf", "%v", IARR{1, IARR{2, IARR{3, 4}}}).expect(`[1, [2, [3, 4]]]`)
}