Jump to content

Module:Repr/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 07:27, 22 February 2021 (add tests for tables with table keys). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Load necessary modules
local mRepr = require('Module:Repr')
local ScribuntoUnit = require('Module:ScribuntoUnit')

-- Initialise test suite
local suite = ScribuntoUnit:new()

local TABLE_WITH_IDENTIFIER_KEYS = {e = "eee", c = "ccc", a = "aaa", d = "ddd", b = "bbb"}
local TABLE_WITH_IDENTIFIER_KEYS_SORTED_REPR = '{a = "aaa", b = "bbb", c = "ccc", d = "ddd", e = "eee"}'

local reprTestData = {
	{
		testName = "testRepr_nil",
		args = {nil, {}},
		expected = 'nil',
	},
	{
		testName = "testRepr_true",
		args = {true, {}},
		expected = 'true',
	},
	{
		testName = "testRepr_false",
		args = {false, {}},
		expected = 'false',
	},
	{
		testName = "testRepr_integer",
		args = {7, {}},
		expected = '7',
	},
	{
		testName = "testRepr_float",
		args = {3.14159, {}},
		expected = '3.14159',
	},
	{
		testName = "testRepr_largeInt",
		args = {12345678901234567890, {}},
		expected = '1.2345678901235e+19',
	},
	{
		testName = "testRepr_positiveInfinity",
		args = {math.huge, {}},
		expected = 'math.huge',
	},
	{
		testName = "testRepr_negativeInfinity",
		args = {-math.huge, {}},
		expected = '-math.huge',
	},
	{
		testName = "testRepr_NaN",
		args = {0/0, {}},
		expected = '-nan',
	},
	{
		testName = "testRepr_emptyString",
		args = {"", {}},
		expected = '""',
	},
	{
		testName = "testRepr_normalString",
		args = {"foo", {}},
		expected = '"foo"',
	},
	{
		testName = "testRepr_stringWithQuotes",
		args = {'"foo"', {}},
		expected = '"\\"foo\\""',
	},
	{
		testName = "testRepr_stringWithNewline",
		args = {'foo\nbar', {}},
		expected = '"foo\\nbar"',
	},
	{
		testName = "testRepr_emptyTable",
		args = {{}, {}},
		expected = '{}',
	},
	{
		testName = "testRepr_sequenceWithSimpleValues",
		args = {{1, "foo", true}, {}},
		expected = '{1, "foo", true}',
	},
	{
		testName = "testRepr_sequenceWithSimpleValues",
		args = {{1, "foo", true}, {}},
		expected = '{1, "foo", true}',
	},
	{
		testName = "testRepr_sparseSequence",
		args = {{1, nil, 3, nil, 5}, {}},
		expected = '{1, nil, 3, nil, 5}',
	},
	{
		testName = "testRepr_tableWithZeroKey",
		args = {{[0] = 0}, {}},
		expected = '{[0] = 0}',
	},
	{
		testName = "testRepr_tableWithNegativeIntegerKey",
		args = {{[-1] = -1}, {}},
		expected = '{[-1] = -1}',
	},
	{
		testName = "testRepr_tableWithFloatKeys",
		args = {{[3.14159] = "pi"}, {}},
		expected = '{[3.14159] = "pi"}',
	},
	{
		testName = "testRepr_tableWithHugeKey",
		args = {{[math.huge] = "huge"}, {}},
		expected = '{[math.huge] = "huge"}',
	},
	{
		testName = "testRepr_tableWithIdentifierKeys",
		args = {{a = "b", c = "d"}, {}},
		expected = '{a = "b", c = "d"}',
	},
	{
		testName = "testRepr_tableWithNonIdentifierKeys",
		args = {{["1foo"] = "foo", ["2bar"] = "bar"}, {}},
		expected = '{["1foo"] = "foo", ["2bar"] = "bar"}',
	},
	{
		testName = "testRepr_tableWithLuaKeywordKey",
		args = {{["break"] = "break"}, {}},
		expected = '{["break"] = "break"}',
	},
	{
		testName = "testRepr_tableWithBooleanKey",
		args = {{[true] = "the truth"}, {}},
		expected = '{[true] = "the truth"}',
	},
	{
		testName = "testRepr_tableWithEmptyTableKey",
		args = {{[{}] = "a table"}, {}},
		expected = '{[{}] = "a table"}',
	},
	{
		testName = "testRepr_nestedSequence",
		args = {{1, 2, {3, 4, {5}}}, {}},
		expected = '{1, 2, {3, 4, {5}}}',
	},
	{
		testName = "testRepr_nestedTableWithIdentifierKeys",
		args = {{a = {b = {c = "d"}}}, {}},
		expected = '{a = {b = {c = "d"}}}',
	},
	{
		testName = "testRepr_tableWithNestedTableKey",
		args = {{[{a = {b = "b"}}] = "value"}, {}},
		expected = '{[{a = {b = "b"}}] = "value"}',
	},
	{
		testName = "testRepr_sequenceKey",
		args = {{[{1, 2, 3}] = 6}, {}},
		expected = '{[{1, 2, 3}] = 6}',
	},
	{
		testName = "testRepr_tableKeySorting_withExplicitSetting",
		args = {TABLE_WITH_IDENTIFIER_KEYS, {sortKeys=true}},
		expected = TABLE_WITH_IDENTIFIER_KEYS_SORTED_REPR,
	},
	{
		testName = "testRepr_tableKeySorting_withDefaultSetting",
		args = {TABLE_WITH_IDENTIFIER_KEYS},
		expected = TABLE_WITH_IDENTIFIER_KEYS_SORTED_REPR,
	},
	{
		testName = "testRepr_tableKeySorting_withInheritedSetting",
		args = {TABLE_WITH_IDENTIFIER_KEYS, {}},
		expected = TABLE_WITH_IDENTIFIER_KEYS_SORTED_REPR,
	},
}

for _, testData in ipairs(reprTestData) do
	suite[testData.testName] = function(self)
		local result = mRepr.repr(unpack(testData.args))
		self:assertEquals(testData.expected, result)
	end
end

return suite