From 080a3636373039d4b7345d26f82744a1668121d7 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 13 Jun 2012 03:58:42 +0200 Subject: [PATCH] qunit.completenessTest: Update 0.3 -> 0.4 * Make standalone from jQuery so that it can be used to test code coverage of jQuery core as well (they've shown interest in using this module). * Various code quality updates and bug fixes. Change-Id: Ifd0169ab6b34039046df4418a9c4b85fe00e7159 --- .../jquery/jquery.qunit.completenessTest.js | 315 ++++++++++++------ 1 file changed, 208 insertions(+), 107 deletions(-) diff --git a/resources/jquery/jquery.qunit.completenessTest.js b/resources/jquery/jquery.qunit.completenessTest.js index 5abb8691ff..2db062981a 100644 --- a/resources/jquery/jquery.qunit.completenessTest.js +++ b/resources/jquery/jquery.qunit.completenessTest.js @@ -1,19 +1,85 @@ /** - * jQuery QUnit CompletenessTest 0.3 + * jQuery QUnit CompletenessTest 0.4 * * Tests the completeness of test suites for object oriented javascript * libraries. Written to be used in environments with jQuery and QUnit. - * Requires jQuery 1.5.2 or higher. - * - * Globals: jQuery, QUnit, console.log + * Requires jQuery 1.7.2 or higher. * * Built for and tested with: - * - Safari 5 + * - Chrome 19 * - Firefox 4 + * - Safari 5 * - * @author Timo Tijhof, 2011 + * @author Timo Tijhof, 2011-2012 */ -( function( $ ) { +/*global jQuery, QUnit */ +/*jshint eqeqeq:false, eqnull:false, forin:false */ +( function ( $ ) { + "use strict"; + + var util, + hasOwn = Object.prototype.hasOwnProperty, + log = (window.console && window.console.log) + ? function () { return window.console.log.apply(window.console, arguments); } + : function () {}; + + // Simplified version of a few jQuery methods, except that they don't + // call other jQuery methods. Required to be able to run the CompletenessTest + // on jQuery itself as well. + util = { + keys: Object.keys || function ( object ) { + var key, keys = []; + for ( key in object ) { + if ( hasOwn.call( object, key ) ) { + keys.push( key ); + } + } + return keys; + }, + extend: function () { + var options, name, src, copy, + target = arguments[0] || {}, + i = 1, + length = arguments.length; + + for ( ; i < length; i++ ) { + // Only deal with non-null/undefined values + if ( (options = arguments[ i ]) != null ) { + // Extend the base object + for ( name in options ) { + src = target[ name ]; + copy = options[ name ]; + + // Prevent never-ending loop + if ( target === copy ) { + continue; + } + + if ( copy !== undefined ) { + target[ name ] = copy; + } + } + } + } + + // Return the modified object + return target; + }, + each: function ( object, callback ) { + var name; + for ( name in object ) { + if ( callback.call( object[ name ], name, object[ name ] ) === false ) { + break; + } + } + }, + // $.type and $.isEmptyObject are safe as is, they don't call + // other $.* methods. Still need to be derefenced into `util` + // since the CompletenessTest will overload them with spies. + type: $.type, + isEmptyObject: $.isEmptyObject + }; + /** * CompletenessTest @@ -33,67 +99,103 @@ var CompletenessTest = function ( masterVariable, ignoreFn ) { // Keep track in these objects. Keyed by strings with the // method names (ie. 'my.foo', 'my.bar', etc.) values are boolean true. + this.injectionTracker = {}; this.methodCallTracker = {}; this.missingTests = {}; - this.ignoreFn = undefined === ignoreFn ? function(){ return false; } : ignoreFn; + this.ignoreFn = undefined === ignoreFn ? function () { return false; } : ignoreFn; // Lazy limit in case something weird happends (like recurse (part of) ourself). - this.lazyLimit = 1000; + this.lazyLimit = 2000; this.lazyCounter = 0; var that = this; // Bind begin and end to QUnit. - QUnit.begin = function(){ - that.checkTests( null, masterVariable, masterVariable, [], CompletenessTest.ACTION_INJECT ); - }; + QUnit.begin( function () { + that.walkTheObject( null, masterVariable, masterVariable, [], CompletenessTest.ACTION_INJECT ); + log( 'CompletenessTest/walkTheObject/ACTION_INJECT', that ); + }); + + QUnit.done( function () { + that.populateMissingTests(); + log( 'CompletenessTest/populateMissingTests', that ); + + var toolbar, testResults, cntTotal, cntCalled, cntMissing; + + cntTotal = util.keys( that.injectionTracker ).length; + cntCalled = util.keys( that.methodCallTracker ).length; + cntMissing = util.keys( that.missingTests ).length; - QUnit.done = function(){ - that.checkTests( null, masterVariable, masterVariable, [], CompletenessTest.ACTION_CHECK ); - console.log( 'CompletenessTest.ACTION_CHECK', that ); + function makeTestResults( blob, title, style ) { + var elOutputWrapper, elTitle, elContainer, elList, elFoot; - // Build HTML representing the outcome from CompletenessTest - // And insert it into the header. + elTitle = document.createElement( 'strong' ); + elTitle.textContent = title || 'Values'; - var makeList = function( blob, title, style ) { - title = title || 'Values'; - var html = '' + mw.html.escape(title) + ''; - $.each( blob, function( key ) { - html += '
' + mw.html.escape(key); + elList = document.createElement( 'ul' ); + util.each( blob, function ( key ) { + var elItem = document.createElement( 'li' ); + elItem.textContent = key; + elList.appendChild( elItem ); }); - html += '

— CompletenessTest'; - var $oldResult = $( '#qunit-completenesstest' ), - $result = $oldResult.length ? $oldResult : $( '
' ); - return $result.css( style ).html( html ); - }; - if ( $.isEmptyObject( that.missingTests ) ) { + elFoot = document.createElement( 'p' ); + elFoot.innerHTML = '— CompletenessTest'; + + elContainer = document.createElement( 'div' ); + elContainer.appendChild( elTitle ); + elContainer.appendChild( elList ); + elContainer.appendChild( elFoot ); + + elOutputWrapper = document.getElementById( 'qunit-completenesstest' ); + if ( !elOutputWrapper ) { + elOutputWrapper = document.createElement( 'div' ); + elOutputWrapper.id = 'qunit-completenesstest'; + } + elOutputWrapper.appendChild( elContainer ); + + util.each( style, function ( key, value ) { + elOutputWrapper.style[key] = value; + }); + return elOutputWrapper; + } + + if ( cntMissing === 0 ) { // Good - var $testResults = makeList( - { 'No missing tests!': true }, - 'missingTests', + testResults = makeTestResults( + {}, + 'Detected calls to ' + cntCalled + '/' + cntTotal + ' methods. No missing tests!', { - background: '#D2E0E6', + backgroundColor: '#D2E0E6', color: '#366097', - padding: '1em' + paddingTop: '1em', + paddingRight: '1em', + paddingBottom: '1em', + paddingLeft: '1em' } ); } else { // Bad - var $testResults = makeList( + testResults = makeTestResults( that.missingTests, - 'missingTests', + 'Detected calls to ' + cntCalled + '/' + cntTotal + ' methods. ' + cntMissing + ' methods not covered:', { - background: '#EE5757', + backgroundColor: '#EE5757', color: 'black', - padding: '1em' + paddingTop: '1em', + paddingRight: '1em', + paddingBottom: '1em', + paddingLeft: '1em' } ); } - $( '#qunit-testrunner-toolbar' ).prepend( $testResults ); - }; + toolbar = document.getElementById( 'qunit-testrunner-toolbar' ); + if ( toolbar ) { + toolbar.insertBefore( testResults, toolbar.firstChild ); + } + }); return this; }; @@ -106,7 +208,7 @@ CompletenessTest.ACTION_CHECK = 501; CompletenessTest.fn = CompletenessTest.prototype = { /** - * CompletenessTest.fn.checkTests + * CompletenessTest.fn.walkTheObject * * This function recursively walks through the given object, calling itself as it goes. * Depending on the action it either injects our listener into the methods, or @@ -121,92 +223,86 @@ CompletenessTest.fn = CompletenessTest.prototype = { * masterVariable. Not including currName. * @param action {Number} What is this function supposed to do (ACTION_INJECT or ACTION_CHECK) */ - checkTests: function( currName, currVar, masterVariable, parentPathArray, action ) { - - // Handle the lazy limit - this.lazyCounter++; - if ( this.lazyCounter > this.lazyLimit ) { - console.log( 'CompletenessTest.fn.checkTests> Limit reached: ' + this.lazyCounter ); - return null; - } + walkTheObject: function ( currName, currVar, masterVariable, parentPathArray, action ) { - var type = $.type( currVar ), + var key, value, tmpPathArray, + type = util.type( currVar ), that = this; // Hard ignores if ( this.ignoreFn( currVar, that, parentPathArray ) ) { return null; + } - // Functions - } else if ( type === 'function' ) { - - /* CHECK MODE */ - - if ( action === CompletenessTest.ACTION_CHECK ) { - - if ( !currVar.prototype || $.isEmptyObject( currVar.prototype ) ) { - - that.hasTest( parentPathArray.join( '.' ) ); + // Handle the lazy limit + this.lazyCounter++; + if ( this.lazyCounter > this.lazyLimit ) { + log( 'CompletenessTest.fn.walkTheObject> Limit reached: ' + this.lazyCounter, parentPathArray ); + return null; + } - // We don't support checking object constructors yet... - } else { + // Functions + if ( type === 'function' ) { - // ...the prototypes are fine tho - $.each( currVar.prototype, function( key, value ) { - if ( key === 'constructor' ) return; + if ( !currVar.prototype || util.isEmptyObject( currVar.prototype ) ) { - // Clone and break reference to parentPathArray - var tmpPathArray = $.extend( [], parentPathArray ); - tmpPathArray.push( 'prototype' ); tmpPathArray.push( key ); + if ( action === CompletenessTest.ACTION_INJECT ) { - that.hasTest( tmpPathArray.join( '.' ) ); + that.injectionTracker[ parentPathArray.join( '.' ) ] = true; + that.injectCheck( masterVariable, parentPathArray, function () { + that.methodCallTracker[ parentPathArray.join( '.' ) ] = true; } ); } - /* INJECT MODE */ - - } else if ( action === CompletenessTest.ACTION_INJECT ) { + // We don't support checking object constructors yet... + // ...we can check the prototypes fine, though. + } else { + if ( action === CompletenessTest.ACTION_INJECT ) { - if ( !currVar.prototype || $.isEmptyObject( currVar.prototype ) ) { + for ( key in currVar.prototype ) { + if ( hasOwn.call( currVar.prototype, key ) ) { + value = currVar.prototype[key]; + if ( key === 'constructor' ) { + continue; + } - // Inject check - that.injectCheck( masterVariable, parentPathArray, function() { - that.methodCallTracker[ parentPathArray.join( '.' ) ] = true; - } ); + // Clone and break reference to parentPathArray + tmpPathArray = util.extend( [], parentPathArray ); + tmpPathArray.push( 'prototype' ); + tmpPathArray.push( key ); - // We don't support checking object constructors yet... - } else { + that.walkTheObject( key, value, masterVariable, tmpPathArray, action ); + } + } - // ... the prototypes are fine tho - $.each( currVar.prototype, function( key, value ) { - if ( key === 'constructor' ) return; - - // Clone and break reference to parentPathArray - var tmpPathArray = $.extend( [], parentPathArray ); - tmpPathArray.push( 'prototype' ); tmpPathArray.push( key ); - - that.checkTests( key, value, masterVariable, tmpPathArray, action ); - } ); } - } - // Recursively. After all, this *is* the completeness test - } else if ( type === 'object' ) { - - $.each( currVar, function( key, value ) { - - // Clone and break reference to parentPathArray - var tmpPathArray = $.extend( [], parentPathArray ); - tmpPathArray.push( key ); + } - that.checkTests( key, value, masterVariable, tmpPathArray, action ); + // Recursively. After all, this is the *completeness* test + if ( type === 'function' || type === 'object' ) { + for ( key in currVar ) { + if ( hasOwn.call( currVar, key ) ) { + value = currVar[key]; - } ); + // Clone and break reference to parentPathArray + tmpPathArray = util.extend( [], parentPathArray ); + tmpPathArray.push( key ); + that.walkTheObject( key, value, masterVariable, tmpPathArray, action ); + } + } } }, + populateMissingTests: function () { + var ct = this; + util.each( ct.injectionTracker, function ( key ) { + ct.hasTest( key ); + }); + }, + /** * CompletenessTest.fn.hasTest * @@ -217,7 +313,7 @@ CompletenessTest.fn = CompletenessTest.prototype = { * @param fnName {String} * @return {Boolean} */ - hasTest: function( fnName ) { + hasTest: function ( fnName ) { if ( !( fnName in this.methodCallTracker ) ) { this.missingTests[fnName] = true; return false; @@ -235,19 +331,24 @@ CompletenessTest.fn = CompletenessTest.prototype = { * @param objectPathArray {Array} * @param injectFn {Function} */ - injectCheck: function( masterVariable, objectPathArray, injectFn ) { - var prev, - curr = masterVariable, - lastMember; + injectCheck: function ( masterVariable, objectPathArray, injectFn ) { + var i, len, prev, memberName, lastMember, + curr = masterVariable; + + // Get the object in question through the path from the master variable, + // We can't pass the value directly because we need to re-define the object + // member and keep references to the parent object, member name and member + // value at all times. + for ( i = 0, len = objectPathArray.length; i < len; i++ ) { + memberName = objectPathArray[i]; - $.each( objectPathArray, function( i, memberName ) { prev = curr; curr = prev[memberName]; lastMember = memberName; - }); + } // Objects are by reference, members (unless objects) are not. - prev[lastMember] = function() { + prev[lastMember] = function () { injectFn(); return curr.apply( this, arguments ); }; -- 2.20.1