Module:ListUtil/testcases

local List = require 'Module:ListUtil' local suite = require 'Module:ScribuntoUnit':new

function suite:test_range suite:assertDeepEquals({1, 2, 3}, List.range(1, 3)) suite:assertDeepEquals({7, 8, 9, 10}, List.range(7, 10)) suite:assertDeepEquals({3, 2, 1}, List.range(3, 1)) suite:assertDeepEquals({3}, List.range(3, 3)) suite:assertDeepEquals({1, 3, 5}, List.range(1, 5, 2)) suite:assertDeepEquals({}, List.range(3, 1, 1)) end

function suite:test_generate suite:assertDeepEquals({2, 3, 4, 5, 6}, List.generate(5, function (x) return x + 1 end)) suite:assertDeepEquals({1, 4, 9}, List.generate(3, function (x) return x * x end)) suite:assertDeepEquals({}, List.generate(0, function (x) return x * x end)) end

function suite:test_array suite:assertDeepEquals({1, 1, 1}, List.array(1, 3)) suite:assertDeepEquals({{1, 1}, {1, 1}, {1, 1}}, List.array(1, 3, 2)) suite:assertDeepEquals({{1, 1, 1}, {1, 1, 1}}, List.array(1, 2, 3)) suite:assertDeepEquals({{}, {}, {}}, List.array(nil, 3, 2)) end

function suite:test_any suite:assertTrue(List.any({3, 5, 2}, function (x) return x >= 4 end)) suite:assertTrue(List.any({6, 5, 7}, function (x) return x >= 4 end)) suite:assertFalse(List.any({3, 1, 2}, function (x) return x >= 4 end)) suite:assertFalse(List.any({}, function (x) return x >= 4 end))

suite:assertTrue(List.any({2, 3, 3, 2}, function (x, i) return x < i end)) suite:assertFalse(List.any({2, 3, 3, 5}, function (x, i) return x < i end)) end

function suite:test_all suite:assertFalse(List.all({3, 5, 2}, function (x) return x >= 4 end)) suite:assertTrue(List.all({6, 5, 7}, function (x) return x >= 4 end)) suite:assertFalse(List.all({3, 1, 2}, function (x) return x >= 4 end)) suite:assertTrue(List.all({}, function (x) return x >= 4 end))

suite:assertFalse(List.all({2, 3, 3, 2}, function (x, i) return x >= i end)) suite:assertTrue(List.all({2, 3, 3, 5}, function (x, i) return x >= i end)) end

function suite:test_none suite:assertFalse(List.none({3, 5, 2}, function (x) return x >= 4 end)) suite:assertFalse(List.none({6, 5, 7}, function (x) return x >= 4 end)) suite:assertTrue(List.none({3, 1, 2}, function (x) return x >= 4 end)) suite:assertTrue(List.none({}, function (x) return x >= 4 end))

suite:assertFalse(List.none({2, 3, 3, 2}, function (x, i) return x < i end)) suite:assertTrue(List.none({2, 3, 3, 5}, function (x, i) return x < i end)) end

function suite:test_count suite:assertEquals(1, List.count({0, 1, 2, 2, 0, 2}, 1)) suite:assertEquals(3, List.count({0, 1, 2, 2, 0, 2}, 2)) suite:assertEquals(0, List.count({0, 1, 2, 2, 0, 2}, 3)) suite:assertEquals(0, List.count({}, nil))

local mt = {__eq = function return true end} local v = function return setmetatable({}, mt) end suite:assertEquals(3, List.count({v, v, v}, v)) end

function suite:test_count_if suite:assertEquals(1, List.count_if({3, 5, 2}, function (x) return x >= 4 end)) suite:assertEquals(3, List.count_if({6, 5, 7}, function (x) return x >= 4 end)) suite:assertEquals(0, List.count_if({3, 1, 2}, function (x) return x >= 4 end)) suite:assertEquals(0, List.count_if({}, function (x) return x >= 4 end))

suite:assertEquals(2, List.count_if({3, 5, 2}, function (x, i) return x > i end)) suite:assertEquals(3, List.count_if({3, 5, 4}, function (x, i) return x > i end)) end

function suite:test_find suite:assertDeepEquals({2, 5}, {List.find({3, 5, 2}, 5)}) suite:assertDeepEquals({3, 2}, {List.find({3, 5, 2}, 2)}) suite:assertDeepEquals({}, {List.find({3, 5, 2}, 4)}) suite:assertDeepEquals({}, {List.find({}, nil)})

local mt = {__eq = function return true end} local v = function return setmetatable({}, mt) end suite:assertEquals(1, List.find({v, v}, v))

suite:assertDeepEquals({2, 5}, {List.find({3, 5, 2}, 5, 1)}) suite:assertDeepEquals({2, 5}, {List.find({3, 5, 2}, 5, 2)}) suite:assertDeepEquals({}, {List.find({3, 5, 2}, 5, 3)}) suite:assertDeepEquals({}, {List.find({3, 5, 2}, 5, 4)}) suite:assertDeepEquals({2, 5}, {List.find({3, 5, 2}, 5, -4)}) suite:assertDeepEquals({2, 5}, {List.find({3, 5, 2}, 5, -3)}) suite:assertDeepEquals({2, 5}, {List.find({3, 5, 2}, 5, -2)}) suite:assertDeepEquals({}, {List.find({3, 5, 2}, 5, -1)}) end

function suite:test_compact suite:assertDeepEquals({'a', 'b', 'c'}, List.compact({'a', 'b', 'c'})) suite:assertDeepEquals({'a', 'b', 'c'}, List.compact({'a', [3] = 'b', [5] = 'c'})) suite:assertDeepEquals({'a', 'b', 'c'}, List.compact({[-1] = 'a', [0] = 'b', [1] = 'c'})) suite:assertDeepEquals({'a', 'b'}, List.compact({[2] = 'a', [5] = 'b', c = 'c'})) end

return suite