Module:Page assessment/testcases

.mw-parser-output .ombox{margin:4px 0;border-collapse:collapse;border:1px solid #a2a9b1;background-color:var(--background-color-neutral-subtle,#f8f9fa);box-sizi…

Module:Page assessment/testcases
-- 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 modToTest = liveMod

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.
]]--


---------- sandbox run ---------------------------------------------------------

function suite.runSandbox(...)
	modToTest = sandbox
	return suite.run(...)
end

---------- getWikitext ---------------------------------------------------------

function suite:testGetWikitext_fromArticle()
	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_fromTalk()
	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_nonExisting()
	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


---------- isRedirect ----------------------------------------------------------

function suite:testIsRedirect_fromWikitext()
	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_fromPages()
	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


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

function suite:testClassByNamesapce_forEachNS()
	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


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

function suite:testNormaliseRating_forEach()
	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


---------- class ---------------------------------------------------------------

function suite:testClass_forStringOutput()
	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


return suite

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.