User:Evad37/extra/test.js

/*************************************************************************************************** ExtraJS testcases --- by Evad37 > These tests are run automatically on extraJs script pages when this script is installed. /* jshint esversion: 6, laxbreak: true, undef: true, maxerr:999 */ /* globals window, mw, $, QUnit, extraJs */ //

/* ========== Set up ============================================================================ */ if (	mw.config.get('wgPageName').includes('User:Evad37/extra') &&	mw.config.get('wgPageContentModel') === 'javascript' &&	mw.config.get('wgAction') === 'view' &&	mw.config.get('wgDiffOldId') === null ) {

var init = function {

var rawUrl = function(page) { var ctype = /\.css$/.test(page) ? 'text/css' : 'text/javascript'; return 'https://en.wikipedia.org/w/index.php?title=' + page.replace(/ /g, '_') + '&action=raw&ctype=' + ctype; }; // Create output div var $output = $(' ').attr('id', 'qunit').insertBefore('#firstHeading');

// Script version: use sandbox version, except on the main script page itself var scriptPage = mw.config.get('wgPageName') === 'User:Evad37/extra.js' ? 'User:Evad37/extra.js' : 'User:Evad37/extra/sandbox.js';

// Get that version of the script mw.loader.getScript( rawUrl(scriptPage) ).then(function {

// Load QUnit mw.loader.load( rawUrl('User:Evad37/qunit-2.8.0.css'), 'text/css' ); $.getScript( rawUrl('User:Evad37/qunit-2.8.0.js'), function{

// Note version of script being tested $(' ').append([	'Testing version ' + extraJs.version + ' (', $(scriptPage === mw.config.get('wgPageName') ? ' ' : '') .attr('href', 'https://en.wikipedia.org/wiki/'+scriptPage) .text(scriptPage), '):' ]).insertBefore( $output );

/* ========== Data to use in tests ============================================================== */ var original = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "+ "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud "+ "exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure "+ "dolors in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "+ "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt "+ "mollit anim id est laborum."; var listItems = "*Lorem\n*ipsum dolor\n*sit amet\n*consectetur"; var file1 = ""; var file2 = ""; var file3 = ""; assert.equal(unlinked3, expected3, 'Gallery image'); }); QUnit.test('Portals', function(assert) { var unlinked1 = extraJs.unlink(portal1, ['Portal:Foo'], 0); var expected1 = ""; assert.equal(unlinked1, expected1, 'Multiple portals in '); var unlinked2 = extraJs.unlink(portal2, ['Portal:Foo'], 0); var expected2 = ""; assert.equal(unlinked2, expected2, 'Single portal in '); var unlinked3 = extraJs.unlink(portal3, ['Portal:Foo'], 0); var expected3 = "Lorem ipsum.\n\n==See also==\n*\n\n==External links=="; assert.equal(unlinked3, expected3, 'List with multiple templates'); var unlinked4 = extraJs.unlink(portal4, ['Portal:Foo'], 0); var expected4 = "Lorem ipsum.\n\n==External links=="; assert.equal(unlinked4, expected4, 'List with single template'); var unlinked5 = extraJs.unlink(portal5, ['Portal:Foo'], 0); var expected5 = ""; assert.equal(unlinked5, expected5, 'Multiple portals in '); var unlinked6 = extraJs.unlink(portal6, ['Portal:Foo'], 0); var expected6 = ""; assert.equal(unlinked6, expected6, 'Single portal in '); var unlinked7 = extraJs.unlink(portal7, ['Portal:Foo'], 0); var expected7 = ""; assert.equal(unlinked7, expected7, ''); var unlinked8 = extraJs.unlink(portal8, ['Portal:Hip hop'], 0); var expected8 = '=== Section ===\nLorem ipsum.\n\n==References=='; assert.equal(unlinked8, expected8, ' at end of a line'); var unlinked9 = extraJs.unlink(portal9, ['Portal:Hip hop'], 0); var expected9 = '=== Section ===\nLorem ipsum.\n\n==References=='; assert.equal(unlinked9, expected9, ' on it\'s own line'); var unlinked10 = extraJs.unlink(portal10, ['Portal:United States Army'], 0); var expected10 = `

`;   assert.equal(unlinked10, expected10, ' per Special:Diff/903501933'); });

// ========== Test Module: extraJs.parseTemplates ======================================================= */ QUnit.module('extraJs.parseTemplates'); QUnit.test("Single template, no params", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext);	assert.equal(parsed.length, 1, "One template found");	assert.equal(parsed[0].name, "ipsum", "Correct name");	assert.equal(parsed[0].parameters.length, 0, "No parameters");	assert.equal(parsed[0].wikitext, "", "Correct wikitext"); }); QUnit.test("Two templates, no params", function(assert) {	var wikitext = "Lorem dorem NaN";	var parsed = extraJs.parseTemplates(wikitext);	assert.equal(parsed.length, 2, "Two template found");	assert.equal(parsed[0].name, "ipsum", "First: Correct name");	assert.equal(parsed[0].parameters.length, 0, "First: No parameters");	assert.equal(parsed[0].wikitext, "", "First: Correct wikitext");	assert.equal(parsed[1].name, "sum", "First: Correct name");	assert.equal(parsed[1].parameters.length, 0, "First: No parameters");	assert.equal(parsed[1].wikitext, "NaN", "First: Correct wikitext"); }); QUnit.test("Two nested templates, not recursive", function(assert) {	var wikitext = "Lorem sum";	var parsed = extraJs.parseTemplates(wikitext);	assert.equal(parsed.length, 1, "One template found");	assert.equal(parsed[0].name, "ipsum", "Correct name");	assert.equal(parsed[0].parameters.length, 1, "One parameter");	assert.equal(parsed[0].parameters[0].name, 1, "Correct parameter name");	assert.equal(parsed[0].parameters[0].value, "", "Correct parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|", "Correct parameter wikitext");	assert.equal(parsed[0].wikitext, "", "Correct wikitext"); }); QUnit.test("Two nested templates, recursive", function(assert) {	var wikitext = "Lorem sum";	var parsed = extraJs.parseTemplates(wikitext, true);	assert.equal(parsed.length, 2, "Two template found");	assert.equal(parsed[0].name, "ipsum", "Correct name");	assert.equal(parsed[0].parameters.length, 1, "One parameter");	assert.equal(parsed[0].parameters[0].name, 1, "Correct parameter name");	assert.equal(parsed[0].parameters[0].value, "", "Correct parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|", "Correct parameter wikitext");	assert.equal(parsed[0].wikitext, "", "Correct wikitext");	assert.equal(parsed[1].name, "dorem", "Correct name");	assert.equal(parsed[1].parameters.length, 0, "No parameters");	assert.equal(parsed[1].wikitext, "", "Correct wikitext"); }); QUnit.test("More nested templates, recursive (Talk:Eleanor Robinson)", function(assert) {	var wikitext = `

`;	var parsed = extraJs.parseTemplates(wikitext, true); assert.equal(parsed.length, 8, "Eight templates found"); assert.equal(parsed[0].name, "WikiProjectBannerShell", "First: Correct name"); assert.equal(parsed[0].parameters.length, 1, "First: One parameter"); assert.equal(parsed[0].parameters[0].name, "1", "First: Correct parameter name"); assert.equal(parsed[0].parameters[0].value, `

`, "First: Correct parameter value");	assert.equal(parsed[0].parameters[0].wikitext, `|1=

`, "First: Correct parameter wikitext");	assert.equal(parsed[0].wikitext, ``, "First: Correct wikitext"); });

QUnit.test("Unnamed parameter", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext);	assert.equal(parsed.length, 1, "One template found");	assert.equal(parsed[0].name, "ipsum", "Correct name");	assert.equal(parsed[0].parameters.length, 1, "One parameter");	assert.equal(parsed[0].parameters[0].name, 1, "Correct parameter name");	assert.equal(parsed[0].parameters[0].value, "foo", "Correct parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|foo", "Correct parameter wikitext");	assert.equal(parsed[0].wikitext, "", "Correct wikitext"); });

QUnit.test("Unnamed parameters", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext);	assert.equal(parsed.length, 1, "One template found");	assert.equal(parsed[0].name, "ipsum", "Correct name");	assert.equal(parsed[0].parameters.length, 2, "Two parameters");	assert.equal(parsed[0].parameters[0].name, 1, "Correct first parameter name");	assert.equal(parsed[0].parameters[0].value, "foo", "Correct first parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|foo", "Correct first parameter wikitext");	assert.equal(parsed[0].parameters[1].name, 2, "Correct second parameter name");	assert.equal(parsed[0].parameters[1].value, "bar", "Correct second parameter value");	assert.equal(parsed[0].parameters[1].wikitext, "|bar", "Correct second parameter wikitext");	assert.equal(parsed[0].wikitext, "", "Correct wikitext"); });

QUnit.test("Numbered parameters", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext);	assert.equal(parsed.length, 1, "One template found");	assert.equal(parsed[0].name, "ipsum", "Correct name");	assert.equal(parsed[0].parameters.length, 2, "Two parameters");	assert.equal(parsed[0].parameters[0].name, "2", "Correct first parameter name");	assert.equal(parsed[0].parameters[0].value, "foo", "Correct first parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|2=foo", "Correct first parameter wikitext");	assert.equal(parsed[0].parameters[1].name, "3", "Correct second parameter name");	assert.equal(parsed[0].parameters[1].value, "bar", "Correct second parameter value");	assert.equal(parsed[0].parameters[1].wikitext, "|3=bar", "Correct second parameter wikitext");	assert.equal(parsed[0].wikitext, "", "Correct wikitext"); });

QUnit.test("Contains triple-brace parameter", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext);	assert.equal(parsed.length, 1, "One template found");	assert.equal(parsed[0].name, "ipsum", "Correct name");	assert.equal(parsed[0].parameters.length, 1, "One parameter");	assert.equal(parsed[0].parameters[0].name, 1, "Correct parameter name");	assert.equal(parsed[0].parameters[0].value, "", "Correct parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|", "Correct parameter wikitext");	assert.equal(parsed[0].wikitext, "", "Correct wikitext"); });

QUnit.test("Four braces (template name is a template), non-recursive", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext);	assert.equal(parsed.length, 1, "One template found");	assert.equal(parsed[0].name, "", "Correct name");	assert.equal(parsed[0].parameters.length, 1, "One parameter");	assert.equal(parsed[0].parameters[0].name, 1, "Correct parameter name");	assert.equal(parsed[0].parameters[0].value, "bar", "Correct parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|bar", "Correct parameter wikitext");	assert.equal(parsed[0].wikitext, "", "Correct wikitext"); });

QUnit.test("Four braces (template name is a template), recursive", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext, true);	assert.equal(parsed.length, 2, "Two templates found");	assert.equal(parsed[0].name, "", "First: Correct name");	assert.equal(parsed[0].parameters.length, 1, "First: One parameter");	assert.equal(parsed[0].parameters[0].name, 1, "First: Correct parameter name");	assert.equal(parsed[0].parameters[0].value, "bar", "First: Correct parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|bar", "First: Correct parameter wikitext");	assert.equal(parsed[0].wikitext, "", "First: Correct wikitext");	assert.equal(parsed[1].name, "ipsum", "Second: Correct name");	assert.equal(parsed[1].parameters.length, 1, "Second: One parameter");	assert.equal(parsed[1].parameters[0].name, 1, "Second: Correct parameter name");	assert.equal(parsed[1].parameters[0].value, "one", "Second: Correct parameter value"); assert.equal(parsed[1].parameters[0].wikitext, "|one", "Second: Correct parameter wikitext"); assert.equal(parsed[1].wikitext, "", "Second: Correct wikitext"); });

QUnit.test("Five braces (template name is a triple-brace parameter), non-recursive", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext);	assert.equal(parsed.length, 1, "One template found");	assert.equal(parsed[0].name, "", "Correct name");	assert.equal(parsed[0].parameters.length, 1, "One parameter");	assert.equal(parsed[0].parameters[0].name, 1, "Correct parameter name");	assert.equal(parsed[0].parameters[0].value, "bar", "Correct parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|bar", "Correct parameter wikitext");	assert.equal(parsed[0].wikitext, "", "Correct wikitext"); });

QUnit.test("Five braces (template name is a triple-brace parameter), recursive", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext, true);	assert.equal(parsed.length, 1, "One template found");	assert.equal(parsed[0].name, "", "Correct name");	assert.equal(parsed[0].parameters.length, 1, "One parameter");	assert.equal(parsed[0].parameters[0].name, 1, "Correct parameter name");	assert.equal(parsed[0].parameters[0].value, "bar", "Correct parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|bar", "Correct parameter wikitext");	assert.equal(parsed[0].wikitext, "", "Correct wikitext"); });

QUnit.test("Six braces (template name is a template, which itself has a template name that is a template), recursive", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext, true);	assert.equal(parsed.length, 3, "Three templates found");	assert.equal(parsed[0].name, "", "First: Correct name");	assert.equal(parsed[0].parameters.length, 1, "First: One parameter");	assert.equal(parsed[0].parameters[0].name, 1, "First: Correct parameter name");	assert.equal(parsed[0].parameters[0].value, "baz", "First: Correct parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|baz", "First: Correct parameter wikitext");	assert.equal(parsed[0].wikitext, "", "First: Correct wikitext");	assert.equal(parsed[1].name, "", "Second: Correct name");	assert.equal(parsed[1].parameters.length, 1, "Second: One parameter");	assert.equal(parsed[1].parameters[0].name, 1, "Second: Correct parameter name"); assert.equal(parsed[1].parameters[0].value, "bar", "Second: Correct parameter value"); assert.equal(parsed[1].parameters[0].wikitext, "|bar", "Second: Correct parameter wikitext"); assert.equal(parsed[1].wikitext, "", "Second: Correct wikitext"); assert.equal(parsed[2].name, "ipsum", "Third: Correct name"); assert.equal(parsed[2].parameters.length, 1, "Third: One parameter"); assert.equal(parsed[2].parameters[0].name, 1, "Third: Correct parameter name"); assert.equal(parsed[2].parameters[0].value, "foo", "Third: Correct parameter value"); assert.equal(parsed[2].parameters[0].wikitext, "|foo", "Third: Correct parameter wikitext"); assert.equal(parsed[2].wikitext, "", "Third: Correct wikitext"); });

QUnit.test("Six braces (triple-brace parameter within triple-brace parameter), recursive", function(assert) {	var wikitext = "Lorem dorem";	var parsed = extraJs.parseTemplates(wikitext, true);	assert.equal(parsed.length, 1, "One template found");	assert.equal(parsed[0].name, "foo", "Correct name");	assert.equal(parsed[0].parameters.length, 2, "Two parameter");	assert.equal(parsed[0].parameters[0].name, 1, "Correct first parameter name");	assert.equal(parsed[0].parameters[0].value, "", "Correct first parameter value");	assert.equal(parsed[0].parameters[0].wikitext, "|", "Correct first parameter wikitext");	assert.equal(parsed[0].parameters[1].name, 2, "Correct second parameter name");	assert.equal(parsed[0].parameters[1].value, "baz", "Correct second parameter value");	assert.equal(parsed[0].parameters[1].wikitext, "|baz", "Correct second parameter wikitext");	assert.equal(parsed[0].wikitext, "", "Correct wikitext"); });

/* ========== End closures ====================================================================== */ }); // end of load QUnit callback }); // end of getScript callback }; // end of init function

// Wait for page load $(function($) {	// Wait a few more seconds, so hopefull other scripts have finished loading, then run init	window.setTimeout(init, 3000); });

} // end if statement

//