Added jquery.qunit.completenessTest.js (A jQuery/QUnit test coverage utility)
* Added to /resources * Conditionally loaded (condition being that the url parameter "completenesstest" has a truthy value) * Fixed a test that was using === and true * Setting an added method somewhere back to undefined so it won't be listed as a potential "missing test".
This commit is contained in:
parent
366e7d9623
commit
540419a82e
5 changed files with 3714 additions and 1707 deletions
5165
resources/jquery/jquery.js
vendored
5165
resources/jquery/jquery.js
vendored
File diff suppressed because it is too large
Load diff
223
resources/jquery/jquery.qunit.completenessTest.js
Normal file
223
resources/jquery/jquery.qunit.completenessTest.js
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
/**
|
||||
* jQuery QUnit CompletenessTest 0.3
|
||||
*
|
||||
* Tests the completeness of test suites for object oriented javascript
|
||||
* libraries. Written to be used in enviroments with jQuery and QUnit.
|
||||
* Requires jQuery 1.5.2 or higher.
|
||||
*
|
||||
* Globals: jQuery, $, QUnit, console.log
|
||||
*
|
||||
* Built for and tested with:
|
||||
* - Safari 5
|
||||
* - Firefox 4
|
||||
*
|
||||
* @author Timo Tijhof, 2011
|
||||
*/
|
||||
(function(){
|
||||
|
||||
/* Private members */
|
||||
var TYPE_SIMPLEFUNC = 101;
|
||||
var TYPE_OBJCONSTRFUNC = 100;
|
||||
|
||||
/**
|
||||
* CompletenessTest
|
||||
* @constructor
|
||||
*
|
||||
* @example
|
||||
* var myTester = new CompletenessTest( myLib );
|
||||
* @param masterVariable {Object} The root variable that contains all object
|
||||
* members. CompletenessTest will recursively traverse objects and keep track
|
||||
* of all methods.
|
||||
* @param ignoreFn {Function} Optionally pass a function to filter out certain
|
||||
* methods. Example: You may want to filter out instances of jQuery or some
|
||||
* other constructor. Otherwise "missingTests" will include all methods that
|
||||
* were not called from that instance.
|
||||
*/
|
||||
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.methodCallTracker = {};
|
||||
this.missingTests = {};
|
||||
|
||||
this.ignoreFn = undefined === ignoreFn ? function(){ return false; } : ignoreFn;
|
||||
|
||||
// Lazy limit in case something weird happends (like recurse (part of) ourself).
|
||||
this.lazyLimit = 1000;
|
||||
this.lazyCounter = 0;
|
||||
|
||||
// Bind begin and end to QUnit.
|
||||
var that = this;
|
||||
QUnit.begin = function(){
|
||||
that.checkTests( null, masterVariable, masterVariable, [], CompletenessTest.ACTION_INJECT );
|
||||
};
|
||||
QUnit.done = function(){
|
||||
that.checkTests( null, masterVariable, masterVariable, [], CompletenessTest.ACTION_CHECK );
|
||||
console.log( 'CompletenessTest.ACTION_CHECK', that );
|
||||
|
||||
// Insert HTML into header
|
||||
|
||||
var makeList = function( blob, title, style ) {
|
||||
title = title || 'Values';
|
||||
var html = '<div style="'+style+'">'
|
||||
+ '<strong>' + mw.html.escape(title) + '</strong>';
|
||||
$.each( blob, function( key ) {
|
||||
html += '<br />' + mw.html.escape(key);
|
||||
});
|
||||
return html + '<br /><br /><em>— CompletenessTest</em></div>';
|
||||
};
|
||||
if ( $.isEmptyObject( that.missingTests ) ) {
|
||||
var testResults = makeList( { 'No missing tests!': true }, 'missingTests', 'background: #D2E0E6; color: #366097; padding:1em' );
|
||||
} else {
|
||||
var testResults = makeList( that.missingTests, 'missingTests', 'background: #EE5757; color: black; padding: 1em' );
|
||||
}
|
||||
$( '#qunit-testrunner-toolbar' ).prepend( testResults );
|
||||
};
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/* Static members */
|
||||
CompletenessTest.ACTION_INJECT = 500;
|
||||
CompletenessTest.ACTION_CHECK = 501;
|
||||
|
||||
/* Public methods */
|
||||
CompletenessTest.fn = CompletenessTest.prototype = {
|
||||
|
||||
/**
|
||||
* CompletenessTest.fn.checkTests
|
||||
*
|
||||
* @param currName {String}
|
||||
* @param currVar {mixed}
|
||||
* @param masterVariable {Object}
|
||||
* @param parentPathArray {Array}
|
||||
* @param action {Number} What action is checkTests commanded to do ?
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
var type = $.type( currVar ),
|
||||
that = this;
|
||||
|
||||
// Hard ignores
|
||||
if ( this.ignoreFn( currVar, that, parentPathArray ) ) {
|
||||
|
||||
|
||||
// Functions
|
||||
} else if ( type === 'function' ) {
|
||||
|
||||
/* CHECK MODE */
|
||||
|
||||
if ( action === CompletenessTest.ACTION_CHECK ) {
|
||||
|
||||
if ( !currVar.prototype || $.isEmptyObject( currVar.prototype ) ) {
|
||||
|
||||
that.hasTest( parentPathArray.join( '.' ) );
|
||||
|
||||
// We don't support checking object constructors yet...
|
||||
} else {
|
||||
|
||||
// ...the prototypes are fine tho
|
||||
$.each( currVar.prototype, function( key, value ) {
|
||||
|
||||
// Clone and brake reference to parentPathArray
|
||||
var tmpPathArray = $.extend([], parentPathArray);
|
||||
tmpPathArray.push('prototype'); tmpPathArray.push(key);
|
||||
|
||||
that.hasTest( tmpPathArray.join( '.' ) );
|
||||
} );
|
||||
}
|
||||
|
||||
/* INJET MODE */
|
||||
|
||||
} else if ( action === CompletenessTest.ACTION_INJECT ) {
|
||||
|
||||
if ( !currVar.prototype || $.isEmptyObject( currVar.prototype ) ) {
|
||||
|
||||
// Inject check
|
||||
that.injectCheck( masterVariable, parentPathArray, function(){
|
||||
|
||||
that.methodCallTracker[ parentPathArray.join( '.' ) ] = true;
|
||||
|
||||
}, TYPE_SIMPLEFUNC );
|
||||
|
||||
// We don't support checking object constructors yet...
|
||||
} else {
|
||||
|
||||
// ... the prototypes are fine tho
|
||||
$.each( currVar.prototype, function( key, value ) {
|
||||
|
||||
// Clone and brake reference to parentPathArray
|
||||
var tmpPathArray = $.extend([], parentPathArray);
|
||||
tmpPathArray.push('prototype'); tmpPathArray.push(key);
|
||||
|
||||
that.checkTests( key, value, masterVariable, tmpPathArray, action );
|
||||
} );
|
||||
}
|
||||
|
||||
} //else { }
|
||||
|
||||
// Recursively. After all, this *is* the completness test
|
||||
} else if ( type === 'object' ) {
|
||||
|
||||
$.each( currVar, function( key, value ) {
|
||||
|
||||
// Clone and brake reference to parentPathArray
|
||||
var tmpPathArray = $.extend([], parentPathArray);
|
||||
tmpPathArray.push(key);
|
||||
|
||||
that.checkTests( key, value, masterVariable, tmpPathArray, action );
|
||||
|
||||
} );
|
||||
|
||||
} // else { }
|
||||
|
||||
return 'End of checkTests';
|
||||
},
|
||||
|
||||
/**
|
||||
* CompletenessTest.fn.hasTest
|
||||
*
|
||||
* @param fnName {String}
|
||||
*/
|
||||
hasTest: function( fnName ) {
|
||||
if ( !(fnName in this.methodCallTracker) ) {
|
||||
this.missingTests[fnName] = true;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* CompletenessTest.fn.injectCheck
|
||||
*
|
||||
* @param masterVariable {Object}
|
||||
* @param objectPathArray {Array}
|
||||
* @param injectFn {Function}
|
||||
*/
|
||||
injectCheck: function( masterVariable, objectPathArray, injectFn, functionType ) {
|
||||
var prev,
|
||||
curr = masterVariable,
|
||||
lastMember;
|
||||
|
||||
$.each(objectPathArray, function(i, memberName){
|
||||
prev = curr;
|
||||
curr = prev[memberName];
|
||||
lastMember = memberName;
|
||||
});
|
||||
|
||||
// Objects are by reference, members (unless objects) are not.
|
||||
prev[lastMember] = function(){
|
||||
injectFn();
|
||||
return curr.apply(this, arguments );
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
window.CompletenessTest = CompletenessTest;
|
||||
|
||||
})();
|
||||
|
|
@ -57,6 +57,14 @@
|
|||
the following script will allow it to extract the results.
|
||||
Harmless otherwise. -->
|
||||
<script src="testswarm.inject.js"></script>
|
||||
|
||||
<!-- CompletenessTest -->
|
||||
<script>
|
||||
if ( QUnit.urlParams.completenesstest ) {
|
||||
document.write( '\x3Cscript src="../../resources/jquery/jquery.qunit.completenessTest.js">\x3C/script>' );
|
||||
document.write( '\x3Cscript src="jquery.qunit.completenessTest.config.js">\x3C/script>' );
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1 id="qunit-header">MediaWiki JavaScript Test Suite</h1>
|
||||
|
|
|
|||
20
tests/qunit/jquery.qunit.completenessTest.config.js
Normal file
20
tests/qunit/jquery.qunit.completenessTest.config.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// Return true to ignore
|
||||
var mwTestIgnore = function( val, tester, funcPath ) {
|
||||
|
||||
// Don't record methods of the properties of mw.Map instances
|
||||
// Because we're therefor skipping any injection for
|
||||
// "new mw.Map()", manually set it to true here.
|
||||
if ( val instanceof mw.Map ) {
|
||||
tester.methodCallTracker['Map'] = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Don't record methods of the properties of a jQuery object
|
||||
if ( val instanceof $ ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
var mwTester = new CompletenessTest( mw, mwTestIgnore );
|
||||
|
|
@ -16,7 +16,6 @@ test( '-- Initial check', function() {
|
|||
|
||||
test( 'mw.Map', function() {
|
||||
expect(15);
|
||||
|
||||
ok( mw.Map, 'mw.Map defined' );
|
||||
|
||||
var conf = new mw.Map(),
|
||||
|
|
@ -49,7 +48,7 @@ test( 'mw.Map', function() {
|
|||
|
||||
strictEqual( conf.exists( 'foo' ), true, 'Map.exists returns boolean true if a key exists' );
|
||||
strictEqual( conf.exists( 'notExist' ), false, 'Map.exists returns boolean false if a key does not exists' );
|
||||
strictEqual( conf.get() === conf.values, true, 'Map.get returns the entire values object by reference (if called without arguments)' );
|
||||
strictEqual( conf.get(), conf.values, 'Map.get returns the entire values object by reference (if called without arguments)' );
|
||||
|
||||
conf.set( 'globalMapChecker', 'Hi' );
|
||||
|
||||
|
|
@ -107,7 +106,6 @@ test( 'mw.message & mw.messages', function() {
|
|||
strictEqual( goodbye.exists(), false, 'Message.exists returns false for inexisting messages' );
|
||||
|
||||
equal( goodbye.toString(), '<goodbye>', 'Message.toString returns <key> if key does not exist' );
|
||||
|
||||
});
|
||||
|
||||
test( 'mw.msg', function() {
|
||||
|
|
@ -160,6 +158,7 @@ test( 'mw.loader', function() {
|
|||
// /sample/awesome.js declares the "mw.loader.testCallback" function
|
||||
// which contains a call to start() and ok()
|
||||
mw.loader.testCallback();
|
||||
mw.loader.testCallback = undefined;
|
||||
|
||||
}, function() {
|
||||
start();
|
||||
|
|
|
|||
Loading…
Reference in a new issue