-- Unit tests for [[Module:Page assessment}}]]. Click talk page to run tests.
local ScribuntoUnit = require('Module:ScribuntoUnit')
local liveMod = require('Module:Page assessment').test -- the live module functions to be tested
local sandbox = require('Module:Page assessment/sandbox').test -- the sandbox module functions to be tested

local suite = ScribuntoUnit:new()

--[[
Tescase naming convention:
 - Live module tests: "test" + MethodName + "_" + description
    (use camel case either side of the underscore, e.g. testFoo_barBaz,
    not testfoo_barBaz, nor testFoo_BarBaz, nor testfoo_barbaz, etc.)
 - Sandbox module tests: As above, suffixed with "__sandbox"
    (note the *double* undersecore)
This ensures the results page has descriptive names for all tests, and that
sandbox tests appear directly after live module tests.
]]--


---------- 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 == talkWikitext, "Subject page is different to talk page")
end
function suite:testGetWikitext_fromArticle__sandbox() return self:testGetWikitext_fromArticle(sandbox) end

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'")
end
function 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")
end
function 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("<pre>#REDIRECT [[Foo]]</pre>"), "Within pre tags")
end
function 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")
end
function suite:testIsRedirect_fromPages__sandbox() return self:testIsRedirect_fromPages(sandbox) end


---------- classByNamespace ----------------------------------------------------

function suite:testClassByNamesapce_forEachNS(modToTest)
	modToTest = modToTest or liveMod
	local data = { -- table of page names and their expected classes
		["Foo"] = "",
		["User:Foo"] = "NA" ,
		["Wikipedia:Foo"] = "Project",
		["File:Foo.ext"] = "File",
		["MediaWiki:Foo"] = "NA",
		["Template:Foo"] = "Template",
		["Help:Foo"] = "Project",
		["Category:Foo"] = "Category",
		["Portal:Foo"] = "Portal",
		["Draft:Foo"] = "Draft",
		["TimedText:Foo"] = "File",
		["Module:Foo"] = "Module",
		["Book:Foo"] = "Book",
	}
	for page, class in ipairs(data) do
		self:assertEquals(
			modToTest.classByNamespace(page),
			class,
			string.format("'%s' get assessed as '%s'", page, class)
		)
	end
end
function suite:testClassByNamesapce_forEachNS__sandbox() return self:testClassByNamesapce_forEachNS(sandbox) end


---------- normaliseRating -----------------------------------------------------

function suite:testNormaliseRating_forEach(modToTest)
	modToTest = modToTest or liveMod
	local data = {
		[""] = {"", nil},
		FA = {"FA", "fa", "Fa"},
		FL = {"FL", "fl", "Fl"},
		FM = {"FM", "fm", "Fm"},
		A = {"A", "a"},
		B = {"B", "b"},
		C = {"C", "c"},
		Start = {"Start", "start"},
		Stub = {"Stub", "stub"},
		List = {"List", "list"},
		File = {"File", "file", "Image", "image", "img"}
	}
	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
	end
end
function 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")
	end
end
function suite:testClass_forStringOutput__sandbox() return suite:testClass_forStringOutput(sandbox) end


return suite