Module:Array/testcases
MyWikiBiz, Author Your Legacy — Tuesday November 19, 2024
< Module:Array
Jump to navigationJump to searchRevision as of 19:20, 8 July 2021 by Zoran (talk | contribs) (Moved page from wikipedia:en:Module:Array/testcases)
Documentation for this module may be created at Module:Array/testcases/doc
local export = {} local Array = require "Module:array" local function collect(iter) local vals = Array() for val in iter do vals:insert(val) end return vals end local total_tests, successes = 0, 0 local function assert(val) total_tests = total_tests + 1 if val then successes = successes + 1 end end 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 { dontTag = true } == "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 ('<span style="color: %s;">%d out of %d assertions passed</span>%s') :format(color, successes, total_tests, err and '\n\n<span class="error">' .. err .. '</span>' or "") end return export