Module:Sandbox/Izno/citationTests explained

-- 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 testedlocal ScribuntoUnit = require('Module:ScribuntoUnit')local suite = ScribuntoUnit:new

-- merge keys and values into one tablelocal 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 t3end

-- 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 generatedlocal function citeref(test_parameters, targs) local merged_args = merge(test_parameters.base_args, targs) local expansion = test_parameters.frame:expandTemplate local _, _, citeref_value = mw.ustring.find(expansion, test_parameters.pattern) if not citeref_value then citeref_value = end return citeref_valueend

-- Tests to ensure author masks don't corrupt the CITEREFfunction suite:testAuthorMask local env = self:assertEquals('CITEREF_A1_2020', citeref(env,)) self:assertEquals('CITEREF_A1_2020', citeref(env,)) self:assertEquals('CITEREF_A1_2020', citeref(env,))end

-- Tests what happens with various counts of contributors, authors, and editorsfunction suite:testCounts local env = self:assertEquals(, citeref(env,)) self:assertEquals('CITEREF_A1_2020', citeref(env,)) self:assertEquals(, citeref(env,)) self:assertEquals(, citeref(env,)) self:assertEquals('CITEREF_E1_2020', citeref(env,)) self:assertEquals('CITEREF_A1_2020', citeref(env,)) self:assertEquals('CITEREF_C1_2020', citeref(env,)) self:assertEquals('CITEREF_A1_2020', citeref(env,)) self:assertEquals('CITEREF_E1_2020', citeref(env,)) self:assertEquals('CITEREF_E1_2020', citeref(env,)) self:assertEquals('CITEREF_A1_2020', citeref(env,)) self:assertEquals('CITEREF_C1_2020', citeref(env,)) self:assertEquals('CITEREF_A1_A2_A3_A4_2020', citeref(env,)) self:assertEquals('CITEREF_A1_A2_A3_A4_2020', citeref(env,)) self:assertEquals('CITEREF_C1_C2_C3_2020', citeref(env,)) self:assertEquals('CITEREF_E1_E2_2020', citeref(env,))end

-- Tests date resolution code, including anchor years.function suite:testDates local env = self:assertEquals('CITEREF_A1_2020', citeref(env,)) self:assertEquals('CITEREF_A1_c\._2020', citeref(env,)) self:assertEquals('CITEREF_A1_2020', citeref(env,)) self:assertEquals('CITEREF_A1_2020a', citeref(env,)) self:assertEquals('CITEREF_A1_2020', citeref(env,)) self:assertEquals('CITEREF_A1_2020a', citeref(env,)) self:assertEquals('CITEREF_A1_2020', citeref(env,)) self:assertEquals('CITEREF_A1_2020a', citeref(env,))end

function suite:testDatesUnexpectedLetter local frame = mw.getCurrentFrame local args = self:assertNotStringContains('1 January 2020a', frame:expandTemplate)end

function suite:testDatesUnexpectedMaint local frame = mw.getCurrentFrame local args = self:assertNotStringContains('CS1 maint: date and year', frame:expandTemplate)end

function suite:testDatesMaint local frame = mw.getCurrentFrame local base_args = local template = 'cite book/new' local maint = 'CS1 maint: date and year' self:assertStringContains(maint, frame:expandTemplate) self:assertStringContains(maint, frame:expandTemplate)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 = self:assertEquals('CITEREF_A1_', citeref(env,))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 = self:assertEquals('CITEREF_A1_', citeref(env,))end

-- Tests to ensure display name settings don't corrupt the CITEREFfunction suite:testDisplayNames local env = self:assertEquals('CITEREF_A1_A2_2020', citeref(env,)) self:assertEquals('CITEREF_A1_A2_2020', citeref(env,)) self:assertEquals('CITEREF_A1_A2_2020', citeref(env,)) self:assertEquals('CITEREF_A1_A2_2020', citeref(env,))end

-- Tests what happens for certain values of reffunction suite:testRef local env = -- TODO test citation for equivalent value self:assertEquals('id=\"CITEREF_A1_2020\"', citeref(env,)) self:assertEquals('id=\"CITEREF_A1_2020\"', citeref(env,)) self:assertEquals(, citeref(env,)) self:assertEquals('id=\"CITEREF_A1_2020\"', citeref(env,))

end

-- slightly different setupfunction suite:testRefREF local env = self:assertEquals('id=\"REF\"', citeref(env,))end

-- tests for expected presence of maintenance messages in reffunction suite:testRefMaint local frame = mw.getCurrentFrame local base_args = self:assertStringContains('CS1 maint: ref duplicates default', frame:expandTemplate) self:assertStringContains('CS1 maint: ref=harv', frame:expandTemplate)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 = self:assertEquals('CITEREF_A1_', citeref(env,))end

return suite