Module:Array/testcases explained

local export =

local Array = require "Module:array"

local function collect(iter) local vals = Array for val in iter do vals:insert(val) end return valsend

local total_tests, successes = 0, 0local function assert(val) total_tests = total_tests + 1 if val then successes = successes + 1 endend

function export.run_tests local ok, ret = pcall(function assert(Array(1, 2, 3):map(function (x) return x * x end):concat ", "

"1, 4, 9") assert(Array(1, 2, 3, 4, 5):filter(function (x) return x % 2

0 end):concat ", "

"2, 4") assert(Array:type

"array") assert(Array(1, nil, 2, nil, 3):compress:concat ", "

"1, 2, 3") assert(collect(Array(1, nil, 2, nil, 3):sparseIpairs):concat ", "

"1, 3, 5") assert(Array("a", "b", "c"):toSet["a"]

true) assert(Array("a", "b", "c"):to_set["a"]

true) assert(Array(2, 4, 8):all(function (x) return x % 2

0 end)

true) assert(Array(1, 2, 3):some(function (x) return x % 2

0 end)

true) assert(Array(1, 2, 3, nil, 4):length

3) assert(Array(1, 2, 3, nil, 4):contains(3)

true) assert(Array(1, 2, 3, nil, 4):contains(4)

false) assert(Array.keysToList(Array("a", "b", "c"):invert):concat ", "

"a, b, c") assert(Array(1, nil, 2, nil, 3):maxIndex

5) assert(Array(1, nil, 2, nil, 3):max_index

5) assert(Array(1, nil, 2, nil, 3):num_keys:concat ", "

"1, 3, 5") assert(collect(Array("a", "b", "c"):reverseIpairs):concat ", "

"3, 2, 1") assert(Array("a", "b", "c"):reverse:concat ", "

"c, b, a") assert(Array("a", "b", "c"):serial_comma_join

"a, b, and c") assert(Array(1, 1, 2, 3, 1, 2, 3):removeDuplicates:concat ", "

"1, 2, 3") assert(Array(1, 2, 3, 4, 5):slice(1, 3):concat ", "

"1, 2, 3") assert(Array(1, 2, 3, 4, 5):slice(1, -3):concat ", "

"1, 2, 3") assert(Array(1, 2, 3):adjust_index(-1)

3) assert((Array(1, 2, 3) + Array(4, 5, 6)):concat ", "

"1, 2, 3, 4, 5, 6") end) local err if not ok then err = ret end local color = successes < total_tests and "red" or "green" return ('

%d out of %d assertions passed%s') :format(color, successes, total_tests, err and '\n\n' .. err .. '' or "")end

return export