extend("test")
extend("array")
describe("array:nth", () => {
it("returns the element at the given index", () => {
expect_equal( array:nth(0, ["A","B","C"]), "A" )
expect_equal( array:nth(2, ["A","B","C"]), "C" )
})
it("supports negative indices", () => {
expect_equal( array:nth(-1, ["X","Y","Z"]), "Z" )
expect_equal( array:nth(-2, ["X","Y","Z"]), "Y" )
})
it("returns '_' if out of range", () => {
result1 = array:nth(5, [10,20,30])
expect_equal(result1, _)
result2 = array:nth(-4, [10,20,30])
expect_equal(result2, _)
})
it("handles partial usage (index only first)", () => {
getThird = array:nth(2)
expect_equal( getThird([100,200,300,400]), 300 )
})
it("handles partial usage with underscore for the first argument", () => {
nthFromList = array:nth(_, [10,11,12])
expect_equal( nthFromList(1), 11 )
expect_equal( nthFromList(-1), 12 )
})
})