From: Timo Tijhof Date: Sat, 12 May 2012 22:37:30 +0000 (+0200) Subject: Allow custom teardown/setup per module through Qunit.newMwEnvironment X-Git-Tag: 1.31.0-rc.0~23626 X-Git-Url: http://git.cyclocoop.org/?a=commitdiff_plain;h=498ef943ddee8b0fcc78c2bc8d0abc1cd0fc5223;p=lhc%2Fweb%2Fwiklou.git Allow custom teardown/setup per module through Qunit.newMwEnvironment - Instead of having newMwEnvironment only create an object with setup() and teardown() that is passed to module() which uses them. Also allow a module test to pass additional setup/teardown to run. Otherwise one would have to extend newMwEnvironment, this is simpler. - Also added a few unit tests of the test runner's newMwEnvironment method itself. (also serves as document/example for how to use it) - This changes the signature of QUnit.newMwEnvironment. Was only used in 2 places in core. Adding release notes just in case. - Needed to fix a unit test breakage in mw.Uri Change-Id: I2c17f4a309208276ab7bd2420b720c0d8e4bf328 --- diff --git a/RELEASE-NOTES-1.20 b/RELEASE-NOTES-1.20 index d36f840f6b..35a3790004 100644 --- a/RELEASE-NOTES-1.20 +++ b/RELEASE-NOTES-1.20 @@ -56,7 +56,10 @@ upgrade PHP if you have not done so prior to upgrading MediaWiki. * (bug 5445) Now remove autoblocks when a user is unblocked. * Added $wgLogExceptionBacktrace, on by default, to allow logging of exception backtraces. -* Added device detection for determining device capabilities +* Added device detection for determining device capabilities. +* QUnit.newMwEnvironment now supports passing a custom setup and/or teardown function. + Arguments signature has changed. First arguments is now an options object of which + 'config' can be a property. Previously 'config' itself was the first and only argument. === Bug fixes in 1.20 === * (bug 30245) Use the correct way to construct a log page title. diff --git a/tests/qunit/data/testrunner.js b/tests/qunit/data/testrunner.js index f4baf95552..f3176abf72 100644 --- a/tests/qunit/data/testrunner.js +++ b/tests/qunit/data/testrunner.js @@ -84,38 +84,17 @@ if ( QUnit.urlParams.completenesstest ) { QUnit.config.urlConfig.push( 'mwlogenv' ); /** - * Reset mw.config to a fresh copy of the live config for each test(); - * @param override {Object} [optional] - * @example: - * - * module( .., newMwEnvironment() ); - * - * test( .., function () { - * mw.config.set( 'foo', 'bar' ); // just for this test - * } ); - * - * test( .., function () { - * mw.config.get( 'foo' ); // doesn't exist - * } ); - * - * - * module( .., newMwEnvironment({ quux: 'corge' }) ); - * - * test( .., function () { - * mw.config.get( 'quux' ); // "corge" - * mw.config.set( 'quux', "grault" ); - * } ); - * - * test( .., function () { - * mw.config.get( 'quux' ); // "corge" - * } ); + * Reset mw.config and others to a fresh copy of the live config for each test(), + * and restore it back to the live one afterwards. + * @param localEnv {Object} [optional] + * @example (see test suite at the bottom of this file) * */ QUnit.newMwEnvironment = ( function () { - var log, liveConfig, liveMsgs; + var log, liveConfig, liveMessages; liveConfig = mw.config.values; - liveMsgs = mw.messages.values; + liveMessages = mw.messages.values; function freshConfigCopy( custom ) { // "deep=true" is important here. @@ -123,34 +102,46 @@ QUnit.newMwEnvironment = ( function () { // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig, // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array // was passed by reference in $.extend's loop. - return $.extend({}, liveConfig, custom, /*deep=*/true ); + return $.extend( {}, liveConfig, custom, /*deep=*/true ); } - function freshMsgsCopy( custom ) { - return $.extend({}, liveMsgs, custom, /*deep=*/true ); + function freshMessagesCopy( custom ) { + return $.extend( {}, liveMessages, custom, /*deep=*/true ); } log = QUnit.urlParams.mwlogenv ? mw.log : function () {}; - return function ( overrideConfig, overrideMsgs ) { - overrideConfig = overrideConfig || {}; - overrideMsgs = overrideMsgs || {}; + return function ( localEnv ) { + localEnv = $.extend( { + // QUnit + setup: $.noop, + teardown: $.noop, + // MediaWiki + config: {}, + messages: {} + }, localEnv ); return { setup: function () { log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module + ': ' + QUnit.config.current.testName + '"' ); + // Greetings, mock environment! - mw.config.values = freshConfigCopy( overrideConfig ); - mw.messages.values = freshMsgsCopy( overrideMsgs ); + mw.config.values = freshConfigCopy( localEnv.config ); + mw.messages.values = freshMessagesCopy( localEnv.messages ); + + localEnv.setup() }, teardown: function () { log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module + ': ' + QUnit.config.current.testName + '"' ); + + localEnv.teardown(); + // Farewell, mock environment! mw.config.values = liveConfig; - mw.messages.values = liveMsgs; + mw.messages.values = liveMessages; } }; }; @@ -200,4 +191,59 @@ addons = { $.extend( QUnit, addons ); $.extend( window, addons ); +/** + * Small test suite to confirm proper functionality of the utilities and + * initializations in this file. + */ +var envExecCount = 0; +module( 'mediawiki.tests.qunit.testrunner', QUnit.newMwEnvironment({ + setup: function () { + envExecCount += 1; + this.mwHtmlLive = mw.html; + mw.html = { + escape: function () { + return 'mocked-' + envExecCount; + } + }; + }, + teardown: function () { + mw.html = this.mwHtmlLive; + }, + config: { + testVar: 'foo' + }, + messages: { + testMsg: 'Foo.' + } +}) ); + +test( 'Setup', function () { + expect( 3 ); + + equal( mw.html.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' ); + equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' ); + equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' ); + + mw.config.set( 'testVar', 'bar' ); + mw.messages.set( 'testMsg', 'Bar.' ); +}); + +test( 'Teardown', function () { + expect( 3 ); + + equal( mw.html.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' ); + equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' ); + equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' ); +}); + +module( 'mediawiki.tests.qunit.testrunner-after', QUnit.newMwEnvironment() ); + +test( 'Teardown', function () { + expect( 3 ); + + equal( mw.html.escape( '<' ), '<', 'extra teardown() callback was ran.' ); + equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' ); + equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' ); +}); + })( jQuery, mediaWiki, QUnit ); diff --git a/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js b/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js index 95284c7d72..f028dbc936 100644 --- a/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js @@ -7,7 +7,7 @@ var config = { wgContentLanguage: 'en' }; -module( 'jquery.tablesorter', QUnit.newMwEnvironment( config ) ); +module( 'jquery.tablesorter', QUnit.newMwEnvironment({ config: config }) ); test( '-- Initial check', function() { expect(1); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js index e04111f106..7ff0fb82d9 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js @@ -55,7 +55,7 @@ var config = { "wgCaseSensitiveNamespaces": [] }; -module( 'mediawiki.Title', QUnit.newMwEnvironment( config ) ); +module( 'mediawiki.Title', QUnit.newMwEnvironment({ config: config }) ); test( '-- Initial check', function () { expect(1);