Make IO.write() call toString on its argument.

This commit is contained in:
Bob Nystrom
2013-12-22 11:50:00 -08:00
parent 2ec01c558f
commit 7868287f89
6 changed files with 130 additions and 12 deletions

12
test/io/write.wren Normal file
View File

@ -0,0 +1,12 @@
class Foo {
toString { return "Foo.toString" }
}
// Calls toString on argument.
IO.write(new Foo) // expect: Foo.toString
// Returns argument.
var result = IO.write(123) // expect: 123
IO.write(result == 123) // expect: true
// TODO: What if toString on argument doesn't return a string?

17
test/list/to_string.wren Normal file
View File

@ -0,0 +1,17 @@
// Handle empty list.
IO.write([].toString) // expect: []
// Does not quote strings.
IO.write([1, "2", true].toString) // expect: [1, 2, true]
// Nested lists.
IO.write([1, [2, [3], 4], 5]) // expect: [1, [2, [3], 4], 5]
// Calls toString on elements.
class Foo {
toString { return "Foo.toString" }
}
IO.write([1, new Foo, 2]) // expect: [1, Foo.toString, 2]
// TODO: Handle lists that contain themselves.

View File

@ -0,0 +1,2 @@
class Foo {}
IO.write((new Foo).toString) // expect: <object>