https://www.mediawiki.org/wiki/Special:Code/MediaWiki/111995
Revision: 111995 Author: santhosh Date: 2012-02-21 09:32:35 +0000 (Tue, 21 Feb 2012) Log Message: ----------- Make the input method rules of Narayam testable. Remove some of the tests added in r111990 since they need to be rewritten in ext.narayam.rules.tests.js using keypress simulation Modified Paths: -------------- trunk/extensions/Narayam/resources/ext.narayam.core/ext.narayam.core.js trunk/extensions/Narayam/tests/qunit/ext.narayam.tests.js Added Paths: ----------- trunk/extensions/Narayam/tests/qunit/ext.narayam.rules.tests.js Modified: trunk/extensions/Narayam/resources/ext.narayam.core/ext.narayam.core.js =================================================================== --- trunk/extensions/Narayam/resources/ext.narayam.core/ext.narayam.core.js 2012-02-21 09:26:26 UTC (rev 111994) +++ trunk/extensions/Narayam/resources/ext.narayam.core/ext.narayam.core.js 2012-02-21 09:32:35 UTC (rev 111995) @@ -439,8 +439,9 @@ /** * Change the current transliteration scheme * @param name String + * @param callback Function to be called when the scheme is ready/dynamically loaded.- Optional */ - this.setScheme = function( name ) { + this.setScheme = function( name, callback ) { var recent = $.cookie( 'narayam-scheme' ) || []; if ( typeof recent === "string" ) { recent = recent.split( "," ); @@ -454,10 +455,12 @@ $.cookie( 'narayam-scheme', recent, { path: '/', expires: 30 } ); if ( name in schemes ) { currentScheme = schemes[name]; + if ( callback ) callback.call(); } else { // load the rules dynamically. mw.loader.using( "ext.narayam.rules." + name, function() { currentScheme = schemes[name]; + if ( callback ) callback.call(); } ); } return true; Added: trunk/extensions/Narayam/tests/qunit/ext.narayam.rules.tests.js =================================================================== --- trunk/extensions/Narayam/tests/qunit/ext.narayam.rules.tests.js (rev 0) +++ trunk/extensions/Narayam/tests/qunit/ext.narayam.rules.tests.js 2012-02-21 09:32:35 UTC (rev 111995) @@ -0,0 +1,124 @@ +/** + * QUnit tests for Narayam typing rules + * + * @file + * @copyright Copyright © 2012 Santhosh Thottingal + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later + */ +( function () { + +module( "ext.narayam.rules", QUnit.newMwEnvironment() ); + +function setup() { + $.narayam.setup(); + $.narayam.enable(); +} +function teardown() { + // we need to disable narayam, otherwise many typing simulation based test eg: jquery.byteLimitTest will fail. + $.narayam.disable(); +} +test( "-- Initial check", function() { + expect( 1 ); + ok( $.narayam, "$.narayam is defined" ); +} ); + +// Basic sendkey-implementation +typeChars = function( $input, charstr ) { + var len = charstr.length; + for ( var i = 0; i < len; i++ ) { + // Get the key code + var code = charstr.charCodeAt(i); + // Trigger event and undo if prevented + var event = new jQuery.Event( 'keypress', { keyCode: code, which: code, charCode: code } ); + $input.trigger( event ); + } +}; + +/** + * Test factory for narayamTest + */ +var narayamTest = function( options ) { + var opt = $.extend( { + description: '', // Test description + $input: null, + tests: [], + scheme: '' // The input method name. + }, options); + + test( opt.description, function() { + expect( opt.tests.length); + $.narayam.enable( ); + stop(); + $.narayam.setScheme( opt.scheme, function(){ + opt.$input.appendTo( '#qunit-fixture' ); + $.narayam.addInputs (opt.$input); + $.narayam.setScheme( opt.scheme ); + for ( var i= 0 ; i < opt.tests.length; i++ ) { + // Simulate pressing keys for each of the sample characters + typeChars( opt.$input, opt.tests[i].input ); + equals( opt.$input.val(), opt.tests[i].output, opt.tests[i].description ); + opt.$input.val(''); + } + $.narayam.disable(); + start(); + }); + } ); +}; + +narayamTest ( { + description: 'Malayalam Transliteration test', + tests: [ + { input : 'ra', output : 'ര', description : 'Malayalam ra'}, + { input : 'nta', output : 'ന്റ', description : 'Malayalam nta'} + ], + scheme : 'ml', + $input: $( '<input >' ).attr( { 'id':"ml", 'type' :'text' } ) +} ); + +narayamTest ( { + description: 'Oriya Inscript test', + tests : [ + { input : 'ka', output : 'କୋ' } + ], + scheme : 'or-inscript', + $input: $( '<input >' ).attr( { 'id':"or", 'type' :'text' } ) +} ); + +narayamTest ( { + description: 'Malayalam Inscript test', + tests : [ + { input : 'ka', output : 'കോ' } + ], + scheme : 'ml-inscript', + $input: $( '<input >' ).attr( { 'id':"ml-inscript", 'type' :'text' } ) +} ); + +narayamTest ( { + description: 'Tamil Inscript test', + tests : [ + { input : 'ka', output : 'கோ', description : 'Tamil Inscript கோ' } + ], + scheme : 'ta-inscript', + $input: $( '<input >' ).attr( { 'id':"ta-inscript", 'type' :'text' } ) +} ); + +narayamTest ( { + description: 'Amharic Transliteration test', + tests : [ + { input : 'ka', output : 'ካ', description : 'Amharic ka->ካ' } + ], + scheme : 'am', + $input: $( '<input >' ).attr( { 'id':"am", 'type' :'text' } ) +} ); + +narayamTest ( { + description: 'Marathi Transliteration test', + tests : [ + { input : 'dny', output : 'ज्ञ्', description : 'dny for ज्ञ् in Marathi transliteration' } + ], + scheme : 'mr', + $input: $( '<input >' ).attr( { 'id':"mr", 'type' :'text' } ) +} ); + +teardown( ); +} ( ) ); Property changes on: trunk/extensions/Narayam/tests/qunit/ext.narayam.rules.tests.js ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/extensions/Narayam/tests/qunit/ext.narayam.tests.js =================================================================== --- trunk/extensions/Narayam/tests/qunit/ext.narayam.tests.js 2012-02-21 09:26:26 UTC (rev 111994) +++ trunk/extensions/Narayam/tests/qunit/ext.narayam.tests.js 2012-02-21 09:32:35 UTC (rev 111995) @@ -87,54 +87,5 @@ teardown(); } ); -test( '-- German transliteration and keybuffers', function() { - expect( 3 ); - setup(); - // Testing keybuffer ("compose key") - $.narayam.setScheme( 'de' ); - equals( $.narayam.transliterate( '~o', '~', false ), 'ö', 'German ~o -> ö' ); - equals( $.narayam.transliterate( '~O', '~', false ), 'Ö', 'German ~O -> Ö' ); - equals( $.narayam.transliterate( '~s', '~', false ), 'ß', 'German ~s -> ß' ); - - teardown(); -} ); - -test( '-- Hebrew transliteration, extended keyboard', function() { - expect( 2 ); - setup(); - - // Testing extended and non-extended - $.narayam.setScheme( 'he-standard-2011-extonly' ); - equals( $.narayam.transliterate( '=', '', false ), '=', 'Hebrew non-extended = does not change' ); - equals( $.narayam.transliterate( '=', '', true ), '–', 'Hebrew extended = becomes en dash' ); - - teardown(); -} ); - -test( '-- Malayalam transliteration, cookie, zwnj, longer keybuffers', function() { - expect( 8 ); - setup(); - - $.narayam.setScheme( 'kn' ); - var recentSchemes = $.cookie( 'narayam-scheme' ), - currentSchemeRegex = new RegExp( '^kn' ); - ok ( currentSchemeRegex.test( recentSchemes ), 'New scheme added to the cookie' ); - - $.narayam.setScheme( 'ml' ); - - equals( $.narayam.transliterate( 'a', '', false ), 'അ', 'Malayalam a -> അ' ); - - // N.B.: There's a zwnj in the input, and no zwnj in the expected result - equals( $.narayam.transliterate( 'നീലa', '', false ), 'നീലഅ', 'Malayalam zwnj+a -> അ' ); - equals( $.narayam.transliterate( 'ൻൿh', 'nc', false ), 'ഞ്ച്', 'Malayalam nch -> ഞ്ച്' ); - - equals( $.narayam.transliterate( 'p', '', false ), 'പ്', 'Malayalam p -> പ്' ); - equals( $.narayam.transliterate( 'പ്a', '', false ), 'പ', 'Malayalam pa -> പ' ); - equals( $.narayam.transliterate( 'ക്h', '', false ), 'ഖ്', 'Malayalam kh -> ഖ്' ); - equals( $.narayam.transliterate( 'ഖ്a', '', false ), 'ഖ', 'Malayalam kha -> ഖ്' ); - - teardown(); -} ); - }()); _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs