-- Unit tests for . Click talk page to run tests.
local tt = require('Module:TableTools/sandbox') -- the module to be testedlocal ScribuntoUnit = require('Module:ScribuntoUnit')local suite = ScribuntoUnit:new
-------------------------------------------------------------------------- Helper functions------------------------------------------------------------------------
function suite.concatIpairs(t) local ret = for i, v in ipairs(t) do ret = ret .. tostring(v) end return retend
suite.isNan = tt.isNan
function suite:assertErrorEquals(expected, func, ...) local success, msg = pcall(func, ...) self:assertEquals(expected, msg)end
function suite:assertTypeErrorEquals(argId, name, expectedType, actualType, func, ...) local expected = "bad argument #%d to '%s' (%s expected, got %s)" expected = expected:format(argId, name, expectedType, actualType) self:assertErrorEquals(expected, func, ...)end
-------------------------------------------------------------------------- Test isPositiveInteger------------------------------------------------------------------------
function suite:testIsPositiveInteger self:assertTrue(tt.isPositiveInteger(1)) self:assertTrue(tt.isPositiveInteger(2)) self:assertTrue(tt.isPositiveInteger(2323874623)) self:assertFalse(tt.isPositiveInteger(0)) self:assertFalse(tt.isPositiveInteger(-1)) self:assertFalse(tt.isPositiveInteger(0.5)) self:assertFalse(tt.isPositiveInteger(1.5)) self:assertFalse(tt.isPositiveInteger('1')) self:assertFalse(tt.isPositiveInteger(math.huge)) self:assertFalse(tt.isPositiveInteger('foo'))end
-------------------------------------------------------------------------- Test isNan------------------------------------------------------------------------
function suite:testIsNan self:assertTrue(tt.isNan(0/0)) self:assertTrue(tt.isNan(-(0/0))) self:assertTrue(tt.isNan(math.huge * 0)) self:assertTrue(tt.isNan(-math.huge * 0)) self:assertTrue(tt.isNan(-1 * math.huge * 0)) self:assertFalse(tt.isNan(0)) self:assertFalse(tt.isNan(1)) self:assertFalse(tt.isNan(6)) self:assertFalse(tt.isNan(-100)) self:assertFalse(tt.isNan(2^0.5)) self:assertFalse(tt.isNan(99999999999999999999999999999999999999999999999)) self:assertFalse(tt.isNan(math.pi))end
-------------------------------------------------------------------------- Test removeDuplicates------------------------------------------------------------------------
function suite:testRemoveDuplicates local dupes = -- Dupes removed: local removed = tt.removeDuplicates(dupes) self:assertEquals(7, #removed) self:assertEquals(1, removed[1]) self:assertEquals(2, removed[2]) self:assertEquals(3, removed[3]) self:assertEquals(4, removed[4]) self:assertTrue(suite.isNan(removed[5])) self:assertEquals(5, removed[6]) self:assertTrue(suite.isNan(removed[7])) self:assertTypeErrorEquals(1, 'removeDuplicates', 'table', 'number', tt.removeDuplicates, 4) self:assertTypeErrorEquals(1, 'removeDuplicates', 'table', 'nil', tt.removeDuplicates)end
-------------------------------------------------------------------------- Sparse array variables------------------------------------------------------------------------
local sparseArray = local sparseArrayConcatenated = '123'local numKeysConcatenated = '135'local reverseSparseArrayConcatenated = '321'local reverseNumKeysConcatenated = '531'
-------------------------------------------------------------------------- Test numKeys------------------------------------------------------------------------
function suite:testnumKeys local numKeys = tt.numKeys(sparseArray) self:assertEquals(numKeysConcatenated, suite.concatIpairs(numKeys)) self:assertTypeErrorEquals(1, 'numKeys', 'table', 'number', tt.numKeys, 4) self:assertTypeErrorEquals(1, 'numKeys', 'table', 'nil', tt.numKeys)end
-------------------------------------------------------------------------- Test affixNums------------------------------------------------------------------------
local affixArray = local aNumsConcatenated = '1310'local aValsConcatenated = 'a1a3a10'local bNumsConcatenated = '25'local bValsConcatenated = 'b2b5'local cdNumsConcatenated = '48'local cdValsConcatenated = 'c4dc8d'local efNumsConcatenated = '12'local efValsConcatenated = 'e-f12'
function suite.concatAffixVals(t, nums, prefix, suffix) local ret = for i, num in ipairs(nums) do local key = (prefix or ) .. tostring(num) .. (suffix or ) ret = ret .. tostring(t[key]) end return retend
function suite:testaffixNums local aNums = tt.affixNums(affixArray, 'a') local bNums = tt.affixNums(affixArray, 'b') local cdNums = tt.affixNums(affixArray, 'c', 'd') local efNums = tt.affixNums(affixArray, 'e-f') -- "-" is magic, so we need to escape it. self:assertEquals(aNumsConcatenated, suite.concatIpairs(aNums)) self:assertEquals(aValsConcatenated, suite.concatAffixVals(affixArray, aNums, 'a')) self:assertEquals(bNumsConcatenated, suite.concatIpairs(bNums)) self:assertEquals(bValsConcatenated, suite.concatAffixVals(affixArray, bNums, 'b')) self:assertEquals(cdNumsConcatenated, suite.concatIpairs(cdNums)) self:assertEquals(cdValsConcatenated, suite.concatAffixVals(affixArray, cdNums, 'c', 'd')) self:assertEquals(efNumsConcatenated, suite.concatIpairs(efNums)) self:assertEquals(efValsConcatenated, suite.concatAffixVals(affixArray, efNums, 'e-f')) self:assertTypeErrorEquals(1, 'affixNums', 'table', 'number', tt.affixNums, 4) self:assertTypeErrorEquals(1, 'affixNums', 'table', 'nil', tt.affixNums)end
-------------------------------------------------------------------------- Test numData------------------------------------------------------------------------
function suite:testNumData local t = local uncompressed = tt.numData(t) local compressed = tt.numData(t, true)
-- Test uncompressed. self:assertEquals(1, uncompressed[1][1]) self:assertEquals(2, uncompressed[2][1]) self:assertEquals(5, uncompressed[5][1]) self:assertEquals("foo", uncompressed.other.foo) self:assertEquals("foo1", uncompressed[1].foo) self:assertEquals("bar1", uncompressed[1].bar) self:assertEquals("foo6", uncompressed[6].foo) self:assertEquals("bar6", uncompressed[6].bar) self:assertEquals(nil, uncompressed[4])
-- Test compressed. self:assertEquals(1, compressed[1][1]) self:assertEquals(2, compressed[2][1]) self:assertEquals(5, compressed[3][1]) self:assertEquals("foo", compressed.other.foo) self:assertEquals("foo1", compressed[1].foo) self:assertEquals("bar1", compressed[1].bar) self:assertEquals("foo6", compressed[4].foo) self:assertEquals("bar6", compressed[4].bar) self:assertEquals(nil, compressed[5])end
-------------------------------------------------------------------------- Test sparse array functions------------------------------------------------------------------------
function suite:testCompressSparseArray local compressedArray = tt.compressSparseArray(sparseArray) self:assertEquals(sparseArrayConcatenated, suite.concatIpairs(compressedArray)) self:assertTypeErrorEquals(1, 'compressSparseArray', 'table', 'number', tt.compressSparseArray, 4) self:assertTypeErrorEquals(1, 'compressSparseArray', 'table', 'nil', tt.compressSparseArray)end
function suite:testSparseIpairs local arrayText = for i, v in tt.sparseIpairs(sparseArray) do arrayText = arrayText .. tostring(v) end self:assertEquals(sparseArrayConcatenated, arrayText) self:assertTypeErrorEquals(1, 'sparseIpairs', 'table', 'number', tt.sparseIpairs, 4) self:assertTypeErrorEquals(1, 'sparseIpairs', 'table', 'nil', tt.sparseIpairs)end
-------------------------------------------------------------------------- Test size function------------------------------------------------------------------------
function suite:testSize self:assertEquals(0, tt.size) self:assertEquals(3, tt.size) self:assertEquals(1, tt.size) self:assertEquals(5, tt.size) self:assertTypeErrorEquals(1, 'size', 'table', 'number', tt.size, 4) self:assertTypeErrorEquals(1, 'size', 'table', 'nil', tt.size)end
-------------------------------------------------------------------------- Test binaryLength function------------------------------------------------------------------------
function suite:testLengthOfEmptyTableIsZero self:assertEquals(0, tt.length)end
function suite:testLengthOfArray local data = self:assertEquals(6, tt.length(data)) local meta = setmetatable self:assertEquals(6, tt.length(meta))end
function suite:testLengthOfQuasiArrayWithPrefixes local data = self:assertEquals(3, tt.length(data, "key")) local meta = setmetatable self:assertEquals(3, tt.length(meta, "key"))end
function suite:testLengthChecksForTypeErrors self:assertTypeErrorEquals(1, 'length', 'table', 'number', tt.length, 1) self:assertTypeErrorEquals(2, 'length', 'string', 'number', tt.length,, 2)end
function suite:testInArray self:assertTrue(tt.inArray("b")) self:assertTrue(tt.inArray("b", 0)) self:assertTrue(tt.inArray("b", 1)) self:assertTrue(tt.inArray("b", 2)) self:assertTrue(tt.inArray("b", -3)) self:assertTrue(tt.inArray("b", -5)) self:assertTrue(tt.inArray("b")) self:assertTrue(tt.inArray("b", 0)) self:assertTrue(tt.inArray("b")) self:assertFalse(tt.inArray) -- value missing self:assertFalse(tt.inArray("e")) -- "e" is not in array self:assertFalse(tt.inArray("b", 3)) -- "b" is before position 3 self:assertFalse(tt.inArray("b", 5)) -- 5 is larger than #array self:assertFalse(tt.inArray("b", -2)) -- "b" is not in the last two positions self:assertFalse(tt.inArray("b", 0)) -- key 100 is non-consecutive self:assertFalse(tt.inArray("b", 0)) -- key "second" is not an integerend
function suite:testMerge self:assertDeepEquals(tt.merge) local function f return "foo" end self:assertDeepEquals(tt.merge) self:assertDeepEquals(tt.merge) local function fn tt.merge(2,3) end self:assertThrows(fn, "bad argument #1 to 'merge' (table expected, got number)")end
function suite:testExtend local t = tt.extend(t,) self:assertDeepEquals(t) local function fn tt.extend(t, "banana") end self:assertThrows(fn, "bad argument #2 to 'extend' (table expected, got string)") local function fn tt.extend(fn, t) end self:assertThrows(fn, "bad argument #1 to 'extend' (table expected, got function)")end
return suite