Jump to content

Module:Sandbox/Izno/citationTests

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Izno (talk | contribs) at 05:29, 20 February 2021 (year = 2020). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local myModule = require('Module:Citation/CS1') -- the module to be tested
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()

local function merge(t1, t2)
	local t3 = {}
	for k, v in pairs(t1) do
		t3[k] = v
	end
	for k, v in pairs(t2) do
		t3[k] = v
	end
	return t3
end

local function citeref(test_parameters, targs)
	
	local merged_args = merge(test_parameters.base_args, targs)
	local expansion = test_parameters.frame:expandTemplate{
		title = test_parameters.template, args = merged_args
	}
	
	local _, _, citeref_value = mw.ustring.find(expansion, test_parameters.pattern)
	if not citeref_value then
		citeref_value = ''
	end
	return citeref_value
end

-- Tests to ensure author masks don't corrupt the CITEREF
function suite:testAuthorMask()
	local env = {
		frame = mw.getCurrentFrame(),
		pattern = 'id=\"(CITEREF%S-)\"',
		template = 'cite book/new',
		base_args = { title = 'T', author = '_A1_', year = '2020' }
	}
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {chapter = 'CH'}))
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {['author-mask'] = 'A1'}))
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {['author-mask1'] = 'A1'}))
	
end

-- Tests what happens with various counts of contributors, authors, and editors
function suite:testCounts()
	local env = {
		frame = mw.getCurrentFrame(),
		pattern = 'id=\"(CITEREF%S-)\"',
		template = 'cite book/new',
		base_args = { title = 'T', year = '2020' }
	}
	self:assertEquals( '', citeref(env, {chapter = 'CH'}))
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {author = '_A1_'}))
	self:assertEquals( '', citeref(env, {contributor = 'C1'}))
	self:assertEquals( '', citeref(env, {contributor = 'C1', contribution = 'CON'}))
	self:assertEquals( 'CITEREF_E1_2020', citeref(env, {editor = '_E1_'}))
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {author = '_A1_', contributor = '_C1_'}))
	self:assertEquals( 'CITEREF_C1_2020', citeref(env, {author = '_A1_', contributor = '_C1_', contribution = 'CON'}))
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {author = '_A1_', editor = '_E1_'}))
	self:assertEquals( 'CITEREF_E1_2020', citeref(env, {editor = '_E1_', contributor = '_C1_'}))
	self:assertEquals( 'CITEREF_E1_2020', citeref(env, {editor = '_E1_', contributor = '_C1_', contribution = 'CON'}))
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {author = '_A1_', contributor = '_C1_', editor = '_E1_'}))
	self:assertEquals( 'CITEREF_C1_2020', citeref(env, {author = '_A1_', contributor = '_C1_', editor = '_E1_', contribution = 'CON'}))
	self:assertEquals( 'CITEREF_A1_A2_A3_A4_2020', citeref(env, {author = '_A1_', author2 = 'A2_', author3 = 'A3_', author4 = 'A4_'}))
	self:assertEquals( 'CITEREF_A1_A2_A3_A4_2020', citeref(env, {author = '_A1_', author2 = 'A2_', author3 = 'A3_', author4 = 'A4_', author5 = 'A5_'}))
	self:assertEquals( 'CITEREF_C1_C2_C3_2020', citeref(env, {author = '_A1_', contributor = '_C1_', contributor2 = 'C2_', contributor3 = 'C3_', contribution = 'CON'}))
	self:assertEquals( 'CITEREF_E1_E2_2020', citeref(env, {editor = '_E1_', editor2 = 'E2_'}))
end

-- Tests date resolution code, including anchor years.
-- TODO: Make "unexpecteds" into their own tests. Somewhere.
function suite:testDates()
	local env = {
		frame = mw.getCurrentFrame(),
		pattern = 'id=\"(CITEREF%S-)\"',
		template = 'cite book/new',
		base_args = { title = 'T', author = '_A1_' }
	}
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {year='2020'}))
	self:assertEquals( 'CITEREF_A1_c._2020', citeref(env, {date='c. 2020'}))
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {date='1 January 2020'}))
	self:assertEquals( 'CITEREF_A1_2020a', citeref(env, {date='1 January 2020a'})) -- UNEXPECTED NO TRIM IN DISPLAY
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {date='1 January 2020', year='2020'})) -- and maint
	self:assertEquals( 'CITEREF_A1_2020a', citeref(env, {date='1 January 2020', year='2020a'})) -- UNEXPECTED MAINT
	self:assertEquals( 'CITEREF_A1_2020', citeref(env, {date='2020-01-01', year='2020'})) -- and maint
	self:assertEquals( 'CITEREF_A1_2020a', citeref(env, {date='2020-01-01', year='2020a'}))
end

-- should fail: extra unexpected nd in the anchor
-- TODO: Should that chnage? I've seen workarounds in the wild.
function suite:testDatesExtraNd()
	local env = {
		frame = mw.getCurrentFrame(),
		pattern = 'id=\"(CITEREF%S-)\"',
		template = 'cite book/new',
		base_args = { title = 'T', author = '_A1_' }
	}
	
	self:assertEquals( 'CITEREF_A1', citeref(env, {date='nd'}))
end

-- should fail: extra unexpected n.d. in the anchor
-- TODO: Should that chnage? I've seen workarounds in the wild.
function suite:testDatesExtraNdPunct()
	local env = {
		frame = mw.getCurrentFrame(),
		pattern = 'id=\"(CITEREF%S-)\"',
		template = 'cite book/new',
		base_args = { title = 'T', author = '_A1_' }
	}
	
	self:assertEquals( 'CITEREF_A1', citeref(env, {date='n.d.'}))
end

-- Tests what happens for certain values of ref
function suite:testRef()
	local env = {
		frame = mw.getCurrentFrame(),
		pattern = '(id=\"CITEREF%S-\")',
		template = 'cite book/new',
		base_args = { title = 'T', author = '_A1_', year = '2020' }
	}
	
	-- TODO make ones with maints real tests
	-- TODO test citation for equivalent value
	self:assertEquals( 'id=\"CITEREF_A1_2020\"', citeref(env, {chapter = 'CH'}))
	self:assertEquals( 'id=\"REF\"', citeref(env, {ref = 'REF'}))
	self:assertEquals( 'id=\"CITEREF_A1_2020\"', citeref(env, {ref = 'harv'})) -- and maint harv
	self:assertEquals( '', citeref(env, {ref = 'none'})) -- no ID
	self:assertEquals( 'id=\"CITEREF_A1_2020\"', citeref(env, {ref = 'CITEREF_A1_2020'})) -- and maint default ref

end

-- should fail: missing trailing underscore in anchor; not sure if that's desirable
-- or if that can change
-- TODO: Ask someone.
function suite:testTrailingAuthorDash()
	local env = {
		frame = mw.getCurrentFrame(),
		pattern = 'id=\"(CITEREF%S-)\"',
		template = 'cite book/new',
		base_args = { title = 'T', author = '_A1_' }
	}
	self:assertEquals( 'CITEREF_A1_', citeref(env, {chapter='CH'}))
end



return suite