extend("test")
extend("array")
extend("string")
describe("Array index assignment => arr[idx] = value", () => {
it("updates an existing int array element", () => {
arr = [10, 20, 30]
arr[1] = 999
expect_equal(arr, [10, 999, 30])
})
it("updates multiple elements in sequence", () => {
arr = [1,2,3,4]
arr[0] = 10
arr[3] = 40
expect_equal(arr, [10,2,3,40])
})
/*
it("fails if the index is out of range (positive)", () => {
arr = [1,2,3]
# the interpret should raise a runtime error => we can catch with test:expect_error:
test:expect_error( () => {
arr[3] = 99
})
expect_equal(arr, [1,2,3]) # unchanged
})
it("fails if the index is negative (not supported in assignment by default)", () => {
arr = [7,8,9]
test:expect_error( () => {
arr[-1] = 1234
})
expect_equal(arr, [7,8,9])
})
it("fails if the new value type doesn't match the array type", () => {
arr = [10,20,30]
test:expect_error( () => {
arr[2] = "hello" # string into int array => error
})
expect_equal(arr, [10,20,30])
})
it("works for a string array if we store a string", () => {
words = ["alpha","beta","gamma"]
words[1] = "BETTER"
expect_equal(words, ["alpha","BETTER","gamma"])
})
it("cannot store an int into a string array", () => {
words = ["One","Two"]
test:expect_error( () => {
words[0] = 999
})
expect_equal(words, ["One","Two"])
})
*/
})