-- Unit tests for . Click talk page to run tests.local ScribuntoUnit = require('Module:ScribuntoUnit')local liveMod = require('Module:Page assessment').test -- the live module functions to be testedlocal sandbox = require('Module:Page assessment/sandbox').test -- the sandbox module functions to be tested
local suite = ScribuntoUnit:new
----
---------- getWikitext ---------------------------------------------------------
function suite:testGetWikitext_fromArticle(modToTest) modToTest = modToTest or liveMod local subjectWikitext, talkWikitext = modToTest.getWikitext("Wikipedia") self:assertTrue(mw.ustring.len(subjectWikitext) > 100, "Wikitext of page 'Wikipedia' more than 100 chars") self:assertTrue(mw.ustring.len(subjectWikitext) > 100, "Wikitext of page 'Talk:Wikipedia' more than 100 chars") self:assertFalse(subjectWikitext
function suite:testGetWikitext_fromTalk(modToTest) modToTest = modToTest or liveMod local subjectWikitext1, talkWikitext1 = modToTest.getWikitext("Wikipedia") local subjectWikitext2, talkWikitext2 = modToTest.getWikitext("Talk:Wikipedia") self:assertEquals(subjectWikitext1, subjectWikitext2, "Subject page of 'Wikipedia' is same as subject page of 'Talk:Wikipedia'") self:assertEquals(talkWikitext1, talkWikitext2, "Talk page of 'Wikipedia' is same as talk page of 'Talk:Wikipedia'")endfunction suite:testGetWikitext_fromTalk__sandbox return self:testGetWikitext_fromTalk(sandbox) end
function suite:testGetWikitext_nonExisting(modToTest) modToTest = modToTest or liveMod local subjectWikitext, talkWikitext = modToTest.getWikitext("Wikipediadsgvbjfdoibtdhbntrsnsr") self:assertEquals(subjectWikitext, nil, "Nil result for subject page wikitext") self:assertEquals(talkWikitext, nil, "Nil result for talk page wikitext")endfunction suite:testGetWikitext_nonExisting__sandbox return self:testGetWikitext_nonExisting(sandbox) end
---------- isRedirect ----------------------------------------------------------
function suite:testIsRedirect_fromWikitext(modToTest) modToTest = modToTest or liveMod
self:assertTrue(modToTest.isRedirect("#REDIRECT Foo"), "uppercased") self:assertTrue(modToTest.isRedirect("#Redirect Foo"), "sentence-cased") self:assertTrue(modToTest.isRedirect("#redirect Foo"), "lower-cased") self:assertFalse(modToTest.isRedirect("# REDIRECT Foo"), "space after #") self:assertTrue(modToTest.isRedirect(" #REDIRECT Foo"), "space before #") self:assertTrue(modToTest.isRedirect("#REDIRECT Foo"), "spaces before target") self:assertFalse(modToTest.isRedirect("How to make a #REDIRECT Foo"), "Not at start") self:assertFalse(modToTest.isRedirect("
#REDIRECT [[Foo]]"), "Within pre tags")endfunction suite:testIsRedirect_fromWikitext__sandbox return self:testIsRedirect_fromWikitext(sandbox) end
function suite:testIsRedirect_fromPages(modToTest) modToTest = modToTest or liveMod
self:assertFalse(modToTest.isRedirect(modToTest.getWikitext("Wikipedia")), "page 'Wikipedia' is not a redirect") self:assertTrue(modToTest.isRedirect(modToTest.getWikitext("MOS:ABBR")), "page 'MOS:ABBR' is a redirect")endfunction suite:testIsRedirect_fromPages__sandbox return self:testIsRedirect_fromPages(sandbox) end
---------- classByNamespace ----------------------------------------------------
function suite:testClassByNamesapce_forEachNS(modToTest) modToTest = modToTest or liveMod local data = for page, class in ipairs(data) do self:assertEquals(modToTest.classByNamespace(page), class, string.format("'%s' get assessed as '%s'", page, class) ) endendfunction suite:testClassByNamesapce_forEachNS__sandbox return self:testClassByNamesapce_forEachNS(sandbox) end
---------- normaliseRating -----------------------------------------------------
function suite:testNormaliseRating_forEach(modToTest) modToTest = modToTest or liveMod local data = for normalised, ratings in pairs(data) do for _, rating in pairs(ratings) do self:assertEquals(normalised, modToTest.normaliseRating(rating), string.format("'%s' normalises to '%s'", normalised, rating) ) end endendfunction suite:testNormaliseRating_forEach__sandbox(modToTest) return suite:testNormaliseRating_forEach(sandbox) end
---------- class ---------------------------------------------------------------
function suite:testClass_forStringOutput(modToTest) modToTest = modToTest or liveMod local pages = mw.text.split("This manual documents Lua as it is used in MediaWiki with the Scribunto extension Some parts are derived from the Lua 5.1 reference manual which is available under the MIT license", " ") for _, page in pairs(pages) do local class = modToTest.class(page) self:assertEquals(type(class), "string", "Class for '" .. page .. "' is a string") self:assertTrue(mw.ustring.len(class) < 10, "Class for '" .. page .. "' is less than 10 characters in length") self:assertEquals(mw.ustring.sub(class, 1, 1), mw.ustring.upper(mw.ustring.sub(class, 1, 1)), "Class for '" .. page .. "' starts with a capital letter") endendfunction suite:testClass_forStringOutput__sandbox return suite:testClass_forStringOutput(sandbox) end
return suite