Module:Sandbox/Izno/citationTests
Appearance
< Module:Sandbox | Izno
-- we never call cs1 directly because it doesn't have a nice implementation
-- as a module, so comment out below line
-- local myModule = require('Module:Citation/CS1') -- the module to be tested
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()
-- merge keys and values into one table
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
-- takes an object and template args
-- the object should have a base_args object, the frame, the template name,
-- and the pattern to find in or near the CITEREF generated
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'] = '2'}))
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.
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'}))
self:assertEquals( 'CITEREF_A1_2020', citeref(env, {date='1 January 2020', year='2020'}))
self:assertEquals( 'CITEREF_A1_2020a', citeref(env, {date='1 January 2020', year='2020a'}))
self:assertEquals( 'CITEREF_A1_2020', citeref(env, {date='2020-01-01', year='2020'}))
self:assertEquals( 'CITEREF_A1_2020a', citeref(env, {date='2020-01-01', year='2020a'}))
end
function suite:testDatesUnexpectedLetter()
local frame = mw.getCurrentFrame()
local args = {title = 'T', author = '_A1_', date = '1 January 2020a'}
self:assertNotStringContains('1 January 2020a', frame:expandTemplate{
title = 'cite book/new', args = args
})
end
function suite:testDatesUnexpectedMaint()
local frame = mw.getCurrentFrame()
local args = {title = 'T', author = '_A1_', date = '1 January 2020', year = '2020a' }
self:assertNotStringContains('CS1 maint: date and year', frame:expandTemplate{
title = 'cite book/new', args = args
})
end
function suite:testDatesMaint()
local frame = mw.getCurrentFrame()
local base_args = {title = 'T', author = '_A1_', year = '2020'}
local template = 'cite book/new'
local maint = 'CS1 maint: date and year'
self:assertStringContains(maint, frame:expandTemplate{
title = template, args = merge(base_args, { date = '1 January 2020'})
})
self:assertStringContains(maint, frame:expandTemplate{
title = template, args = merge(base_args, { date = '2020-01-01'})
})
end
-- should fail: extra unexpected nd in the anchor, plus trailingauthordash below
-- TODO: Should that change? 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 change? 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 to ensure display name settings don't corrupt the CITEREF
function suite:testDisplayNames()
local env = {
frame = mw.getCurrentFrame(),
pattern = 'id=\"(CITEREF%S-)\"',
template = 'cite book/new',
base_args = { title = 'T', author = '_A1_', author2 = 'A2_', date = '2020' }
}
self:assertEquals( 'CITEREF_A1_A2_2020', citeref(env, {chapter = 'CH'}))
self:assertEquals( 'CITEREF_A1_A2_2020', citeref(env, {['display-authors'] = '0'}))
self:assertEquals( 'CITEREF_A1_A2_2020', citeref(env, {['display-authors'] = '1'}))
self:assertEquals( 'CITEREF_A1_A2_2020', citeref(env, {['display-authors'] = 'etal'}))
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 test citation for equivalent value
self:assertEquals( 'id=\"CITEREF_A1_2020\"', citeref(env, {chapter = 'CH'}))
self:assertEquals( 'id=\"CITEREF_A1_2020\"', citeref(env, {ref = 'harv'}))
self:assertEquals( '', citeref(env, {ref = 'none'}))
self:assertEquals( 'id=\"CITEREF_A1_2020\"', citeref(env, {ref = 'CITEREF_A1_2020'}))
end
-- slightly different setup
function suite:testRefREF()
local env = {
frame = mw.getCurrentFrame(),
pattern = '(id=\"REF\")',
template = 'cite book/new',
base_args = { title = 'T', author = '_A1_', year = '2020' }
}
self:assertEquals( 'id=\"REF\"', citeref(env, {ref = 'REF'}))
end
-- tests for expected presence of maintenance messages in ref
function suite:testRefMaint()
local frame = mw.getCurrentFrame()
local base_args = {title = 'T', author = '_A1_', date = '2020'}
self:assertStringContains('CS1 maint: ref duplicates default', frame:expandTemplate{
title = 'cite book/new', args = merge(base_args, { ref = 'CITEREF_A1_2020'})
})
self:assertStringContains('CS1 maint: ref=harv', frame:expandTemplate{
title = 'cite book/new', args = merge(base_args, { ref = 'harv'})
})
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
--[=[
Unit tests for [[Module:Citation/CS1]] anchor and CITEREF creation.
Click talk page to run tests.
]=]
local p = require('Module:UnitTests')
--[[ test_author_mask
Tests to ensure author masks don't corrupt the CITEREF
]]
function p:test_author_mask()
self:preprocess_equals_preprocess_many(
'{{cite book/new |title=T |author=_A1_ |date=2020 |', '}}',
'{{cite book |title=T |author=_A1_ |date=2020 |', '}}',
{
{'chapter=CH'}, -- no author mask: CITEREF_A1_2020
{'author-mask=A1'}, -- CITEREF_A1_2020
{'author-mask1=2'}, -- CITEREF_A1_2020
},
{nowiki=false, templatestyles=true}
)
end
--[[ test_counts
Tests what happens with various counts of contributors, authors, and editors
0 0 0
1 0 0
0 1 0
0 0 1
1 1 0
1 0 1
0 1 1
1 1 1
4 0 0
5 0 0
0 3 0
0 0 2
]]
function p:test_counts()
self:preprocess_equals_preprocess_many(
'{{cite book/new |title=T |year=2020 |', '}}',
'{{cite book |title=T |year=2020 |', '}}',
{
-- Contributor needs both Contribution and Author to be in the ID
{'chapter=CH'}, -- no auto CITEREF without people
{'author=_A1_'}, -- CITEREF_A1_2020
{'contributor=C1'}, -- no auto CITEREF without contribution and author
{'contributor=C1 |contribution=CON'}, -- no auto CITEREF without author
{'editor=_E1_'}, -- CITEREF_E1_2020
{'author=_A1_ |contributor=_C1_'}, -- CITEREF_A1_2020
{'author=_A1_ |contributor=_C1_ |contribution=CON'}, -- CITEREF_C1_2020
{'author=_A1_ |editor=_E1_'}, -- CITEREF_A1_2020
{'editor=_E1_ |contributor=_C1_'}, -- CITEREF_E1_2020
{'editor=_E1_ |contributor=_C1_ |contribution=CON'}, -- CITEREF_E1_2020
{'author=_A1_ |contributor=_C1_ |editor=_E1_'}, -- CITEREF_A1_2020
{'author=_A1_ |contributor=_C1_ |editor=_E1_ |contribution=CON'}, -- CITEREF_C1_2020
{'author=_A1_ |author2=A2_ |author3=A3_ |author4=A4_'}, -- CITEREF_A1_A2_A3_A4_2020
{'author=_A1_ |author2=A2_ |author3=A3_ |author4=A4_ |author5=A5_'}, -- CITEREF_A1_A2_A3_A4_2020
{'author=_A1_ |contributor=_C1_ |contributor2=C2_ |contributor3=C3_ |contribution=CON'}, -- CITEREF_C1_C2_C3_2020
{'editor=_E1_ |editor2=E2_'}, -- CITEREF_E1_E2_2020
},
{nowiki=false, templatestyles=true}
)
end
--[[ test_dates
Tests date resolution code, including anchor years.
]]
function p:test_dates()
self:preprocess_equals_preprocess_many(
'{{cite book/new |title=T |author=_A1_ |', '}}',
'{{cite book |title=T |author=_A1_ |', '}}',
{
{'chapter=CH'}, -- no date: CITEREF_A1 -- UNEXPECTED MIA UNDERSCORE
{'year=2020'}, -- CITEREF_A1_2020
{'date=c. 2020'}, -- CITEREF_A1_c._2020
{'date=1 January 2020'}, -- CITEREF_A1_2020
{'date=1 January 2020a'}, -- CITEREF_A1_2020a -- UNEXPECTED NO TRIM IN DISPLAY
{'date=1 January 2020 |year=2020'}, -- CITEREF_A1_2020 and maint
{'date=1 January 2020 |year=2020a'}, -- CITEREF_A1_2020a -- UNEXPECTED MAINT
{'date=2020-01-01 |year=2020'}, -- CITEREF_A1_2020 and maint
{'date=2020-01-01 |year=2020a'}, -- CITEREF_A1_2020a
{'date=n.d.'}, -- CITEREF_A1_n.d. -- UNEXPECTED n.d.
{'date=nd'}, -- CITEREF_A1_nd -- UNEXPECTED nd
},
{nowiki=false, templatestyles=true}
)
end
--[[
test_display_names
Tests to ensure display name settings don't corrupt the CITEREF
]]
function p:test_display_names()
self:preprocess_equals_preprocess_many(
'{{cite book/new |title=T |author=_A1_ |author2=_A2_ |date=2020 |', '}}',
'{{cite book |title=T |author=_A1_ |author2=_A2_ |date=2020 |', '}}',
{
{'chapter=CH'}, -- no display names: CITEREF_A1_A2_2020
{'display-authors=0'}, -- CITEREF_A1_A2_2020
{'display-authors=1'}, -- CITEREF_A1_A2_2020
{'display-authors=etal'}, -- CITEREF_A1_A2_2020
},
{nowiki=false, templatestyles=true}
)
end
--[[ test_ref
Tests what happens for certain values of ref
]]
function p:test_ref()
self:preprocess_equals_preprocess_many(
'{{cite book/new |title=T |author=_A1_ |date=2020 |', '}}',
'{{cite book |title=T |author=_A1_ |date=2020 |', '}}',
{
{'chapter=CH'}, -- no ref: CITEREF_A1_2020
{'ref=REF'}, -- REF
{'ref=harv'}, -- CITEREF_A1_2020 and maint harv
{'ref=none'}, -- no ID
{'ref=CITEREF_A1_2020'}, -- CITEREF_A1_2020 and maint default ref
},
{nowiki=false, templatestyles=true}
)
end
return suite, p