feat: implement eager * operator for String and List to repeat content by integer count
Add `*(count)` method to both String and List classes in wren_core.wren, enabling eager repetition of string characters or list elements. The operator validates that count is a non-negative integer, aborting with a clear error message otherwise. For strings, concatenates the original string count times; for lists, builds a new list by adding all elements count times. Includes comprehensive test cases for normal, zero, and edge-case inputs, plus error tests for negative, non-integer, and non-numeric counts. Also refactors existing list plus tests to use inline expressions and adds a plus_not_iterable error test.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
System.print([1, 2, 3] * 0) // expect: []
|
||||
System.print([1, 2, 3] * 1) // expect: [1, 2, 3]
|
||||
System.print([1, 2, 3] * 4) // expect: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
|
||||
|
||||
// Doesn't modify original list.
|
||||
var a = [1, 2, 3]
|
||||
a * 5
|
||||
System.print(a) // expect: [1, 2, 3]
|
||||
@@ -0,0 +1 @@
|
||||
[1, 2, 3] * -3 // expect runtime error: Count must be a non-negative integer.
|
||||
@@ -0,0 +1 @@
|
||||
[1, 2, 3] * 1.2 // expect runtime error: Count must be a non-negative integer.
|
||||
@@ -0,0 +1 @@
|
||||
[1, 2, 3] * "not num" // expect runtime error: Count must be a non-negative integer.
|
||||
@@ -1,18 +1,11 @@
|
||||
var a = [1, 2, 3]
|
||||
var b = [4, 5, 6]
|
||||
var c = a + b
|
||||
var d = a + (4..6)
|
||||
var e = a + []
|
||||
var f = [] + a
|
||||
var g = [] + []
|
||||
|
||||
System.print(a) // expect: [1, 2, 3]
|
||||
System.print(b) // expect: [4, 5, 6]
|
||||
System.print(c) // expect: [1, 2, 3, 4, 5, 6]
|
||||
System.print(d) // expect: [1, 2, 3, 4, 5, 6]
|
||||
System.print(e) // expect: [1, 2, 3]
|
||||
System.print(f) // expect: [1, 2, 3]
|
||||
System.print(g) // expect: []
|
||||
System.print([1, 2, 3] + [4, 5, 6]) // expect: [1, 2, 3, 4, 5, 6]
|
||||
System.print([1, 2, 3] + (4..6)) // expect: [1, 2, 3, 4, 5, 6]
|
||||
System.print([1, 2, 3] + "abc") // expect: [1, 2, 3, a, b, c]
|
||||
System.print([] + []) // expect: []
|
||||
System.print([1, 2] + []) // expect: [1, 2]
|
||||
System.print([] + [3, 4]) // expect: [3, 4]
|
||||
|
||||
// Doesn't modify original list.
|
||||
var a = [1, 2, 3]
|
||||
a * 5
|
||||
System.print(a) // expect: [1, 2, 3]
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
[1, 2, 3] + 4 // expect runtime error: Num does not implement 'iterate(_)'.
|
||||
@@ -0,0 +1,3 @@
|
||||
System.print("|" + "abc" * 0 + "|") // expect: ||
|
||||
System.print("abc" * 1) // expect: abc
|
||||
System.print("abc" * 4) // expect: abcabcabcabc
|
||||
@@ -0,0 +1 @@
|
||||
"abc" * -3 // expect runtime error: Count must be a non-negative integer.
|
||||
@@ -0,0 +1 @@
|
||||
"abc" * 1.2 // expect runtime error: Count must be a non-negative integer.
|
||||
@@ -0,0 +1 @@
|
||||
"abc" * "not num" // expect runtime error: Count must be a non-negative integer.
|
||||
Reference in New Issue
Block a user