Skip to content

Instantly share code, notes, and snippets.

@he7d3r
Created September 15, 2014 13:20
Show Gist options
  • Save he7d3r/0dce20bd839de42783ca to your computer and use it in GitHub Desktop.
Save he7d3r/0dce20bd839de42783ca to your computer and use it in GitHub Desktop.
LanguageConverter tests
/**
* MediaWiki JavaScript library test suite
*
* Available on Special:BlankPage?action=lctest&debug=true
* @source Adapted from
* https://www.mediawiki.org/wiki/Special:Code/MediaWiki/87360
*/
/*jslint browser: true, white: true, evil: true, plusplus: true, vars: true, forin: true */
/*global jQuery, mediaWiki */
( function( mw, $ ) {
'use strict';
mw.test = {
/* Variables */
'$table' : null,
// contains either a header or a test
// test: [ code, result, contain ] see addTest
// header: [ 'HEADER', escapedtitle, id ] see addHead
'addedTests' : [],
'headResults' : [],
'numberOfHeader' : 0,
'dictForTests' : {
'activado': 'ativado',
'sistema operativo': 'sistema operacional',
//'Sistema Operativo': 'Sistema Operacional', //Será possível evitar esta duplicação?
'álbum de cromos': 'álbum de figurinhas'
//'Álbum De Cromos': 'Álbum De Figurinhas' //Será possível evitar esta duplicação?
},
/* Functions */
/**
* Adds a row to the test-table
*
* @param code String Code of the test to be executed
* @param result String Expected result in 'var (vartype)' form
* @param contain String Important part of the result,
* if result is different but does contain this it will not return ERROR but PARTIALLY
*/
'addTest' : function( code, result, contain ) {
if ( !contain ) {
contain = result;
}
this.addedTests.push( [code, result, contain] );
this.$table.append( '<tr class="mw-lctest-test">'
+ '<td>' + mw.html.escape( code ).replace( / {2}/g, '&nbsp;&nbsp;' )
+ '</td><td>' + mw.html.escape( result ).replace( / {2}/g, '&nbsp;&nbsp;' )
+ '</td><td></td><td>?</td></tr>' );
return true;
},
/**
* Adds a heading to the test-table
*
* @param title String Title of the section
*/
'addHead' : function( title ) {
if ( !title ) {
return false;
}
var escapedtitle = mw.html.escape( title ).replace( / {2}/g, '&nbsp;&nbsp;' );
this.addedTests.push( [ 'HEADER', escapedtitle, mw.test.numberOfHeader++ ] );
this.$table.append( '<tr class="mw-lctest-head" id="mw-lctest-head'+mw.test.numberOfHeader+'"><th colspan="4">' + escapedtitle + '</th></tr>' );
return true;
},
/* Initialisation */
'initialised' : false,
'init' : function() {
if ( this.initialised === false ) {
this.initialised = true;
// jQuery document ready
$( function() {
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Blankpage'
&& mw.util.getParamValue( 'action' ) === 'lctest' ) {
// Build page
document.title = 'Suíte de testes do script Conversor de Idiomas - ' + mw.config.get( 'wgSiteName' );
$( '#firstHeading' ).text( 'Suíte de testes do script Conversor de Idiomas' );
var skinLinksText = 'Testar com: ',
skinLinks = [],
availableSkins = mw.config.get( 'wgAvailableSkins' ),
skincode = '';
for ( skincode in availableSkins ) {
skinLinks.push( mw.html.element( 'a', {
'href': mw.util.getUrl( mw.config.get( 'wgPageName' ) ) + '?action=lctest&debug=true&useskin=' + encodeURIComponent( skincode )
}, availableSkins[skincode] ) );
}
skinLinksText += skinLinks.join( ' | ' ) + '.';
mw.util.$content.html(
'<p>Abaixo está uma lista de testes para confirmar o funcionamento adequado do script conversor de idiomas</p>'
+ '<p>' + skinLinksText + '</p>'
+ '<hr />'
+ '<table id="mw-lctest-table" class="wikitable" style="white-space:break; font-family:monospace,\'Courier New\'; width:100%;">'
+ '<tr><th>Executar</th><th>Deve produzir</th><th>Produz</th><th>Iguais ?</th></tr>'
+ '</table>'
);
mw.util.addCSS(
'#mw-lctest-table tr td { padding:0 !important; }' // Override wikitable padding for <td>
);
mw.test.$table = $( 'table#mw-lctest-table' );
/* Language conversion tests */
mw.test.addHead( 'Regras com uma única palavra' );
mw.test.addTest('LanguageConverter.conv_text_from_dic(' +
'"O carvão activado tem...", mw.test.dictForTests, false, null, false )',
"O carvão ativado tem... (string)" );
mw.test.addTest('LanguageConverter.conv_text_from_dic(' +
'"Activado", mw.test.dictForTests, false, null, false )',
"Ativado (string)" );
mw.test.addTest('LanguageConverter.conv_text_from_dic(' +
'"ACTIVADO", mw.test.dictForTests, false, null, false )',
"ATIVADO (string)" );
mw.test.addHead( 'Regras com 2 palavras' );
mw.test.addTest('LanguageConverter.conv_text_from_dic(' +
'"Unix é um sistema operativo portátil.", mw.test.dictForTests, false, null, false )',
"Unix é um sistema operacional portátil. (string)" );
mw.test.addTest('LanguageConverter.conv_text_from_dic(' +
'"Sistema operativo é...", mw.test.dictForTests, false, null, false )',
"Sistema operacional é... (string)" );
mw.test.addTest('LanguageConverter.conv_text_from_dic(' +
'"Sistema Operativo", mw.test.dictForTests, false, null, false )',
"Sistema Operacional (string)" );
mw.test.addTest('LanguageConverter.conv_text_from_dic(' +
'"SISTEMA OPERATIVO", mw.test.dictForTests, false, null, false )',
"SISTEMA OPERACIONAL (string)" );
mw.test.addHead( 'Regras com 3 palavras' );
mw.test.addTest('LanguageConverter.conv_text_from_dic(' +
'"um álbum de cromos...", mw.test.dictForTests, false, null, false )',
"um álbum de figurinhas... (string)" );
mw.test.addTest('LanguageConverter.conv_text_from_dic(' +
'"Álbum De Cromos", mw.test.dictForTests, false, null, false )',
"Álbum De Figurinhas (string)" );
mw.test.addTest('LanguageConverter.conv_text_from_dic(' +
'"ÁLBUM DE CROMOS", mw.test.dictForTests, false, null, false )',
"ÁLBUM DE FIGURINHAS (string)" );
// End of tests.
mw.test.addHead( '*** Fim dos testes ***' );
// Run tests and compare results
var exec,
//result,
//resulttype,
numberOfTests = 0,
numberOfPasseds = 0,
numberOfPartials = 0,
numberOfErrors = 0,
headNumberOfTests = 0,
headNumberOfPasseds = 0,
headNumberOfPartials = 0,
headNumberOfErrors = 0,
numberOfHeaders = 0,
previousHeadTitle = '',
$testrows = mw.test.$table.find( 'tr:has(td)' );
$.each( mw.test.addedTests, function( i, item ) {
var doesReturn;
// New header
if( item[0] === 'HEADER' ) {
// update current header with its tests results
mw.test.$table.find( 'tr#mw-lctest-head' + numberOfHeaders +' > th' )
.html( previousHeadTitle + ' <span style="float:right">('
+ 'Testes: ' + headNumberOfTests
+ ' OK: ' + headNumberOfPasseds
+ ' Parcial: ' + headNumberOfPartials
+ ' Erro: ' + headNumberOfErrors
+ ')</span>' );
numberOfHeaders++;
// Reset values for the new header;
headNumberOfTests = 0;
headNumberOfPasseds = 0;
headNumberOfPartials = 0;
headNumberOfErrors = 0;
previousHeadTitle = item[1];
return true;
}
exec = item[0];
var shouldreturn = item[1];
var shouldcontain = item[2];
numberOfTests++;
headNumberOfTests++;
try {
doesReturn = eval( exec );
} catch (e){
mw.log ('mw.util.test> ' + e );
}
doesReturn = doesReturn + ' (' + typeof doesReturn + ')';
var $thisrow = $testrows.eq( i - numberOfHeaders ); // since headers are rows as well
$thisrow.find( '> td' ).eq(2).html( mw.html.escape( doesReturn ).replace(/ {2}/g, '&nbsp;&nbsp;' ) );
if ( doesReturn.indexOf( shouldcontain ) !== -1 ) {
if ( doesReturn === shouldreturn ) {
$thisrow.find( '> td' ).eq(3).css( 'background', '#AFA' ).text( 'OK' );
numberOfPasseds++;
headNumberOfPasseds++;
} else {
$thisrow.find( '> td' ).eq(3).css( 'background', '#FFA' ).html( '<small>PARTIALLY</small>' );
numberOfPartials++;
headNumberOfPartials++;
}
} else {
$thisrow.css( 'background', '#FAA' ).find( '> td' ).eq(3).text( 'ERROR' );
numberOfErrors++;
headNumberOfErrors++;
}
} );
mw.test.$table.before( '<p><strong>Executados ' + numberOfTests + ' testes. ' +
numberOfPasseds + ' testes passaram. ' + numberOfErrors + ' erro(s). ' +
numberOfPartials + 'testes passaram parcialmente. </p>' );
}
} );
}
}
};
mw.test.init();
}(mediaWiki, jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment