Module:Util/testcases: Difference between revisions
Jump to navigation
Jump to search
>OmegaK2 No edit summary |
No edit summary |
||
(14 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
local util = require('Module:Util') -- the module to be tested | local util = require('Module:Util/sandbox') -- the module to be tested | ||
local ScribuntoUnit = require('Module:ScribuntoUnit') | local ScribuntoUnit = require('Module:ScribuntoUnit') | ||
local suite = ScribuntoUnit:new() | local suite = ScribuntoUnit:new() | ||
Line 44: | Line 44: | ||
function suite:test_cast_number__invalid_with_default() | function suite:test_cast_number__invalid_with_default() | ||
self:assertEquals(5, util.cast.number('wrong', {default=5})) | self:assertEquals(5, util.cast.number('wrong', {default=5})) | ||
end | |||
function suite:test_cast_replace_if_match__stringmatchpresent() | |||
self:assertEquals('DEF', util.cast.replace_if_match('ABC', {pattern='B', replacewith='DEF'})) | |||
end | |||
function suite:test_cast_replace_if_match__stringmatchnotpresent() | |||
self:assertEquals('ABC', util.cast.replace_if_match('ABC', {pattern='D', replacewith='DEF'})) | |||
end | |||
function suite:test_cast_replace_if_match__numericmatchpresent() | |||
self:assertEquals(456, util.cast.replace_if_match(123, {pattern='2', replacewith=456})) | |||
end | |||
function suite:test_cast_replace_if_match__numericmatchnotpresent() | |||
self:assertEquals(123, util.cast.replace_if_match(123, {pattern='4', replacewith=456})) | |||
end | |||
function suite:test_cast_replace_if_match__argsnotpresent() | |||
self:assertEquals('ABC', util.cast.replace_if_match('ABC')) | |||
end | |||
function suite:test_cast_replace_if_match__patternnotpresent() | |||
self:assertEquals('ABC', util.cast.replace_if_match('ABC', {replacewith='DEF'})) | |||
end | |||
function suite:test_cast_replace_if_match__replacewithnotpresent() | |||
self:assertEquals(nil, util.cast.replace_if_match('ABC', {pattern='B'})) | |||
end | |||
function suite:test_cast_replace_if_match__ignorequestionmark_match() | |||
self:assertEquals(nil, util.cast.replace_if_match('?', {pattern='^%?$'})) | |||
end | |||
function suite:test_cast_replace_if_match__ignorequestionmark_nomatch() | |||
self:assertEquals('Will this match? No', util.cast.replace_if_match('Will this match? No', {pattern='^%?$'})) | |||
end | |||
function suite:test_cast_table_from_table() | |||
local testtable = { [1] = 23, myval = "foo" } | |||
self:assertDeepEquals(testtable, util.cast.table(testtable)) | |||
end | |||
function suite:test_cast_table_from_string() | |||
local test_table = { "23", "foo" } | |||
local test_table_str = "23, foo" | |||
self:assertDeepEquals(test_table, util.cast.table(test_table_str)) | |||
end | |||
function suite:test_cast_table_from_string_custom_delimiter() | |||
local test_table = { "23", "foo" } | |||
local test_table_str = "23/foo" | |||
self:assertDeepEquals(test_table, util.cast.table(test_table_str, {pattern = '/'})) | |||
end | |||
function suite:test_cast_table_from_split_args_string() | |||
local test_table = { myval = '23', myotherval = "foo" } | |||
local test_table_str = "myval=23, myotherval = foo" | |||
self:assertDeepEquals(test_table, util.cast.table(test_table_str, {split_args = true})) | |||
end | |||
function suite:test_cast_table_from_split_args_string_custom_delimiter() | |||
local test_table = { myval = '23', myotherval = "foo" } | |||
local test_table_str = "myval:23/myotherval:foo" | |||
self:assertDeepEquals(test_table, util.cast.table(test_table_str, {split_args = true, pattern = '/', split_args_pattern = ':'})) | |||
end | end | ||
Line 72: | Line 131: | ||
end | end | ||
-- | |||
-- Tests: util.args.from_cargo_map | |||
-- | |||
function suite:test_args_from_cargo_maps__cast_valid_fields() | |||
local tpl_args = { | |||
string = 'string', | |||
integer = '12345', | |||
float = '12.345', | |||
boolean_true = 'true', | |||
boolean_false = 'false', | |||
} | |||
local tpl_args_expected = { | |||
string = 'string', | |||
integer = 12345, | |||
float = 12.345, | |||
boolean_true = true, | |||
boolean_false = false, | |||
} | |||
util.args.from_cargo_map{ | |||
tpl_args=tpl_args, | |||
frame=mw.getCurrentFrame(), | |||
table_map={ | |||
table = 'test', | |||
order = {'string', 'integer', 'float', 'boolean_true', 'boolean_false'}, | |||
fields = { | |||
string = { | |||
type = 'String', | |||
field = 'string', | |||
}, | |||
integer = { | |||
type = 'Integer', | |||
field = 'integer', | |||
}, | |||
float = { | |||
type = 'Float', | |||
field = 'float', | |||
}, | |||
boolean_true = { | |||
type = 'Boolean', | |||
field = 'boolean_true', | |||
}, | |||
boolean_false = { | |||
type = 'Boolean', | |||
field = 'boolean_false', | |||
}, | |||
}, | |||
} | |||
} | |||
self:assertDeepEquals(tpl_args_expected, tpl_args, 'Casting / parsing of valid fields failed') | |||
end | |||
function suite:test_args_from_cargo_maps__default_value() | |||
local tpl_args = { | |||
} | |||
local tpl_args_expected = { | |||
default = 5, | |||
} | |||
util.args.from_cargo_map{ | |||
tpl_args=tpl_args, | |||
frame=mw.getCurrentFrame(), | |||
table_map={ | |||
table = 'test', | |||
order = {'default'}, | |||
fields = { | |||
default = { | |||
type = 'Integer', | |||
field = 'default', | |||
default = 5 | |||
}, | |||
}, | |||
} | |||
} | |||
self:assertDeepEquals(tpl_args_expected, tpl_args, 'Setting default values for nil fields failed') | |||
end | |||
function suite:test_args_from_cargo_maps__default_function() | |||
local tpl_args = { | |||
} | |||
local tpl_args_expected = { | |||
default = 5, | |||
} | |||
util.args.from_cargo_map{ | |||
tpl_args=tpl_args, | |||
frame=mw.getCurrentFrame(), | |||
table_map={ | |||
table = 'test', | |||
order = {'default'}, | |||
fields = { | |||
default = { | |||
type = 'Integer', | |||
field = 'default', | |||
default = function (tpl_args, frame) | |||
return 5 | |||
end | |||
}, | |||
}, | |||
} | |||
} | |||
self:assertDeepEquals(tpl_args_expected, tpl_args, 'Setting default values via function for nil fields failed') | |||
end | |||
function suite:test_args_from_cargo_maps__func() | |||
local tpl_args = { | |||
custom = 10 | |||
} | |||
local tpl_args_expected = { | |||
custom = 5, | |||
} | |||
util.args.from_cargo_map{ | |||
tpl_args=tpl_args, | |||
frame=mw.getCurrentFrame(), | |||
table_map={ | |||
table = 'test', | |||
order = {'custom'}, | |||
fields = { | |||
custom = { | |||
type = 'Integer', | |||
field = 'custom', | |||
func = function (tpl_args, frame) | |||
return tpl_args.custom / 2 | |||
end | |||
}, | |||
}, | |||
} | |||
} | |||
self:assertDeepEquals(tpl_args_expected, tpl_args, 'Parsing via func failed') | |||
end | |||
function suite:test_args_from_cargo_maps__name() | |||
local tpl_args = { | |||
different_name = '5' | |||
} | |||
local tpl_args_expected = { | |||
different_name = '5', | |||
custom = 5, | |||
} | |||
util.args.from_cargo_map{ | |||
tpl_args=tpl_args, | |||
frame=mw.getCurrentFrame(), | |||
table_map={ | |||
table = 'test', | |||
order = {'custom'}, | |||
fields = { | |||
custom = { | |||
type = 'Integer', | |||
field = 'custom', | |||
name = 'different_name', | |||
}, | |||
}, | |||
} | |||
} | |||
self:assertDeepEquals(tpl_args_expected, tpl_args, 'Parsing from a different name in tpl_args failed') | |||
end | |||
function suite:test_args_from_cargo_maps__error_missing_in_order() | |||
local tpl_args = { | |||
custom = '5', | |||
} | |||
self:assertFalse(pcall(util.args.from_cargo_map, {}, { | |||
tpl_args=tpl_args, | |||
frame=mw.getCurrentFrame(), | |||
table_map={ | |||
table = 'test', | |||
order = {}, | |||
fields = { | |||
custom = { | |||
type = 'Integer', | |||
field = 'custom', | |||
}, | |||
}, | |||
} | |||
}), 'Field that is present in fields but missing in order should raise an error') | |||
end | |||
function suite:test_args_from_cargo_maps__error_missing_in_fields() | |||
local tpl_args = { | |||
custom = '5', | |||
} | |||
self:assertFalse(pcall(util.args.from_cargo_map, {}, { | |||
tpl_args=tpl_args, | |||
frame=mw.getCurrentFrame(), | |||
table_map={ | |||
table = 'test', | |||
order = {'custom'}, | |||
fields = { | |||
}, | |||
} | |||
}), 'Field that is present in order but missing in fields should raise an error') | |||
end | |||
-- ---------------------------------------------------------------------------- | |||
-- Tests: util.args.version | |||
-- ---------------------------------------------------------------------------- | |||
function suite:test_args_version__success() | |||
local tpl_argtbl = { | |||
removal_version='3.16.0', | |||
ignore_this='123', | |||
release_version='3.14.0' | |||
} | |||
local tpl_args = { | |||
frame=mw.getCurrentFrame(), | |||
variables = { | |||
release = {value = '', property='release_version_property'}, | |||
removal = {value = '', property='removal_version_property'}, | |||
}, | |||
noquery = 1, | |||
return_ids_and_keys = 1 | |||
} | |||
local tpl_returned_ids_expected = {'3.14.0', '3.16.0'} | |||
local tpl_returned_keys_expected = {['3.14.0']='release', ['3.16.0']='removal'} | |||
util.args.version(tpl_argtbl, tpl_args) | |||
self:assertDeepEquals(tpl_returned_keys_expected, tpl_args.version_keys) | |||
self:assertDeepEquals(tpl_returned_ids_expected, tpl_args.version_ids) | |||
end | |||
function suite:test_args_version__misformatted_release_version() | |||
local tpl_argtbl = { | |||
removal_version='3.16.0', | |||
ignore_this='123', | |||
release_version='3.14' | |||
} | |||
local tpl_args = { | |||
frame=mw.getCurrentFrame(), | |||
variables = { | |||
release = {value = '', property='release_version_property'}, | |||
removal = {value = '', property='removal_version_property'}, | |||
}, | |||
noquery = 1, | |||
return_ids_and_keys = 1 | |||
} | |||
local succeeded, actualMessage = pcall(util.args.version, tpl_argtbl, tpl_args) | |||
self:assertEquals(false, succeeded) | |||
self:assertStringContains('"3.14" is not a recognized version number', actualMessage, true) | |||
end | |||
function suite:test_args_version__ignore_unknowns_success() | |||
local tpl_argtbl = { | |||
removal_version='?', | |||
ignore_this='123', | |||
release_version='3.14.0' | |||
} | |||
local tpl_args = { | |||
frame=mw.getCurrentFrame(), | |||
variables = { | |||
release = {value = '', property='release_version_property'}, | |||
removal = {value = '', property='removal_version_property'}, | |||
}, | |||
noquery = 1, | |||
return_ids_and_keys = 1, | |||
} | |||
local succeeded, actualMessage = pcall(util.args.version, tpl_argtbl, tpl_args) | |||
self:assertEquals(false, succeeded) | |||
self:assertStringContains('"?" is not a recognized version number', actualMessage, true) | |||
end | |||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
Line 81: | Line 403: | ||
end | end | ||
function suite: | function suite:test_html_error__with_msg() | ||
self:assertEquals('<span class="module-error">Module Error: Test</span>', util.html.error{msg='Test'}) | self:assertEquals('<strong class="error"><span class="module-error">Module Error: Test</span></strong>', util.html.error{msg='Test'}) | ||
self:assertEquals('<span class="module-error">Module Error: </span>', util.html.error()) | end | ||
function suite:test_html_error__without_msg() | |||
self:assertEquals('<strong class="error"><span class="module-error">Module Error: </span></strong>', util.html.error()) | |||
end | end | ||
Line 101: | Line 425: | ||
end | end | ||
function suite: | function suite:test_misc_add_category__success() | ||
self:assertEquals('[[Category:test]]', util.misc.add_category({'test'}, { | self:assertEquals('[[Category:test]]', util.misc.add_category({'test'}, {ignore_blacklist=true})) | ||
end | |||
function suite:test_misc_add_category__blacklisted() | |||
self:assertEquals('', util.misc.add_category({'test'})) | |||
end | end | ||
Line 109: | Line 436: | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
function suite:test_smw_safeguard() | -- util.smw does not seem to be present -- disabling tests | ||
self:assertTrue(util.smw.safeguard(), 'Should return true for module namespace') | -- function suite:test_smw_safeguard() | ||
end | -- self:assertTrue(util.smw.safeguard(), 'Should return true for module namespace') | ||
-- end | |||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
Line 121: | Line 449: | ||
self:assertDeepEquals({'a','b',x='c','d', y='e'}, util.string.split_args('a;b;x=c;d;y=e', {sep=';'}), 'Separator failed.') | self:assertDeepEquals({'a','b',x='c','d', y='e'}, util.string.split_args('a;b;x=c;d;y=e', {sep=';'}), 'Separator failed.') | ||
self:assertFalse(pcall(util.string.split_args, 'a,b=1=1'), 'Testing errornous string failed (duplicate =).') | self:assertFalse(pcall(util.string.split_args, 'a,b=1=1'), 'Testing errornous string failed (duplicate =).') | ||
end | |||
function suite:test_trim() | |||
self:assertEquals("abc", util.string.trim("abc")) | |||
self:assertEquals("abcdef", util.string.trim(" abcdef ")) | |||
self:assertEquals("abc def", util.string.trim(" abc def ")) | |||
end | end | ||
Line 149: | Line 483: | ||
self:assertEquals(12, util.table.find_in_nested_array{tbl=data_assoc, key='a', value=6, rtrkey='b'}) | self:assertEquals(12, util.table.find_in_nested_array{tbl=data_assoc, key='a', value=6, rtrkey='b'}) | ||
self:assertEquals(6, util.table.find_in_nested_array{tbl=data_assoc, key={'a', 'b'}, value=12, rtrkey='a'}) | self:assertEquals(6, util.table.find_in_nested_array{tbl=data_assoc, key={'a', 'b'}, value=12, rtrkey='a'}) | ||
end | |||
-------------------------------------------------------------------------------------------- | |||
-- Test util.html.format_value | |||
-------------------------------------------------------------------------------------------- | |||
function suite:assert_formatted_value_equals(expected, value, options) | |||
local out = util.html.format_value(nil, value, options) | |||
self:assertEquals(expected, out) | |||
end | |||
local static_value = { | |||
base = 10, | |||
min = 10, | |||
max = 10, | |||
} | |||
local range_value = { | |||
base = 10, | |||
min = 15, | |||
max = 25, | |||
} | |||
function suite:test_formatted_value_01() | |||
options = { | |||
color = nil, | |||
inline = nil, | |||
inline_color = nil, | |||
} | |||
self:assert_formatted_value_equals('<em class="tc -value ">10</em>', static_value, options) | |||
self:assert_formatted_value_equals('<em class="tc -mod ">(15-25)</em>', range_value, options) | |||
end | |||
function suite:test_formatted_value_02() | |||
options = { | |||
color = false, | |||
inline = nil, | |||
inline_color = nil, | |||
} | |||
self:assert_formatted_value_equals('10', static_value, options) | |||
self:assert_formatted_value_equals('(15-25)', range_value, options) | |||
end | |||
function suite:test_formatted_value_03() | |||
options = { | |||
color = 'fire', | |||
inline = nil, | |||
inline_color = nil, | |||
} | |||
self:assert_formatted_value_equals('<em class="tc -fire ">10</em>', static_value, options) | |||
self:assert_formatted_value_equals('<em class="tc -fire ">(15-25)</em>', range_value, options) | |||
end | |||
function suite:test_formatted_value_04() | |||
options = { | |||
color = nil, | |||
inline = '+%s%%', | |||
inline_color = nil, | |||
} | |||
self:assert_formatted_value_equals('<em class="tc -value ">+10%</em>', static_value, options) | |||
self:assert_formatted_value_equals('<em class="tc -mod ">+(15-25)%</em>', range_value, options) | |||
end | |||
function suite:test_formatted_value_05() | |||
options = { | |||
color = nil, | |||
inline = '+%s%%', | |||
inline_color = false, | |||
} | |||
self:assert_formatted_value_equals('+<em class="tc -value ">10</em>%', static_value, options) | |||
self:assert_formatted_value_equals('+<em class="tc -mod ">(15-25)</em>%', range_value, options) | |||
end | |||
function suite:test_formatted_value_06() | |||
options = { | |||
color = nil, | |||
inline = '+%s%%', | |||
inline_color = 'default', | |||
} | |||
self:assert_formatted_value_equals('<em class="tc -default ">+<em class="tc -value ">10</em>%</em>', static_value, options) | |||
self:assert_formatted_value_equals('<em class="tc -default ">+<em class="tc -mod ">(15-25)</em>%</em>', range_value, options) | |||
end | |||
function suite:test_formatted_value_07() | |||
options = { | |||
color = false, | |||
inline = '+%s%%', | |||
inline_color = nil, | |||
} | |||
self:assert_formatted_value_equals('+10%', static_value, options) | |||
self:assert_formatted_value_equals('+(15-25)%', range_value, options) | |||
end | |||
function suite:test_formatted_value_08() | |||
options = { | |||
color = false, | |||
inline = '+%s%%', | |||
inline_color = false, | |||
} | |||
self:assert_formatted_value_equals('+10%', static_value, options) | |||
self:assert_formatted_value_equals('+(15-25)%', range_value, options) | |||
end | |||
function suite:test_formatted_value_09() | |||
options = { | |||
color = false, | |||
inline = '+%s%%', | |||
inline_color = 'default', | |||
} | |||
self:assert_formatted_value_equals('<em class="tc -default ">+10%</em>', static_value, options) | |||
self:assert_formatted_value_equals('<em class="tc -default ">+(15-25)%</em>', range_value, options) | |||
end | |||
function suite:test_formatted_value_10() | |||
options = { | |||
color = 'fire', | |||
inline = '+%s%%', | |||
inline_color = nil, | |||
} | |||
self:assert_formatted_value_equals('<em class="tc -fire ">+10%</em>', static_value, options) | |||
self:assert_formatted_value_equals('<em class="tc -fire ">+(15-25)%</em>', range_value, options) | |||
end | |||
function suite:test_formatted_value_11() | |||
options = { | |||
color = 'fire', | |||
inline = '+%s%%', | |||
inline_color = false, | |||
} | |||
self:assert_formatted_value_equals('+<em class="tc -fire ">10</em>%', static_value, options) | |||
self:assert_formatted_value_equals('+<em class="tc -fire ">(15-25)</em>%', range_value, options) | |||
end | |||
function suite:test_formatted_value_12() | |||
options = { | |||
color = 'fire', | |||
inline = '+%s%%', | |||
inline_color = 'default', | |||
} | |||
self:assert_formatted_value_equals('<em class="tc -default ">+<em class="tc -fire ">10</em>%</em>', static_value, options) | |||
self:assert_formatted_value_equals('<em class="tc -default ">+<em class="tc -fire ">(15-25)</em>%</em>', range_value, options) | |||
end | end | ||
Latest revision as of 21:28, 4 January 2024
This module implements the test cases for Module:Util.
All tests passed.
Name | Expected | Actual | |
---|---|---|---|
test_args_from_cargo_maps__cast_valid_fields | |||
test_args_from_cargo_maps__default_function | |||
test_args_from_cargo_maps__default_value | |||
test_args_from_cargo_maps__error_missing_in_fields | |||
test_args_from_cargo_maps__error_missing_in_order | |||
test_args_from_cargo_maps__func | |||
test_args_from_cargo_maps__name | |||
test_args_stats__errors | |||
test_args_stats__min_max | |||
test_args_stats__value | |||
test_args_version__ignore_unknowns_success | |||
test_args_version__misformatted_release_version | |||
test_args_version__success | |||
test_cast_boolean | |||
test_cast_number | |||
test_cast_number__error_invalid_number | |||
test_cast_number__error_max | |||
test_cast_number__error_min | |||
test_cast_number__invalid_with_default | |||
test_cast_replace_if_match__argsnotpresent | |||
test_cast_replace_if_match__ignorequestionmark_match | |||
test_cast_replace_if_match__ignorequestionmark_nomatch | |||
test_cast_replace_if_match__numericmatchnotpresent | |||
test_cast_replace_if_match__numericmatchpresent | |||
test_cast_replace_if_match__patternnotpresent | |||
test_cast_replace_if_match__replacewithnotpresent | |||
test_cast_replace_if_match__stringmatchnotpresent | |||
test_cast_replace_if_match__stringmatchpresent | |||
test_cast_table_from_split_args_string | |||
test_cast_table_from_split_args_string_custom_delimiter | |||
test_cast_table_from_string | |||
test_cast_table_from_string_custom_delimiter | |||
test_cast_table_from_table | |||
test_formatted_value_01 | |||
test_formatted_value_02 | |||
test_formatted_value_03 | |||
test_formatted_value_04 | |||
test_formatted_value_05 | |||
test_formatted_value_06 | |||
test_formatted_value_07 | |||
test_formatted_value_08 | |||
test_formatted_value_09 | |||
test_formatted_value_10 | |||
test_formatted_value_11 | |||
test_formatted_value_12 | |||
test_html_abbr | |||
test_html_error__with_msg | |||
test_html_error__without_msg | |||
test_misc_add_category__blacklisted | |||
test_misc_add_category__success | |||
test_misc_get_frame | |||
test_misc_is_frame | |||
test_string_split_args | |||
test_table_find_in_nested_array__assoc | |||
test_trim |
local util = require('Module:Util/sandbox') -- the module to be tested
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()
-- ----------------------------------------------------------------------------
-- Tests: util.cast
-- ----------------------------------------------------------------------------
function suite:test_cast_boolean()
self:assertEquals(true, util.cast.boolean('true'))
self:assertEquals(true, util.cast.boolean(5))
self:assertEquals(true, util.cast.boolean(true))
self:assertEquals(false, util.cast.boolean('false'))
self:assertEquals(false, util.cast.boolean(0))
self:assertEquals(false, util.cast.boolean(false))
self:assertEquals(false, util.cast.boolean(nil))
end
function suite:test_cast_number()
-- boolean
self:assertEquals(1, util.cast.number(true))
self:assertEquals(0, util.cast.number(false))
-- number
self:assertEquals(5, 5)
self:assertEquals(5, 5.0)
-- string
self:assertEquals(5, util.cast.number('5'))
self:assertEquals(5, util.cast.number('5.0'))
self:assertEquals(5, util.cast.number('5e0'))
end
function suite:test_cast_number__error_invalid_number()
self:assertFalse(pcall(util.cast.number, 'wrong'))
end
function suite:test_cast_number__error_min()
self:assertFalse(pcall(util.cast.number, 5, {min=6}))
end
function suite:test_cast_number__error_max()
self:assertFalse(pcall(util.cast.number, 5, {max=4}))
end
function suite:test_cast_number__invalid_with_default()
self:assertEquals(5, util.cast.number('wrong', {default=5}))
end
function suite:test_cast_replace_if_match__stringmatchpresent()
self:assertEquals('DEF', util.cast.replace_if_match('ABC', {pattern='B', replacewith='DEF'}))
end
function suite:test_cast_replace_if_match__stringmatchnotpresent()
self:assertEquals('ABC', util.cast.replace_if_match('ABC', {pattern='D', replacewith='DEF'}))
end
function suite:test_cast_replace_if_match__numericmatchpresent()
self:assertEquals(456, util.cast.replace_if_match(123, {pattern='2', replacewith=456}))
end
function suite:test_cast_replace_if_match__numericmatchnotpresent()
self:assertEquals(123, util.cast.replace_if_match(123, {pattern='4', replacewith=456}))
end
function suite:test_cast_replace_if_match__argsnotpresent()
self:assertEquals('ABC', util.cast.replace_if_match('ABC'))
end
function suite:test_cast_replace_if_match__patternnotpresent()
self:assertEquals('ABC', util.cast.replace_if_match('ABC', {replacewith='DEF'}))
end
function suite:test_cast_replace_if_match__replacewithnotpresent()
self:assertEquals(nil, util.cast.replace_if_match('ABC', {pattern='B'}))
end
function suite:test_cast_replace_if_match__ignorequestionmark_match()
self:assertEquals(nil, util.cast.replace_if_match('?', {pattern='^%?$'}))
end
function suite:test_cast_replace_if_match__ignorequestionmark_nomatch()
self:assertEquals('Will this match? No', util.cast.replace_if_match('Will this match? No', {pattern='^%?$'}))
end
function suite:test_cast_table_from_table()
local testtable = { [1] = 23, myval = "foo" }
self:assertDeepEquals(testtable, util.cast.table(testtable))
end
function suite:test_cast_table_from_string()
local test_table = { "23", "foo" }
local test_table_str = "23, foo"
self:assertDeepEquals(test_table, util.cast.table(test_table_str))
end
function suite:test_cast_table_from_string_custom_delimiter()
local test_table = { "23", "foo" }
local test_table_str = "23/foo"
self:assertDeepEquals(test_table, util.cast.table(test_table_str, {pattern = '/'}))
end
function suite:test_cast_table_from_split_args_string()
local test_table = { myval = '23', myotherval = "foo" }
local test_table_str = "myval=23, myotherval = foo"
self:assertDeepEquals(test_table, util.cast.table(test_table_str, {split_args = true}))
end
function suite:test_cast_table_from_split_args_string_custom_delimiter()
local test_table = { myval = '23', myotherval = "foo" }
local test_table_str = "myval:23/myotherval:foo"
self:assertDeepEquals(test_table, util.cast.table(test_table_str, {split_args = true, pattern = '/', split_args_pattern = ':'}))
end
-- ----------------------------------------------------------------------------
-- Tests: util.args
-- ----------------------------------------------------------------------------
function suite:test_args_stats__value()
local args = {stat1_id='stat1', stat1_value='1'}
util.args.stats(args, {})
self:assertEquals(args.stat1_value, 1)
end
function suite:test_args_stats__min_max()
local args = {stat1_id='stat1', stat1_min='1', stat1_max='2'}
util.args.stats(args, {})
self:assertEquals(args.stat1_min, 1)
self:assertEquals(args.stat1_max, 2)
self:assertEquals(args.stat1_avg, 1.5)
end
function suite:test_args_stats__errors()
local args = {stat1_id='stat1', stat1_value='1', stat1_min='1'}
self:assertFalse(pcall(util.args.stats, args, {}))
args = {stat1_id='stat1', stat1_min='1'}
self:assertFalse(pcall(util.args.stats, args, {}))
end
--
-- Tests: util.args.from_cargo_map
--
function suite:test_args_from_cargo_maps__cast_valid_fields()
local tpl_args = {
string = 'string',
integer = '12345',
float = '12.345',
boolean_true = 'true',
boolean_false = 'false',
}
local tpl_args_expected = {
string = 'string',
integer = 12345,
float = 12.345,
boolean_true = true,
boolean_false = false,
}
util.args.from_cargo_map{
tpl_args=tpl_args,
frame=mw.getCurrentFrame(),
table_map={
table = 'test',
order = {'string', 'integer', 'float', 'boolean_true', 'boolean_false'},
fields = {
string = {
type = 'String',
field = 'string',
},
integer = {
type = 'Integer',
field = 'integer',
},
float = {
type = 'Float',
field = 'float',
},
boolean_true = {
type = 'Boolean',
field = 'boolean_true',
},
boolean_false = {
type = 'Boolean',
field = 'boolean_false',
},
},
}
}
self:assertDeepEquals(tpl_args_expected, tpl_args, 'Casting / parsing of valid fields failed')
end
function suite:test_args_from_cargo_maps__default_value()
local tpl_args = {
}
local tpl_args_expected = {
default = 5,
}
util.args.from_cargo_map{
tpl_args=tpl_args,
frame=mw.getCurrentFrame(),
table_map={
table = 'test',
order = {'default'},
fields = {
default = {
type = 'Integer',
field = 'default',
default = 5
},
},
}
}
self:assertDeepEquals(tpl_args_expected, tpl_args, 'Setting default values for nil fields failed')
end
function suite:test_args_from_cargo_maps__default_function()
local tpl_args = {
}
local tpl_args_expected = {
default = 5,
}
util.args.from_cargo_map{
tpl_args=tpl_args,
frame=mw.getCurrentFrame(),
table_map={
table = 'test',
order = {'default'},
fields = {
default = {
type = 'Integer',
field = 'default',
default = function (tpl_args, frame)
return 5
end
},
},
}
}
self:assertDeepEquals(tpl_args_expected, tpl_args, 'Setting default values via function for nil fields failed')
end
function suite:test_args_from_cargo_maps__func()
local tpl_args = {
custom = 10
}
local tpl_args_expected = {
custom = 5,
}
util.args.from_cargo_map{
tpl_args=tpl_args,
frame=mw.getCurrentFrame(),
table_map={
table = 'test',
order = {'custom'},
fields = {
custom = {
type = 'Integer',
field = 'custom',
func = function (tpl_args, frame)
return tpl_args.custom / 2
end
},
},
}
}
self:assertDeepEquals(tpl_args_expected, tpl_args, 'Parsing via func failed')
end
function suite:test_args_from_cargo_maps__name()
local tpl_args = {
different_name = '5'
}
local tpl_args_expected = {
different_name = '5',
custom = 5,
}
util.args.from_cargo_map{
tpl_args=tpl_args,
frame=mw.getCurrentFrame(),
table_map={
table = 'test',
order = {'custom'},
fields = {
custom = {
type = 'Integer',
field = 'custom',
name = 'different_name',
},
},
}
}
self:assertDeepEquals(tpl_args_expected, tpl_args, 'Parsing from a different name in tpl_args failed')
end
function suite:test_args_from_cargo_maps__error_missing_in_order()
local tpl_args = {
custom = '5',
}
self:assertFalse(pcall(util.args.from_cargo_map, {}, {
tpl_args=tpl_args,
frame=mw.getCurrentFrame(),
table_map={
table = 'test',
order = {},
fields = {
custom = {
type = 'Integer',
field = 'custom',
},
},
}
}), 'Field that is present in fields but missing in order should raise an error')
end
function suite:test_args_from_cargo_maps__error_missing_in_fields()
local tpl_args = {
custom = '5',
}
self:assertFalse(pcall(util.args.from_cargo_map, {}, {
tpl_args=tpl_args,
frame=mw.getCurrentFrame(),
table_map={
table = 'test',
order = {'custom'},
fields = {
},
}
}), 'Field that is present in order but missing in fields should raise an error')
end
-- ----------------------------------------------------------------------------
-- Tests: util.args.version
-- ----------------------------------------------------------------------------
function suite:test_args_version__success()
local tpl_argtbl = {
removal_version='3.16.0',
ignore_this='123',
release_version='3.14.0'
}
local tpl_args = {
frame=mw.getCurrentFrame(),
variables = {
release = {value = '', property='release_version_property'},
removal = {value = '', property='removal_version_property'},
},
noquery = 1,
return_ids_and_keys = 1
}
local tpl_returned_ids_expected = {'3.14.0', '3.16.0'}
local tpl_returned_keys_expected = {['3.14.0']='release', ['3.16.0']='removal'}
util.args.version(tpl_argtbl, tpl_args)
self:assertDeepEquals(tpl_returned_keys_expected, tpl_args.version_keys)
self:assertDeepEquals(tpl_returned_ids_expected, tpl_args.version_ids)
end
function suite:test_args_version__misformatted_release_version()
local tpl_argtbl = {
removal_version='3.16.0',
ignore_this='123',
release_version='3.14'
}
local tpl_args = {
frame=mw.getCurrentFrame(),
variables = {
release = {value = '', property='release_version_property'},
removal = {value = '', property='removal_version_property'},
},
noquery = 1,
return_ids_and_keys = 1
}
local succeeded, actualMessage = pcall(util.args.version, tpl_argtbl, tpl_args)
self:assertEquals(false, succeeded)
self:assertStringContains('"3.14" is not a recognized version number', actualMessage, true)
end
function suite:test_args_version__ignore_unknowns_success()
local tpl_argtbl = {
removal_version='?',
ignore_this='123',
release_version='3.14.0'
}
local tpl_args = {
frame=mw.getCurrentFrame(),
variables = {
release = {value = '', property='release_version_property'},
removal = {value = '', property='removal_version_property'},
},
noquery = 1,
return_ids_and_keys = 1,
}
local succeeded, actualMessage = pcall(util.args.version, tpl_argtbl, tpl_args)
self:assertEquals(false, succeeded)
self:assertStringContains('"?" is not a recognized version number', actualMessage, true)
end
-- ----------------------------------------------------------------------------
-- Tests: util.html
-- ----------------------------------------------------------------------------
function suite:test_html_abbr()
self:assertEquals('<abbr title="content" class="cls">test</abbr>', util.html.abbr('test', 'content', 'cls'))
end
function suite:test_html_error__with_msg()
self:assertEquals('<strong class="error"><span class="module-error">Module Error: Test</span></strong>', util.html.error{msg='Test'})
end
function suite:test_html_error__without_msg()
self:assertEquals('<strong class="error"><span class="module-error">Module Error: </span></strong>', util.html.error())
end
-- ----------------------------------------------------------------------------
-- Tests: util.misc
-- ----------------------------------------------------------------------------
function suite:test_misc_is_frame()
self:assertFalse(util.misc.is_frame(), 'Test failed for nil')
self:assertFalse(util.misc.is_frame(0), 'Test failed for number')
self:assertFalse(util.misc.is_frame({}), 'Test failed for unrelated table')
self:assertTrue(util.misc.is_frame(mw.getCurrentFrame()), 'Test failed for correct frame object')
end
function suite:test_misc_get_frame()
util.misc.get_frame()
end
function suite:test_misc_add_category__success()
self:assertEquals('[[Category:test]]', util.misc.add_category({'test'}, {ignore_blacklist=true}))
end
function suite:test_misc_add_category__blacklisted()
self:assertEquals('', util.misc.add_category({'test'}))
end
-- ----------------------------------------------------------------------------
-- Tests: util.smw
-- ----------------------------------------------------------------------------
-- util.smw does not seem to be present -- disabling tests
-- function suite:test_smw_safeguard()
-- self:assertTrue(util.smw.safeguard(), 'Should return true for module namespace')
-- end
-- ----------------------------------------------------------------------------
-- Tests: util.string
-- ----------------------------------------------------------------------------
function suite:test_string_split_args()
self:assertDeepEquals({'a','b',x='c','d', y='e'}, util.string.split_args('a,b,x=c,d,y=e'), 'Basic functionality test failed.')
self:assertDeepEquals({'a','b',x='c','d', y='e'}, util.string.split_args('a;b;x=c;d;y=e', {sep=';'}), 'Separator failed.')
self:assertFalse(pcall(util.string.split_args, 'a,b=1=1'), 'Testing errornous string failed (duplicate =).')
end
function suite:test_trim()
self:assertEquals("abc", util.string.trim("abc"))
self:assertEquals("abcdef", util.string.trim(" abcdef "))
self:assertEquals("abc def", util.string.trim(" abc def "))
end
-- ----------------------------------------------------------------------------
-- Tests: util.table
-- ----------------------------------------------------------------------------
local data_array = {
{ 5, 10 },
{ 6, 12 },
}
local data_assoc = {
{
a = 5,
b = 10,
},
{
a = 6,
b = 12,
},
}
function suite:test_table_find_in_nested_array__assoc()
self:assertEquals(6, util.table.find_in_nested_array{tbl=data_assoc, key='a', value=6})
self:assertEquals(nil, util.table.find_in_nested_array{tbl=data_assoc, key='a', value=0})
self:assertEquals(data_assoc[2], util.table.find_in_nested_array{tbl=data_assoc, key='a', value=6, rtrvalue=false})
self:assertEquals(12, util.table.find_in_nested_array{tbl=data_assoc, key='a', value=6, rtrkey='b'})
self:assertEquals(6, util.table.find_in_nested_array{tbl=data_assoc, key={'a', 'b'}, value=12, rtrkey='a'})
end
--------------------------------------------------------------------------------------------
-- Test util.html.format_value
--------------------------------------------------------------------------------------------
function suite:assert_formatted_value_equals(expected, value, options)
local out = util.html.format_value(nil, value, options)
self:assertEquals(expected, out)
end
local static_value = {
base = 10,
min = 10,
max = 10,
}
local range_value = {
base = 10,
min = 15,
max = 25,
}
function suite:test_formatted_value_01()
options = {
color = nil,
inline = nil,
inline_color = nil,
}
self:assert_formatted_value_equals('<em class="tc -value ">10</em>', static_value, options)
self:assert_formatted_value_equals('<em class="tc -mod ">(15-25)</em>', range_value, options)
end
function suite:test_formatted_value_02()
options = {
color = false,
inline = nil,
inline_color = nil,
}
self:assert_formatted_value_equals('10', static_value, options)
self:assert_formatted_value_equals('(15-25)', range_value, options)
end
function suite:test_formatted_value_03()
options = {
color = 'fire',
inline = nil,
inline_color = nil,
}
self:assert_formatted_value_equals('<em class="tc -fire ">10</em>', static_value, options)
self:assert_formatted_value_equals('<em class="tc -fire ">(15-25)</em>', range_value, options)
end
function suite:test_formatted_value_04()
options = {
color = nil,
inline = '+%s%%',
inline_color = nil,
}
self:assert_formatted_value_equals('<em class="tc -value ">+10%</em>', static_value, options)
self:assert_formatted_value_equals('<em class="tc -mod ">+(15-25)%</em>', range_value, options)
end
function suite:test_formatted_value_05()
options = {
color = nil,
inline = '+%s%%',
inline_color = false,
}
self:assert_formatted_value_equals('+<em class="tc -value ">10</em>%', static_value, options)
self:assert_formatted_value_equals('+<em class="tc -mod ">(15-25)</em>%', range_value, options)
end
function suite:test_formatted_value_06()
options = {
color = nil,
inline = '+%s%%',
inline_color = 'default',
}
self:assert_formatted_value_equals('<em class="tc -default ">+<em class="tc -value ">10</em>%</em>', static_value, options)
self:assert_formatted_value_equals('<em class="tc -default ">+<em class="tc -mod ">(15-25)</em>%</em>', range_value, options)
end
function suite:test_formatted_value_07()
options = {
color = false,
inline = '+%s%%',
inline_color = nil,
}
self:assert_formatted_value_equals('+10%', static_value, options)
self:assert_formatted_value_equals('+(15-25)%', range_value, options)
end
function suite:test_formatted_value_08()
options = {
color = false,
inline = '+%s%%',
inline_color = false,
}
self:assert_formatted_value_equals('+10%', static_value, options)
self:assert_formatted_value_equals('+(15-25)%', range_value, options)
end
function suite:test_formatted_value_09()
options = {
color = false,
inline = '+%s%%',
inline_color = 'default',
}
self:assert_formatted_value_equals('<em class="tc -default ">+10%</em>', static_value, options)
self:assert_formatted_value_equals('<em class="tc -default ">+(15-25)%</em>', range_value, options)
end
function suite:test_formatted_value_10()
options = {
color = 'fire',
inline = '+%s%%',
inline_color = nil,
}
self:assert_formatted_value_equals('<em class="tc -fire ">+10%</em>', static_value, options)
self:assert_formatted_value_equals('<em class="tc -fire ">+(15-25)%</em>', range_value, options)
end
function suite:test_formatted_value_11()
options = {
color = 'fire',
inline = '+%s%%',
inline_color = false,
}
self:assert_formatted_value_equals('+<em class="tc -fire ">10</em>%', static_value, options)
self:assert_formatted_value_equals('+<em class="tc -fire ">(15-25)</em>%', range_value, options)
end
function suite:test_formatted_value_12()
options = {
color = 'fire',
inline = '+%s%%',
inline_color = 'default',
}
self:assert_formatted_value_equals('<em class="tc -default ">+<em class="tc -fire ">10</em>%</em>', static_value, options)
self:assert_formatted_value_equals('<em class="tc -default ">+<em class="tc -fire ">(15-25)</em>%</em>', range_value, options)
end
-- ----------------------------------------------------------------------------
return suite