Jump to content

User:Zackmann08/scripts/GenerateTestcase.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<nowiki>
jQuery(document).ready(function($) {

// Only works on pages containing "testcases" in the pagename
if(mw.config.get('wgPageName').toLowerCase().includes('testcases') && document.getElementsByName('wpTextbox1')[0]) {
	mw.loader.using(['mediawiki.util']).done( function() {
		var portletlink = mw.util.addPortletLink('p-tb','#','GenTestcase','t-gentestcases');
		$(portletlink).click(function(e) {
			e.preventDefault();
			generateTestcase();
		});
	});
}
// -------------------------------------------------------------------------------- //
function generateTestcase()
{
	var mycontent = document.getElementById('wpTextbox1');
	// Copy the contents of the text window so we can modify it without problems
	var mytxt = mycontent.value;
	var original_text = mycontent.value;
	
	const params_regex = /\{\{\#invoke:Check for unknown.*ignoreblank\s*=\s*y\s*\|(mapframe_args\s*=\s*y\s*\|)?(.*)\}\}<noinclude>[\w\W]*/i;
	var param_text = original_text.match(params_regex);
	
	if (param_text == null){
		alert('Match not found');
	}
	
	param_text = original_text.match(params_regex)[2];
	
	var final_text = '== All params ==\n{{testcase table\n';
	
	var params = param_text.split('|');
	for (let i = 0; i<params.length; i++){
		final_text = final_text + '| ' + params[i].trim() + ' = {{{' + params[i].trim() + '}}}\n'; 
	}
	
	final_text = final_text + '}}';
	final_text = original_text.replace(params_regex, final_text);
	
	mycontent.value = final_text;
	
	var mysummary = 'Adding all params testcase';
	var editsummary = document.getElementsByName('wpSummary')[0];
    	if(typeof editsummary == 'object') {
    	if (editsummary.value.indexOf(mysummary) == -1) {
        if (editsummary.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
          editsummary.value += '; ' + mysummary;
        } else {
          editsummary.value += mysummary;
        }
      }
    }
    
}
// -------------------------------------------------------------------------------- //
});
//</nowiki>