From d0b27d21d9e3838b12ea598856f392711dab27e6 Mon Sep 17 00:00:00 2001 From: Krinkle Date: Fri, 10 Jun 2011 19:55:40 +0000 Subject: [PATCH] Review of unit test suites * There's more than ok(), equal() and deepEqual(). Using the others as appropriate. ** Considered: strictEqual(a,b) tests the same as ok(a===b). But the latter doesn't include the values in the report when it fails. So strictEqual saves a lot of time in debugging (especially on TestSwarm where the report is all you have, there "not okay" or "Expected { foo: 500 }, Given: ['bar', '250']" is a big saver. * Adding an "expect" to every test. * Applying whitespace conventions and bringing consistency in the use of mw, mediaWiki, $, $j, jQuery. (except in the initial test where the aliases are being checked) * Added cleanup for added CSSStyleSheet * Making IPtest more complete (Based on IPTest.php) --- tests/qunit/sample/awesome.js | 2 +- .../resources/jquery/jquery.autoEllipsis.js | 10 +- .../suites/resources/jquery/jquery.client.js | 25 ++- .../resources/jquery/jquery.colorUtil.js | 94 ++++----- .../resources/jquery/jquery.mwPrototypes.js | 62 +++--- .../resources/jquery/jquery.tabIndex.js | 35 ++- .../mediawiki.util/mediawiki.util.js | 199 ++++++++++++------ .../suites/resources/mediawiki/mediawiki.js | 134 ++++++------ .../resources/mediawiki/mediawiki.user.js | 25 ++- 9 files changed, 326 insertions(+), 260 deletions(-) diff --git a/tests/qunit/sample/awesome.js b/tests/qunit/sample/awesome.js index 3adc270677..d7852e3dd2 100644 --- a/tests/qunit/sample/awesome.js +++ b/tests/qunit/sample/awesome.js @@ -1,4 +1,4 @@ window.mw.loader.testCallback = function(){ start(); - deepEqual( true, true, 'Implementing a module, is the callback timed properly ?'); + ok( true, 'Implementing a module, is the callback timed properly ?'); }; diff --git a/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.js b/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.js index 6be5212e18..8a25439158 100644 --- a/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.js +++ b/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.js @@ -1,8 +1,8 @@ module( 'jquery.autoEllipsis.js' ); -test( '-- Initial check', function(){ +test( '-- Initial check', function() { expect(1); - ok( jQuery.fn.autoEllipsis, 'jQuery.fn.autoEllipsis defined' ); + ok( $.fn.autoEllipsis, 'jQuery.fn.autoEllipsis defined' ); }); function createWrappedDiv( text ) { @@ -31,11 +31,11 @@ test( 'Position right', function() { // Verify that, and only one, span element was created var $span = $wrapper.find( '> span' ); - deepEqual( $span.length, 1, 'autoEllipsis wrapped the contents in a span element' ); + strictEqual( $span.length, 1, 'autoEllipsis wrapped the contents in a span element' ); // Check that the text fits by turning on word wrapping $span.css( 'whiteSpace', 'nowrap' ); - deepEqual( $span.width() <= $span.parent().width(), true, "Text fits (span's width is no larger than its parent's width)" ); + strictEqual( $span.width() <= $span.parent().width(), true, "Text fits (span's width is no larger than its parent's width)" ); // Add one character using scary black magic var spanText = $span.text(); @@ -44,7 +44,7 @@ test( 'Position right', function() { // Put this text in the span and verify it doesn't fit $span.text( spanText ); - deepEqual( $span.width() > $span.parent().width(), true, 'Fit is maximal (adding one character makes it not fit any more)' ); + strictEqual( $span.width() > $span.parent().width(), true, 'Fit is maximal (adding one character makes it not fit any more)' ); // Clean up $wrapper.remove(); diff --git a/tests/qunit/suites/resources/jquery/jquery.client.js b/tests/qunit/suites/resources/jquery/jquery.client.js index 268a2bc2a7..56d1aec05d 100644 --- a/tests/qunit/suites/resources/jquery/jquery.client.js +++ b/tests/qunit/suites/resources/jquery/jquery.client.js @@ -1,28 +1,27 @@ module( 'jquery.client.js' ); -test( '-- Initial check', function(){ +test( '-- Initial check', function() { expect(1); ok( jQuery.client, 'jQuery.client defined' ); }); -test( 'profile', function(){ +test( 'profile', function() { expect(7); var p = $.client.profile(); - var unk = 'unknown'; - var unkOrType = function( val, type ) { - return typeof val === type || val === unk; + var unknownOrType = function( val, type, summary ) { + return ok( typeof val === type || val === 'unknown', summary ); }; - equal( typeof p, 'object', 'profile() returns an object' ); - ok( unkOrType( p.layout, 'string' ), 'p.layout is a string (or "unknown")' ); - ok( unkOrType( p.layoutVersion, 'number' ), 'p.layoutVersion is a number (or "unknown")' ); - ok( unkOrType( p.platform, 'string' ), 'p.platform is a string (or "unknown")' ); - ok( unkOrType( p.version, 'string' ), 'p.version is a string (or "unknown")' ); - ok( unkOrType( p.versionBase, 'string' ), 'p.versionBase is a string (or "unknown")' ); + equal( typeof p, 'object', 'profile returns an object' ); + unknownOrType( p.layout, 'string', 'p.layout is a string (or "unknown")' ); + unknownOrType( p.layoutVersion, 'number', 'p.layoutVersion is a number (or "unknown")' ); + unknownOrType( p.platform, 'string', 'p.platform is a string (or "unknown")' ); + unknownOrType( p.version, 'string', 'p.version is a string (or "unknown")' ); + unknownOrType( p.versionBase, 'string', 'p.versionBase is a string (or "unknown")' ); equal( typeof p.versionNumber, 'number', 'p.versionNumber is a number' ); }); -test( 'test', function(){ +test( 'test', function() { expect(1); // Example from WikiEditor @@ -54,6 +53,6 @@ test( 'test', function(){ // then do a basic return value type check var testMatch = $.client.test( testMap ); - equal( typeof testMatch, 'boolean', 'test() returns a boolean value' ); + equal( typeof testMatch, 'boolean', 'test returns a boolean value' ); }); diff --git a/tests/qunit/suites/resources/jquery/jquery.colorUtil.js b/tests/qunit/suites/resources/jquery/jquery.colorUtil.js index 9ac289d3ae..01965a2f6c 100644 --- a/tests/qunit/suites/resources/jquery/jquery.colorUtil.js +++ b/tests/qunit/suites/resources/jquery/jquery.colorUtil.js @@ -1,73 +1,71 @@ module( 'jquery.colorUtil.js' ); -test( '-- Initial check', function(){ +test( '-- Initial check', function() { expect(1); - ok( jQuery.colorUtil, 'jQuery.colorUtil defined' ); + ok( $.colorUtil, '$.colorUtil defined' ); }); -test( 'getRGB', function(){ +test( 'getRGB', function() { expect(18); - equal( typeof jQuery.colorUtil.getRGB(), 'undefined', 'No arguments' ); - equal( typeof jQuery.colorUtil.getRGB( '' ), 'undefined', 'Empty string' ); - deepEqual( jQuery.colorUtil.getRGB( [0, 100, 255] ), [0, 100, 255], 'Array' ); - deepEqual( jQuery.colorUtil.getRGB( 'rgb(0,100,255)' ), [0, 100, 255], 'Parse simple string' ); - deepEqual( jQuery.colorUtil.getRGB( 'rgb(0, 100, 255)' ), [0, 100, 255], 'Parse simple string (whitespace)' ); - deepEqual( jQuery.colorUtil.getRGB( 'rgb(0%,20%,40%)' ), [0, 51, 102], 'Parse percentages string' ); - deepEqual( jQuery.colorUtil.getRGB( 'rgb(0%, 20%, 40%)' ), [0, 51, 102], 'Parse percentages string (whitespace)' ); - deepEqual( jQuery.colorUtil.getRGB( '#f2ddee' ), [242, 221, 238], 'Hex string: 6 char lowercase' ); - deepEqual( jQuery.colorUtil.getRGB( '#f2DDEE' ), [242, 221, 238], 'Hex string: 6 char uppercase' ); - deepEqual( jQuery.colorUtil.getRGB( '#f2DdEe' ), [242, 221, 238], 'Hex string: 6 char mixed' ); - deepEqual( jQuery.colorUtil.getRGB( '#eee' ), [238, 238, 238], 'Hex string: 3 char lowercase' ); - deepEqual( jQuery.colorUtil.getRGB( '#EEE' ), [238, 238, 238], 'Hex string: 3 char uppercase' ); - deepEqual( jQuery.colorUtil.getRGB( '#eEe' ), [238, 238, 238], 'Hex string: 3 char mixed' ); - deepEqual( jQuery.colorUtil.getRGB( 'rgba(0, 0, 0, 0)' ), [255, 255, 255], 'Zero rgba for Safari 3; Transparent (whitespace)' ); - // Perhaps this is a bug in colorUtil, but it is the current behaviour so, let's keep track - // would that ever change - equal( typeof jQuery.colorUtil.getRGB( 'rgba(0,0,0,0)' ), 'undefined', 'Zero rgba without whitespace' ); - - deepEqual( jQuery.colorUtil.getRGB( 'lightGreen' ), [144, 238, 144], 'Color names (lightGreen)' ); - deepEqual( jQuery.colorUtil.getRGB( 'lightGreen' ), [144, 238, 144], 'Color names (transparent)' ); - equal( typeof jQuery.colorUtil.getRGB( 'mediaWiki' ), 'undefined', 'Inexisting color name' ); - + strictEqual( $.colorUtil.getRGB(), undefined, 'No arguments' ); + strictEqual( $.colorUtil.getRGB( '' ), undefined, 'Empty string' ); + deepEqual( $.colorUtil.getRGB( [0, 100, 255] ), [0, 100, 255], 'Parse array of rgb values' ); + deepEqual( $.colorUtil.getRGB( 'rgb(0,100,255)' ), [0, 100, 255], 'Parse simple rgb string' ); + deepEqual( $.colorUtil.getRGB( 'rgb(0, 100, 255)' ), [0, 100, 255], 'Parse simple rgb string with spaces' ); + deepEqual( $.colorUtil.getRGB( 'rgb(0%,20%,40%)' ), [0, 51, 102], 'Parse rgb string with percentages' ); + deepEqual( $.colorUtil.getRGB( 'rgb(0%, 20%, 40%)' ), [0, 51, 102], 'Parse rgb string with percentages and spaces' ); + deepEqual( $.colorUtil.getRGB( '#f2ddee' ), [242, 221, 238], 'Hex string: 6 char lowercase' ); + deepEqual( $.colorUtil.getRGB( '#f2DDEE' ), [242, 221, 238], 'Hex string: 6 char uppercase' ); + deepEqual( $.colorUtil.getRGB( '#f2DdEe' ), [242, 221, 238], 'Hex string: 6 char mixed' ); + deepEqual( $.colorUtil.getRGB( '#eee' ), [238, 238, 238], 'Hex string: 3 char lowercase' ); + deepEqual( $.colorUtil.getRGB( '#EEE' ), [238, 238, 238], 'Hex string: 3 char uppercase' ); + deepEqual( $.colorUtil.getRGB( '#eEe' ), [238, 238, 238], 'Hex string: 3 char mixed' ); + deepEqual( $.colorUtil.getRGB( 'rgba(0, 0, 0, 0)' ), [255, 255, 255], 'Zero rgba for Safari 3; Transparent (whitespace)' ); + + // Perhaps this is a bug in colorUtil, but it is the current behaviour so, let's keep + // track of it, so we will know in case it would ever change. + strictEqual( $.colorUtil.getRGB( 'rgba(0,0,0,0)' ), undefined, 'Zero rgba without whitespace' ); + + deepEqual( $.colorUtil.getRGB( 'lightGreen' ), [144, 238, 144], 'Color names (lightGreen)' ); + deepEqual( $.colorUtil.getRGB( 'lightGreen' ), [144, 238, 144], 'Color names (transparent)' ); + strictEqual( $.colorUtil.getRGB( 'mediaWiki' ), undefined, 'Inexisting color name' ); }); -test( 'rgbToHsl', function(){ - expect(4); +test( 'rgbToHsl', function() { + expect(1); + + var hsl = $.colorUtil.rgbToHsl( 144, 238, 144 ); - var hsl = jQuery.colorUtil.rgbToHsl( 144, 238, 144 ); + // Cross-browser differences in decimals... + // Round to two decimals so they can be more reliably checked. var dualDecimals = function(a,b){ return Math.round(a*100)/100; }; + // Re-create the rgbToHsl return array items, limited to two decimals. + var ret = [dualDecimals(hsl[0]), dualDecimals(hsl[1]), dualDecimals(hsl[2])]; - ok( hsl, 'Basic return evaluation' ); - deepEqual( dualDecimals(hsl[0]) , 0.33, 'rgb(144, 238, 144): H 0.33' ); - deepEqual( dualDecimals(hsl[1]) , 0.73, 'rgb(144, 238, 144): S 0.73' ); - deepEqual( dualDecimals(hsl[2]) , 0.75, 'rgb(144, 238, 144): L 0.75' ); - + deepEqual( ret, [0.33, 0.73, 0.75], 'rgb(144, 238, 144): hsl(0.33, 0.73, 0.75)' ); }); -test( 'hslToRgb', function(){ - expect(4); +test( 'hslToRgb', function() { + expect(1); - var rgb = jQuery.colorUtil.hslToRgb( 0.3, 0.7, 0.8 ); + var rgb = $.colorUtil.hslToRgb( 0.3, 0.7, 0.8 ); - ok( rgb, 'Basic return evaluation' ); - deepEqual( Math.round(rgb[0]) , 183, 'hsl(0.3, 0.7, 0.8): R 183' ); - deepEqual( Math.round(rgb[1]) , 240, 'hsl(0.3, 0.7, 0.8): G 240' ); - deepEqual( Math.round(rgb[2]) , 168, 'hsl(0.3, 0.7, 0.8): B 168' ); + // Cross-browser differences in decimals... + // Re-create the hslToRgb return array items, rounded to whole numbers. + var ret = [Math.round(rgb[0]), Math.round(rgb[1]), Math.round(rgb[2])]; + deepEqual( ret ,[183, 240, 168], 'hsl(0.3, 0.7, 0.8): rgb(183, 240, 168)' ); }); -test( 'getColorBrightness', function(){ +test( 'getColorBrightness', function() { expect(2); - var a = jQuery.colorUtil.getColorBrightness( 'red', +0.1 ); - - equal( a, 'rgb(255,50,50)', 'Start with named color, brighten 10%' ); - - var b = jQuery.colorUtil.getColorBrightness( 'rgb(200,50,50)', -0.2 ); - - equal( b, 'rgb(118,29,29)', 'Start with rgb string, darken 10%' ); + var a = $.colorUtil.getColorBrightness( 'red', +0.1 ); + equal( a, 'rgb(255,50,50)', 'Start with named color "red", brighten 10%' ); + var b = $.colorUtil.getColorBrightness( 'rgb(200,50,50)', -0.2 ); + equal( b, 'rgb(118,29,29)', 'Start with rgb string "rgb(200,50,50)", darken 20%' ); }); diff --git a/tests/qunit/suites/resources/jquery/jquery.mwPrototypes.js b/tests/qunit/suites/resources/jquery/jquery.mwPrototypes.js index 6fd681fc4c..bb6d2a1b17 100644 --- a/tests/qunit/suites/resources/jquery/jquery.mwPrototypes.js +++ b/tests/qunit/suites/resources/jquery/jquery.mwPrototypes.js @@ -1,58 +1,56 @@ module( 'jquery.mwPrototypes.js' ); -test( 'String functions', function(){ +test( 'String functions', function() { - equal( $j.trimLeft( ' foo bar ' ), 'foo bar ', 'trimLeft' ); - equal( $j.trimRight( ' foo bar ' ), ' foo bar', 'trimRight' ); - equal( $j.ucFirst( 'foo'), 'Foo', 'ucFirst' ); + equal( $.trimLeft( ' foo bar ' ), 'foo bar ', 'trimLeft' ); + equal( $.trimRight( ' foo bar ' ), ' foo bar', 'trimRight' ); + equal( $.ucFirst( 'foo'), 'Foo', 'ucFirst' ); - equal( $j.escapeRE( '