Jump to content

Module:Infobox/dates/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 17:19, 7 April 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Unit tests for [[Module:Infobox/dates/testcases]]. Click talk page to run tests.
local p = require('Module:UnitTests')

local START_DATE_DFFAULT = {"2015", "4", "23"}
local END_DATE_DEFAULT = {"2016", "4", "23"}

local START_DATE_DFFAULT_DF = {"2015", "4", "23", df="yes"}
local END_DATE_DEFAULT_DF = {"2016", "4", "23", df="yes"}

local START_DATE_DFFAULT_plain = "April 23, 2015"
local END_DATE_DEFAULT_plain = "April 23, 2016"

local START_DATE_DFFAULT_PLAIN_DF = "23 April 2015"
local END_DATE_DEFAULT_PLAIN_DF = "23 April 2016"

local DASH_BREAK = '&nbsp;–<br />'
local DASH = '–'

local function expand_template(template, value)
	local frame = mw.getCurrentFrame()
	return frame:expandTemplate{title = template, args = value}
end

-- Extract the hidden span portion if it exists
local function extract_span(text)
	local span_start = string.find(text, "<span")
	if span_start then
		return string.sub(text, span_start)
	end
	return ""
end

-- Extract visible part (before any span)
local function extract_visible(text)
	text = text:match("^(.-)<span") or text
	return text:gsub("&nbsp;", " ")
end

local function get_correctly_formatted_date(text)
	local visible = extract_visible(text)
	local span = extract_span(text)
	return visible .. span
end

function p:test_01_valid()
	self:preprocess_equals_sandbox_many('{{#invoke:Infobox/dates', 'dates', {
		-- With templates
		{'1=' .. expand_template("start date", START_DATE_DFFAULT), expand_template("start date", START_DATE_DFFAULT)},
		{'1=' .. expand_template("end date", END_DATE_DEFAULT), expand_template("end date", END_DATE_DEFAULT)},
		-- Different years		
		{'1=' .. expand_template("start date", START_DATE_DFFAULT) .. '|2=' .. expand_template("end date", END_DATE_DEFAULT), get_correctly_formatted_date(expand_template("start date", START_DATE_DFFAULT)) .. DASH_BREAK .. get_correctly_formatted_date(expand_template("end date", END_DATE_DEFAULT))},
		-- Same years, different months
		{'1=' .. expand_template("start date", START_DATE_DFFAULT) .. '|2=' .. expand_template("end date", {"2015", "5", "23"}), 'April 23' .. extract_span(expand_template("start date", START_DATE_DFFAULT)) .. DASH_BREAK .. get_correctly_formatted_date(expand_template("end date", {"2015", "5", "23"}))},
		-- Same years, different months, no days
		{'1=' .. expand_template("start date", {"2015", "4"}) .. '|2=' .. expand_template("end date", {"2015", "5"}), 'April' .. extract_span(expand_template("start date", {"2015", "4"})) .. DASH .. 'May 2015' .. extract_span(expand_template("end date", {"2015", "5"}))},
		-- Same years and months, different days
		{'1=' .. expand_template("start date", START_DATE_DFFAULT) .. '|2=' .. expand_template("end date", {"2015", "4", "24"}), 'April 23' .. extract_span(expand_template("start date", START_DATE_DFFAULT)) .. DASH .. '24, 2015' .. extract_span(expand_template("end date", {"2015", "4", "24"}))},

		-- Without templates
		{'1=' .. START_DATE_DFFAULT_plain, START_DATE_DFFAULT_plain},
		{'1=' .. END_DATE_DEFAULT_plain, END_DATE_DEFAULT_plain},
		-- Different years	
		{'1=' .. START_DATE_DFFAULT_plain .. '|2=' .. END_DATE_DEFAULT_plain, START_DATE_DFFAULT_plain .. DASH_BREAK .. END_DATE_DEFAULT_plain},
		-- Same years, different months
		{'1=' .. START_DATE_DFFAULT_plain .. '|2=May 23, 2015', 'April 23' .. DASH_BREAK .. 'May 23, 2015'},
		-- Same years and months, different days
		{'1=' .. START_DATE_DFFAULT_plain .. '|2=May 24, 2015', 'April 23' .. DASH_BREAK .. 'May 24, 2015'},

		-- To present full date
		{'1=' .. expand_template("start date", START_DATE_DFFAULT) .. '|2=present', get_correctly_formatted_date(expand_template("start date", START_DATE_DFFAULT)) .. DASH_BREAK .. 'present'},
		-- To present only year and month
		{'1=' .. expand_template("start date", {"2015", "4"}) .. '|2=present', 'April 2015' .. extract_span(expand_template("start date", {"2015", "4"})) .. " " .. DASH .. ' present'},
		-- To present only year
		{'1=' .. expand_template("start date", {"2015"}) .. '|2=present', '2015' .. extract_span(expand_template("start date", {"2015"})) .. DASH .. 'present'},

		{'1=' .. START_DATE_DFFAULT_plain .. '|2=present', START_DATE_DFFAULT_plain .. DASH_BREAK .. 'present'},
		-- To present only year and month
		{'1=April 2015|2=present', 'April 2015 ' .. DASH .. ' present'},
		-- To present only year
		{'1=2015|2=present', '2015' .. DASH .. 'present'},
		
		-- Partial dates with templates

		-- Just years, different year
		{'1=' .. expand_template("start date", {"2015"}) .. '|2=' .. expand_template("end date", {"2016"}), expand_template("start date", {"2015"}) .. DASH .. expand_template("end date", {"2016"})},
		-- Just years, same year
		{'1=' .. expand_template("start date", {"2015"}) .. '|2=' .. expand_template("end date", {"2015"}), expand_template("start date", {"2015"}) .. DASH .. expand_template("end date", {"2015"})},
		-- Years and months, different months and years
		{'1=' .. expand_template("start date", {"2015", "4"}) .. '|2=' .. expand_template("end date", {"2016", "5"}), get_correctly_formatted_date(expand_template("start date", {"2015", "4"})) .. DASH_BREAK .. get_correctly_formatted_date(expand_template("end date", {"2016", "5"}))},
		-- Years and months, same year
		{'1=' .. expand_template("start date", {"2015", "4"}) .. '|2=' .. expand_template("end date", {"2015", "5"}), 'April' .. extract_span(expand_template("start date", {"2015", "4"})) .. DASH .. get_correctly_formatted_date(expand_template("end date", {"2015", "5"}))},

		-- Partial dates without templates
		
		-- Just years, different year
		{'1=2015|2=2016', '2015' .. DASH .. '2016'},
		-- Just years, same year
		{'1=2015|2=2015', '2015' .. DASH .. '2015'},
		-- Years and months, different months and years
		{'1=April 2015|2=May 2016', 'April 2015' .. DASH_BREAK .. 'May 2016'},
		-- Years and months, same year
		{'1=April 2015|2=May 2015', 'April' .. DASH .. 'May 2015'},
	}, {nowiki=1})
end

-- -- Same with df=yes
-- function p:test_02_valid_df()
-- 	self:preprocess_equals_sandbox_many('{{#invoke:Infobox/dates', 'dates', {
-- 		-- With templates
-- 		{'1=' .. expand_template("start date", START_DATE_DFFAULT_DF), expand_template("start date", START_DATE_DFFAULT_DF)},
-- 		{'1=' .. expand_template("end date", END_DATE_DEFAULT_DF), expand_template("end date", END_DATE_DEFAULT_DF)},
-- 		-- Different years		
-- 		{'1=' .. expand_template("start date", START_DATE_DFFAULT_DF) .. '|2=' .. expand_template("end date", END_DATE_DEFAULT), expand_template("start date", START_DATE_DFFAULT_DF) .. DASH_BREAK .. expand_template("end date", END_DATE_DEFAULT_DF)},
-- 		-- Same years, different months
-- 		{'1=' .. expand_template("start date", START_DATE_DFFAULT_DF) .. '|2=' .. expand_template("end date", {"2015", "5", "23", df="yes"}), 'April 23' .. DASH_BREAK .. expand_template("end date", {"2015", "5", "23", df="yes"})},
-- 		-- Same years and months, different days
-- 		{'1=' .. expand_template("start date", START_DATE_DFFAULT_DF) .. '|2=' .. expand_template("end date", {"2015", "4", "24", df="yes"}), 'April 23' .. DASH_BREAK .. expand_template("end date", {"2015", "4", "24", df="yes"})},
-- 		-- To present	
-- 		{'1=' .. expand_template("start date", START_DATE_DFFAULT_DF) .. '|2=present', expand_template("start date", START_DATE_DFFAULT_DF) .. DASH_BREAK .. 'present'},
		
-- 		-- Without templates
-- 		{'1=' .. START_DATE_DFFAULT_PLAIN_DF, START_DATE_DFFAULT_PLAIN_DF},
-- 		{'1=' .. END_DATE_DEFAULT_PLAIN_DF, END_DATE_DEFAULT_PLAIN_DF},
-- 		-- Different years	
-- 		{'1=' .. START_DATE_DFFAULT_PLAIN_DF .. '|2=' .. END_DATE_DEFAULT_PLAIN_DF, START_DATE_DFFAULT_PLAIN_DF .. DASH_BREAK .. END_DATE_DEFAULT_PLAIN_DF},
-- 		-- Same years, different months
-- 		{'1=' .. START_DATE_DFFAULT_PLAIN_DF .. '|2=23 May 2015', '23 April' .. DASH_BREAK .. '23 May 2015'},
-- 		-- Same years and months, different days
-- 		{'1=' .. START_DATE_DFFAULT_PLAIN_DF .. '|2=24 May 2015', '23 April' .. DASH_BREAK .. '24 May 2015'},
-- 		-- To present
-- 		{'1=' .. START_DATE_DFFAULT_PLAIN_DF .. '|2=present', START_DATE_DFFAULT_PLAIN_DF .. DASH_BREAK .. 'present'},
		
-- 		-- Partial dates with templates

-- 		-- Just years, differnt years
-- 		{'1=' .. expand_template("start date", {"2015", df="yes"}) .. '|2=' .. expand_template("end date", {"2016", df="yes"}), expand_template("start date", {"2015", df="yes"}) .. DASH_BREAK .. expand_template("end date", {"2016", df="yes"})},
-- 		-- Just years, same year
-- 		{'1=' .. expand_template("start date", {"2015", df="yes"}) .. '|2=' .. expand_template("end date", {"2015", df="yes"}), expand_template("start date", {"2015", df="yes"}) .. DASH_BREAK .. expand_template("end date", {"2015", df="yes"})},
-- 		-- Years and months, different months
-- 		{'1=' .. expand_template("start date", {"2015", "4", df="yes"}) .. '|2=' .. expand_template("end date", {"2016", "5", df="yes"}), expand_template("start date", {"2015", "4", df="yes"}) .. DASH_BREAK .. expand_template("end date", {"2016", "5", df="yes"})},
-- 		-- Years and months, same months
-- 		{'1=' .. expand_template("start date", {"2015", "4", df="yes"}) .. '|2=' .. expand_template("end date", {"2016", "4", df="yes"}), expand_template("start date", {"2015", "4", df="yes"}) .. DASH_BREAK .. expand_template("end date", {"2016", "4", df="yes"})},

-- 	})
-- end

-- function p:test_03_invalid_range()
-- 	self:preprocess_equals_sandbox_many('{{#invoke:Infobox/dates', 'dates', {
-- 		 -- end date starts before start date
-- 		{'1=' .. expand_template("start date", END_DATE_DEFAULT) .. '|2=' .. expand_template("end date", START_DATE_DFFAULT), 'Invalid date range'},
-- 		{'1=' .. END_DATE_DEFAULT_plain .. '|2=' .. START_DATE_DFFAULT_plain, 'Invalid date range'},
-- 		-- Same date
-- 		{'1=' .. expand_template("start date", START_DATE_DFFAULT) .. '|2=' .. expand_template("end date", {"2015", "4", "24"}), 'Invalid date range'},
-- 		{'1=' .. START_DATE_DFFAULT_plain .. '|2=' .. START_DATE_DFFAULT_plain, 'Invalid date range'},
-- 		{'1=' .. expand_template("start date", START_DATE_DFFAULT_DF) .. '|2=' .. expand_template("end date", {"2015", "4", "24", df="yes"}), 'Invalid date range'},
-- 		{'1=' .. START_DATE_DFFAULT_PLAIN_DF .. '|2=' .. START_DATE_DFFAULT_PLAIN_DF, 'Invalid date range'},
-- 	})
-- end

return p