Documentation for this module may be created at Module:Set/testcases/doc
-- Unit tests for [[Module:Set]]. Click talk page to run tests.
local set = require('Module:Set/sandbox') -- the module to be tested
local tt = require('Module:TableTools') -- contains some necessary functions
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()
-- Dummy NaN constant
local NaN = {}
------------------------------------------------------------------------
-- Helper functions
------------------------------------------------------------------------
suite.isNan = tt.isNan
-- Counts the values in an array. Returns a table with values as keys and counts
-- as values. NaNs are stored as a unique table accessed with the variable NaN.
-- For example, this table:
-- { 'a', 'b', 'c', 'c', 0/0 }
-- Will be converted to this:
-- { a = 1, b = 1, c = 2, [NaN] = 1 }
function suite.getValueCounts(t)
local ret = {}
for i, v in ipairs(t) do
if suite.isNan(v) then
v = NaN
end
if ret[v] then
ret[v] = ret[v] + 1
else
ret[v] = 1
end
end
return ret
end
-- Asset that two arrays have the same values, not necessarily in the same
-- order. If tables contain duplicate values, the number of values must be
-- the same for assertion to succeed.
function suite:assertArraysHaveSameValues(...)
local tables = {...}
if #tables < 2 then
error('assertArraysHaveSameValues needs at least two arguments', 2)
end
local counts = {}
for i, t in ipairs(tables) do
if type(t) ~= 'table' then
error(string.format(
'argument #%d to assertArraysHaveSameValues was not a table',
i
), 2)
end
counts[#counts + 1] = suite.getValueCounts(t)
end
for i = 2, #counts do
suite:assertDeepEquals(counts[1], counts[i])
end
end
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
------------------------------------------------------------------------
-- Make test sets
------------------------------------------------------------------------
function suite.getKeyValueTables()
local t1 = {eggs = 'eggs', beans = 'beans', sausage = 'sausage'}
local t2 = {eggs = 'eggs', beans = 'baked beans', sausage = 'sausage', spam = 'spam'}
local t3 = {lobster = 'lobster thermidor', eggs = 'scrambled eggs', beans = 'beans', sausage = 'sausage'}
local union = {eggs = {'eggs', 'scrambled eggs'}, beans = {'beans', 'baked beans'}, sausage = 'sausage', spam = 'spam', lobster = 'lobster thermidor'}
local intersection = {sausage = 'sausage'}
local complement123 = {lobster = 'lobster thermidor', eggs = 'scrambled eggs'}
local complement321 = {}
return t1, t2, t3, union, intersection, complement123, complement321
end
function suite.getValueNumsTables()
local nums1 = {1, 3, 4, 5, foo = 7}
local nums2 = {2, bar = 3, 5, 6}
local nums3 = {2, 3, 5, 8}
local numsUnion = {1, 2, 3, 4, 5, 6, 7, 8}
local numsIntersection = {3, 5}
local numsComplement = {8}
return nums1, nums2, nums3, numsUnion, numsIntersection, numsComplement
end
function suite.getValueStringsTables()
local strings1 = {'foo', 'bar', 'baz'}
local strings2 = {'eggs', 'sausage', 'spam', 'bar', 'baz'}
local stringsUnion = {'foo', 'eggs', 'sausage', 'spam', 'bar', 'baz'}
local stringsIntersection = {'bar', 'baz'}
local stringsComplement = {'eggs', 'sausage', 'spam'}
return strings1, strings2, stringsUnion, stringsIntersection, stringsComplement
end
function suite.getValueMixedTables()
local mixed1 = {1, 3, 4, 5, foo = 7, 'foo', 'bar', 'baz'}
local mixed2 = {2, bar = 3, 5, 6, 'eggs', 'sausage', 'spam', 'bar', 'baz'}
local mixedUnion = {1, 2, 3, 4, 5, 6, 7, 'foo', 'bar', 'baz', 'eggs', 'sausage', 'spam'}
local mixedIntersection = {3, 5, 'bar', 'baz'}
local mixedComplement = {2, 6, 'eggs', 'sausage', 'spam'}
return mixed1, mixed2, mixedUnion, mixedIntersection, mixedComplement
end
function suite.getValueFalseTables()
local false1 = {1, 3, false, 4}
local false2 = {2, 3, false, 5, 6}
local falseUnion = {1, 2, 3, 4, 5, 6, false}
local falseIntersection = {3, false}
local falseComplement = {2, 5, 6}
return false1, false2, falseUnion, falseIntersection, falseComplement
end
function suite.getValueNansTables()
local nans1 = {1, 3, 0/0, 2}
local nans2 = {5, 2, 3, 0/0, 6}
local nansUnion = {1, 2, 3, 5, 6, 0/0, 0/0} -- We have two 0/0s here as NaNs are never equal to each other.
local nansIntersection = {2, 3} -- 0/0 is not included as a NaN is never equal to another NaN.
local nansComplement = {5, 0/0, 6} -- The 0/0 in nans2 is included as it is a NaN, and therefore not equal to the NaN in nans1.
return nans1, nans2, nansUnion, nansIntersection, nansComplement
end
function suite.getValueDupesTables()
local dupes1 = {'a', 'a', 'b'}
local dupes2 = {'b', 'b', 'c'}
local dupesUnion = {'a', 'b', 'c'}
local dupesIntersection = {'b'}
local dupesComplement = {'c'}
return dupes1, dupes2, dupesUnion, dupesIntersection, dupesComplement
end
------------------------------------------------------------------------
-- Test union
------------------------------------------------------------------------
function suite:testUnion()
local t1, t2, t3, union, intersection = suite.getKeyValueTables()
self:assertDeepEquals(
{beans = {'beans', 'baked beans'}, spam = 'spam', egg = 'egg'},
set.union(
{beans = 'beans'},
{beans = 'baked beans', spam = 'spam'},
{spam = 'spam', egg = 'egg'}
)
)
end
function suite:testUnionErrors()
self:assertErrorEquals("too few arguments to 'union' (minimum is 2, received 0)", set.union)
self:assertErrorEquals("too few arguments to 'union' (minimum is 2, received 1)", set.union, 3)
self:assertTypeErrorEquals(1, 'union', 'table', 'number', set.union, 4, 5)
self:assertTypeErrorEquals(2, 'union', 'table', 'string', set.union, {}, 'foo')
self:assertTypeErrorEquals(1, 'union', 'table', 'nil', set.union, nil, nil)
end
------------------------------------------------------------------------
-- Test intersection
------------------------------------------------------------------------
function suite:testIntersection()
self:assertDeepEquals(
{spam = 'spam'},
set.intersection(
{beans = 'beans', spam = 'spam', egg = 'egg'},
{beans = 'beans', spam = 'spam'},
{beans = 'baked beans', spam = 'spam'}
)
)
end
function suite:testIntersectionErrors()
self:assertErrorEquals("too few arguments to 'intersection' (minimum is 2, received 0)", set.intersection)
self:assertErrorEquals("too few arguments to 'intersection' (minimum is 2, received 1)", set.intersection, 3)
self:assertTypeErrorEquals(1, 'intersection', 'table', 'number', set.intersection, 4, 5)
self:assertTypeErrorEquals(2, 'intersection', 'table', 'string', set.intersection, {}, 'foo')
self:assertTypeErrorEquals(1, 'intersection', 'table', 'nil', set.intersection, nil, nil)
end
------------------------------------------------------------------------
-- Test complement
------------------------------------------------------------------------
function suite.getComplementTables()
return {eggs = 'eggs', beans = 'beans', sausage = 'sausage'},
{eggs = 'eggs', beans = 'baked beans', sausage = 'sausage', spam = 'spam'},
{eggs = 'scrambled eggs', beans = 'beans', sausage = 'sausage', lobster = 'lobster thermidor'}
end
function suite:testComplementAscending()
local t1, t2, t3 = suite.getComplementTables()
self:assertDeepEquals(
{lobster = 'lobster thermidor', eggs = 'scrambled eggs'},
set.complement(t1, t2, t3)
)
end
function suite:testComplementDescending()
local t1, t2, t3 = suite.getComplementTables()
self:assertDeepEquals(
{},
set.complement(t3, t2, t1)
)
end
function suite:testComplementErrors()
self:assertErrorEquals("too few arguments to 'complement' (minimum is 2, received 0)", set.complement)
self:assertErrorEquals("too few arguments to 'complement' (minimum is 2, received 1)", set.complement, 3)
self:assertTypeErrorEquals(2, 'complement', 'table', 'string', set.complement, {}, 'foo')
self:assertTypeErrorEquals(3, 'complement', 'table', 'nil', set.complement, {}, {}, nil, {})
end
------------------------------------------------------------------------
-- Test valueUnion
------------------------------------------------------------------------
function suite:testValueUnion()
self:assertArraysHaveSameValues(
{1, 2, 3, 4},
set.valueUnion({1, foo = 4}, {2}, {1, 3})
)
end
function suite:testValueUnionDuplicates()
self:assertArraysHaveSameValues(
{1, 2, 3},
set.valueUnion({1, 1, 2}, {2, 3})
)
end
function suite:testValueUnionNans()
self:assertArraysHaveSameValues(
{1, 2, 0/0, 0/0}, -- We have two 0/0s here as NaNs are never equal to each other.
set.valueUnion({1, 0/0}, {2, 0/0})
)
end
function suite:testValueUnionErrors()
self:assertErrorEquals("too few arguments to 'valueUnion' (minimum is 2, received 0)", set.valueUnion)
self:assertErrorEquals("too few arguments to 'valueUnion' (minimum is 2, received 1)", set.valueUnion, 3)
self:assertTypeErrorEquals(1, 'valueUnion', 'table', 'number', set.valueUnion, 4, 4)
self:assertTypeErrorEquals(2, 'valueUnion', 'table', 'string', set.valueUnion, {}, 'foo')
self:assertTypeErrorEquals(1, 'valueUnion', 'table', 'nil', set.valueUnion, nil, nil)
end
------------------------------------------------------------------------
-- Test valueIntersection
------------------------------------------------------------------------
function suite:testValueIntersection()
self:assertArraysHaveSameValues(
{3, 5},
set.valueIntersection(
{1, 3, 4, 5},
{2, bar = 3, 5},
{2, 3, 5}
)
)
end
function suite:testValueIntersectionDuplicates()
self:assertArraysHaveSameValues(
{2},
set.valueIntersection({1, 1, 2}, {2, 3})
)
end
function suite:testValueIntersectionNans()
self:assertArraysHaveSameValues(
{2}, -- 0/0 is not included as a NaN is never equal to another NaN.
set.valueIntersection({1, 0/0, 2}, {2, 3, 0/0})
)
end
function suite:testValueIntersectionSameTable()
local t = {1, 2, foo = 3, 0/0, 0/0}
self:assertArraysHaveSameValues(
{1, 2, 3, 0/0, 0/0}, -- The intersection of a value set with itself is itself, but normalized to an array
set.valueIntersection(t, t)
)
end
function suite:testValueIntersectionErrors()
self:assertErrorEquals("too few arguments to 'valueIntersection' (minimum is 2, received 0)", set.valueIntersection)
self:assertErrorEquals("too few arguments to 'valueIntersection' (minimum is 2, received 1)", set.valueIntersection, 3)
self:assertTypeErrorEquals(1, 'valueIntersection', 'table', 'number', set.valueIntersection, 4, 5)
self:assertTypeErrorEquals(2, 'valueIntersection', 'table', 'string', set.valueIntersection, {}, 'foo')
self:assertTypeErrorEquals(1, 'valueIntersection', 'table', 'nil', set.valueIntersection, nil, nil)
end
------------------------------------------------------------------------
-- Test valueComplement
------------------------------------------------------------------------
function suite:testValueComplement()
self:assertArraysHaveSameValues(
{8},
set.valueComplement(
{1, 3, 4, 5, foo = 7},
{2, bar = 3, 5, 6},
{2, 3, 5, 8}
)
)
end
function suite:testValueComplementDuplicates()
self:assertArraysHaveSameValues(
{3},
set.valueComplement({1, 1, 2}, {2, 3, 3})
)
end
function suite:testValueComplementNans()
self:assertArraysHaveSameValues(
{3, 0/0}, -- 0/0 is included as a NaN is never equal to another NaN.
set.valueComplement({1, 0/0, 2}, {2, 3, 0/0})
)
end
function suite:testValueComplementErrors()
self:assertErrorEquals("too few arguments to 'valueComplement' (minimum is 2, received 0)", set.valueComplement)
self:assertErrorEquals("too few arguments to 'valueComplement' (minimum is 2, received 1)", set.valueComplement, 3)
self:assertTypeErrorEquals(1, 'valueComplement', 'table', 'number', set.valueComplement, 4, 5)
self:assertTypeErrorEquals(2, 'valueComplement', 'table', 'string', set.valueComplement, {}, 'foo')
self:assertTypeErrorEquals(1, 'valueComplement', 'table', 'nil', set.valueComplement, nil, nil)
end
return suite
