Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Infobox/dates/testcases

From Wikipedia, the free encyclopedia
-- 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, different months, no days
		{'1=April 2015|2=May 2015', 'April' .. DASH .. 'May 2015'},
		-- Same years and months, different days
		{'1=' .. START_DATE_DFFAULT_PLAIN .. '|2=April 24, 2015', 'April 23' .. DASH .. '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'},
	})
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_DF), get_correctly_formatted_date(expand_template("start date", START_DATE_DFFAULT_DF)) .. DASH_BREAK .. get_correctly_formatted_date(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"}), '23 April' .. extract_span(expand_template("start date", START_DATE_DFFAULT_DF)) .. DASH_BREAK .. get_correctly_formatted_date(expand_template("end date", {"2015", "5", "23", df="yes"}))},
		-- Same years, different months, no days
		{'1=' .. expand_template("start date", {"2015", "4", df="yes"}) .. '|2=' .. expand_template("end date", {"2015", "5", df="yes"}), 'April' .. extract_span(expand_template("start date", {"2015", "4", df="yes"})) .. DASH .. 'May 2015' .. extract_span(expand_template("end date", {"2015", "5", 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"}), '23' .. extract_span(expand_template("start date", START_DATE_DFFAULT_DF)) .. DASH .. '24 April 2015' .. extract_span(expand_template("end date", {"2015", "4", "24", df="yes"}))},

		-- 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, different months, no days
		{'1=April 2015|2=May 2015', 'April' .. DASH .. '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 full date
		{'1=' .. expand_template("start date", START_DATE_DFFAULT_DF) .. '|2=present', get_correctly_formatted_date(expand_template("start date", START_DATE_DFFAULT_DF)) .. DASH_BREAK .. 'present'},
		-- To present only year and month
		{'1=' .. expand_template("start date", {"2015", "4", df="yes"}) .. '|2=present', 'April 2015' .. extract_span(expand_template("start date", {"2015", "4", df="yes"})) .. " " .. DASH .. ' present'},
		-- To present only year
		{'1=' .. expand_template("start date", {"2015", df="yes"}) .. '|2=present', '2015' .. extract_span(expand_template("start date", {"2015", df="yes"})) .. DASH .. 'present'},

		{'1=' .. START_DATE_DFFAULT_PLAIN_DF .. '|2=present', START_DATE_DFFAULT_PLAIN_DF .. 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", df="yes"}) .. '|2=' .. expand_template("end date", {"2016", df="yes"}), expand_template("start date", {"2015", df="yes"}) .. DASH .. 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 .. expand_template("end date", {"2015", df="yes"})},
		-- Years and months, different months and years
		{'1=' .. expand_template("start date", {"2015", "4", df="yes"}) .. '|2=' .. expand_template("end date", {"2016", "5", df="yes"}), get_correctly_formatted_date(expand_template("start date", {"2015", "4", df="yes"})) .. DASH_BREAK .. get_correctly_formatted_date(expand_template("end date", {"2016", "5", df="yes"}))},
		-- Years and months, same year
		{'1=' .. expand_template("start date", {"2015", "4", df="yes"}) .. '|2=' .. expand_template("end date", {"2015", "5", df="yes"}), 'April' .. extract_span(expand_template("start date", {"2015", "4", df="yes"})) .. DASH .. get_correctly_formatted_date(expand_template("end date", {"2015", "5", df="yes"}))},

		-- 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'},
	})
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'},

		-- TODO: Same date; unclear what should be the expected result
		-- {'1=' .. expand_template("start date", START_DATE_DFFAULT) .. '|2=' .. expand_template("end date", START_DATE_DFFAULT), ''},
		-- {'1=' .. START_DATE_DFFAULT_PLAIN .. '|2=' .. START_DATE_DFFAULT_PLAIN, ''},
		-- {'1=' .. expand_template("start date", START_DATE_DFFAULT_DF) .. '|2=' .. expand_template("end date", START_DATE_DFFAULT_DF), ''},
		-- {'1=' .. START_DATE_DFFAULT_PLAIN_DF .. '|2=' .. START_DATE_DFFAULT_PLAIN_DF, ''},
	})
end

return p