Abstract List's toString method to a more general join method on Sequence.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
// Handle empty list.
|
||||
IO.print([].join(",") == "") // expect: true
|
||||
|
||||
// Handle a simple list with an empty delimeter.
|
||||
IO.print([1, 2, 3].join("")) // expect: 123
|
||||
|
||||
// Handle a simple list with no separator.
|
||||
IO.print([1, 2, 3].join) // expect: 123
|
||||
|
||||
// Does not quote strings.
|
||||
IO.print([1, "2", true].join(",")) // expect: 1,2,true
|
||||
|
||||
// Nested lists.
|
||||
IO.print([1, [2, [3], 4], 5].join(",")) // expect: 1,[2, [3], 4],5
|
||||
|
||||
// Calls toString on elements.
|
||||
class Foo {
|
||||
toString { "Foo.toString" }
|
||||
}
|
||||
|
||||
IO.print([1, new Foo, 2].join(", ")) // expect: 1, Foo.toString, 2
|
||||
|
||||
// TODO: Handle lists that contain themselves.
|
||||
@@ -0,0 +1 @@
|
||||
[1, 2, 3].join(2) // expect runtime error: Right operand must be a string.
|
||||
@@ -0,0 +1,4 @@
|
||||
var a = 1..3
|
||||
|
||||
IO.print(a.join) // expect: 123
|
||||
IO.print(a.join(", ")) // expect: 1, 2, 3
|
||||
@@ -0,0 +1 @@
|
||||
(1..3).join(2) // expect runtime error: Right operand must be a string.
|
||||
@@ -0,0 +1,5 @@
|
||||
var str = "string"
|
||||
|
||||
IO.print(str.join("") == str) // expect: true
|
||||
|
||||
IO.print(str.join(", ")) // expect: s, t, r, i, n, g
|
||||
@@ -0,0 +1 @@
|
||||
"string".join(2) // // expect runtime error: Right operand must be a string.
|
||||
Reference in New Issue
Block a user