Jump to content

Module:String/testcases

From Wikipedia, the free encyclopedia
-- Unit tests for [[Module:String]]. Click talk page to run tests.
local p = require('Module:UnitTests')

function p:test_len()
	local cases = {
		{' foo ', '5'},
		{'s= foo ', '3'},
		{'', '0'},
		{'s=', '0'},
		{'   ', '3'},
		{'最近の更新', '5'},
	}
	self:preprocess_equals_many('{{#invoke:String|len|', '}}', cases)
	self:preprocess_equals_many('{{#invoke:String/sandbox|len|', '}}', cases)
end

function p:test_sub()
	local cases = {
		{'Lorem ipsum', 'Lorem ipsum'},
		{' Lorem ipsum ', ' Lorem ipsum '},
		{'s= Lorem ipsum ', 'Lorem ipsum'},
		{'Lorem ipsum|1', 'Lorem ipsum'},
		{'Lorem ipsum|2', 'orem ipsum'},
		{'Lorem ipsum|-5', 'ipsum'},
		{'Lorem ipsum|-1', 'm'},
		{'Lorem ipsum|11', 'm'},
		{'Lorem ipsum||11', 'Lorem ipsum'},
		{'Lorem ipsum||10', 'Lorem ipsu'},
		{'Lorem ipsum||-1', 'Lorem ipsum'},
		{'Lorem ipsum||-2', 'Lorem ipsu'},
		{'Lorem ipsum||-11', 'L'},
		{'Lorem ipsum|1|11', 'Lorem ipsum'},
		{'Lorem ipsum|2|10', 'orem ipsu'},
		{'Lorem ipsum|1|-1', 'Lorem ipsum'},
		{'Lorem ipsum|2|-2', 'orem ipsu'},
		{'Lorem ipsum|7|-5', 'i'},
		{'s=Lorem ipsum|i=7|j=-5', 'i'},

		{'|no_category=true', '<strong class="error">String Module Error: String subset index out of range</strong>'},
		{'s=|no_category=true', '<strong class="error">String Module Error: String subset index out of range</strong>'},
		{'Lorem ipsum|0|no_category=true', '<strong class="error">String Module Error: String subset index out of range</strong>'},
		{'Lorem ipsum||-12|no_category=true', '<strong class="error">String Module Error: String subset index out of range</strong>'},
		{'Lorem ipsum|7|-6|no_category=true', '<strong class="error">String Module Error: String subset indices out of order</strong>'},
	
		{'s=Lorem ipsum|i=11|j=1|no_category=true', '<strong class="error">String Module Error: String subset indices out of order</strong>'},
		{'s=Lorem ipsum|i=11|j=1|ignore_errors=true', ''},
	}
	self:preprocess_equals_many('{{#invoke:String|sub|', '}}', cases)
	self:preprocess_equals_many('{{#invoke:String/sandbox|sub|', '}}', cases)
end

function p:test_sublength()
	local cases = {
		{'s=abcde|i=0|len=5', 'abcde'},
		{'s=abcde|i=0|len=1', 'a'},
		{'s=abcde|i=1|len=2', 'bc'},
		{'s=abcde|i=-3|len=2', 'de'},
		{'s=abcde|i=3|len=-2', ''},
		{'s=abcde|i=1|len=0', ''},
		{'s=abcde|i=0|len=0', ''},
		{'s=|i=1|len=1', ''},
		{'s=abcde|i=0|len=99', 'abcde'},
	}
	self:preprocess_equals_many('{{#invoke:String|sublength|', '}}', cases)
	self:preprocess_equals_many('{{#invoke:String/sandbox|sublength|', '}}', cases)
end

function p:test_replace()
	local cases = {
		{'1.1.1.1|.|/', '1/1/1/1'},
		{'source=1.1.1.1|pattern=.|replace=/|count=2', '1/1/1.1'},
		{'alpha (beta)| (| gamma (', 'alpha gamma (beta)'},
		{'Dog (cat)|%s%(.*%)||plain=false', 'Dog'},
	}
	self:preprocess_equals_many('{{#invoke:String|replace|', '}}', cases)
	self:preprocess_equals_many('{{#invoke:String/sandbox|replace|', '}}', cases)
end

function p:test_match()
	local cases = {
		{'dog (cat)|%((%a-)%)', 'cat'},
		{'dog (cat) (canary) (fish)|%((%a-)%)||-1', 'fish'},
		{'dog (cat) (canary) (fish)|%((%a-)%)||2', 'canary'},
		{'dog (cat) (canary) (fish)|%((%a-)%)|6|1', 'canary'},
		{'dog (cat) (canary) (fish)|%((%a-)%)|6|2', 'fish'},
		{'dog (cat)|%((%a-)%)||2|no_category=true', '<strong class="error">String Module Error: Match not found</strong>'},
		{'dog (cat)|%((%a-)%)||2|ignore_errors=true', ''},
	}
	self:preprocess_equals_many('{{#invoke:String|match|', '}}', cases)
	self:preprocess_equals_many('{{#invoke:String/sandbox|match|', '}}', cases)
end

function p:test_match_reverse()
	local cases = {
		{'a (1) (2) (3) (4) (5) (6) (a)|%((%d)%)||-1', '6'},
		{'a (1) (2) (3) (4) (5) (6) (a)|%((%d)%)||-2', '5'},
		{'a (1) (2) (3) (4) (5) (6) (a)|%((%d)%)||-4', '3'},
		{'a (1) (2) (3) (4) (5) (6) (a)|%((%d)%)||-5', '2'},
		{'a (1) (2) (3) (4) (5) (6) (a)|%((%d)%)||-6', '1'},
		{'a (1) (2) (3) (4) (5) (6) (a)|%((%d)%)||-7|no_category=true', '<strong class="error">String Module Error: Match not found</strong>'},
	}
	self:preprocess_equals_many('{{#invoke:String|match|', '}}', cases)
	self:preprocess_equals_many('{{#invoke:String/sandbox|match|', '}}', cases)
end

function p:test_pos()
	local out_of_range_error = '[[Category:Errors reported by Module String]]<strong class="error">String Module Error: String index out of range</strong>'
	local cases = {
		{'abc|1', 'a'},
		{'abc|3', 'c'},
		{'abc|-1', 'c'},
		{'abc|-3', 'a'},
		{'target=abc|pos=2', 'b'},
		
		{'abc|-4', out_of_range_error},
		{'abc|4', out_of_range_error},
		{'abc|0', out_of_range_error},
		{'abc|', out_of_range_error},
		{'', out_of_range_error},
	}
	self:preprocess_equals_many('{{#invoke:String|pos|', '}}', cases, {nowiki=1})
	self:preprocess_equals_many('{{#invoke:String/sandbox|pos|', '}}', cases, {nowiki=1})
end

function p:test_find()
	self:preprocess_equals_many('{{#invoke:String/sandbox|find|', '}}', {
		{'', '0'},
		{'source=', '0'},
		{'source=|target=', '0'},
		{'source=|target=Bob', '0'},
		{'Bob|Joe', '0'},
		{'Bob|b', '3'},
		{'Bob Joe|oe', '6'},
		{'source= Bob |o', '2'},
		{' Bob |o', '3'},
		{'높지만 급격히|만', '3'},
		{'source=BAAAB|target=%w|plain=false', '1'},
		{'source=BAAAB|target=%w|start=3|plain=false', '3'},
		{'source=BAAAB|target=(%w)%1|plain=false', '2'},
	})
end

function p:test_rep()
	local cases = {
		{'abc|1', 'abc'},
		{'a b|7', 'a ba ba ba ba ba ba b'},
		{'bla|glug|no_category=true', '<strong class="error">String Module Error: function rep expects a number as second parameter, received "glug"</strong>'},
		{'bla|glug|ignore_errors=true', ''},
		{'àèò|3', 'àèòàèòàèò'},
		{'abc|0', ''},
		{'abc|-1', ''},
	}
	self:preprocess_equals_many('{{#invoke:String|rep|', '}}', cases)
	self:preprocess_equals_many('{{#invoke:String/sandbox|rep|', '}}', cases)
end

function p:test_escapePattern()
	self:preprocess_equals_many('{{#invoke:String/sandbox|escapePattern|', '}}', {
		{'foo', 'foo'},
		{'A.D.', 'A%.D%.'},
		{'0123', '0123'},
		{'^', '%^'},
		{'$', '%$'},
		{'(', '%('},
		{')', '%)'},
		{'%', '%%'},
		{'.', '%.'},
		{'[', '%['},
		{']', '%]'},
		{'*', '%*'},
		{'+', '%+'},
		{'-', '%-'},
		{'?', '%?'},
		{'!', '!'},
		{'"', '"'},
		{'&', '&'},
		{"'", "'"},
		{'{', '{'},
		{'}', '}'},
		{'`', '`'},
		{'@', '@'},
		{',', ','},
		{'<', '<'},
		{'>', '>'},
		{'/', '/'},
		{'_', '_'},
		{'\\', '\\'},
		{'~', '~'},
		{' ', ' '},
		{'', ''},
		{'x#', 'x#'}, -- We can't just test "#" because it will be interpreted as a list
		{'x;', 'x;'}, -- We can't just test ";" because it will be interpreted as a list
		{'x:', 'x:'}, -- We can't just test ":" because it will be interpreted as a list
		{'{{!}}', '|'}, -- Need to escape the template pipe syntax
		{'1==', '='}, -- Need to escape the "foo = bar" template syntax
		{'no_category=true', '<strong class="error">String Module Error: No pattern string specified</strong>'},
	})
end

function p:test_join()
	self:preprocess_equals_many('{{#invoke:String/sandbox|join|', '}}', {
		{'', ''},
		{'X', ''},
		{'X|a', 'a'},
		{'X|a|b', 'aXb'},
		{'X|a|b|c', 'aXbXc'},
		{'X|a|b|c|d', 'aXbXcXd'},
		{'X|a|b||d', 'aXbXd'},
		{'X|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z', 'aXbXcXdXeXfXgXhXiXjXkXlXmXnXoXpXqXrXsXtXuXvXwXxXyXz'}
	})

end


return p