2 * jQuery QUnit CompletenessTest 0.3
4 * Tests the completeness of test suites for object oriented javascript
5 * libraries. Written to be used in environments with jQuery and QUnit.
6 * Requires jQuery 1.5.2 or higher.
8 * Globals: jQuery, QUnit, console.log
10 * Built for and tested with:
14 * @author Timo Tijhof, 2011
23 * var myTester = new CompletenessTest( myLib );
24 * @param masterVariable {Object} The root variable that contains all object
25 * members. CompletenessTest will recursively traverse objects and keep track
27 * @param ignoreFn {Function} Optionally pass a function to filter out certain
28 * methods. Example: You may want to filter out instances of jQuery or some
29 * other constructor. Otherwise "missingTests" will include all methods that
30 * were not called from that instance.
32 var CompletenessTest = function ( masterVariable, ignoreFn ) {
34 // Keep track in these objects. Keyed by strings with the
35 // method names (ie. 'my.foo', 'my.bar', etc.) values are boolean true.
36 this.methodCallTracker = {};
37 this.missingTests = {};
39 this.ignoreFn = undefined === ignoreFn ? function(){ return false; } : ignoreFn;
41 // Lazy limit in case something weird happends (like recurse (part of) ourself).
42 this.lazyLimit = 1000;
47 // Bind begin and end to QUnit.
48 QUnit.begin = function(){
49 that.checkTests( null, masterVariable, masterVariable, [], CompletenessTest.ACTION_INJECT );
52 QUnit.done = function(){
53 that.checkTests( null, masterVariable, masterVariable, [], CompletenessTest.ACTION_CHECK );
54 console.log( 'CompletenessTest.ACTION_CHECK', that );
56 // Build HTML representing the outcome from CompletenessTest
57 // And insert it into the header.
59 var makeList = function( blob, title, style ) {
60 title = title || 'Values';
61 var html = '<strong>' + mw.html.escape(title) + '</strong>';
62 $.each( blob, function( key ) {
63 html += '<br />' + mw.html.escape(key);
65 html += '<br /><br /><em>— CompletenessTest</em>';
66 var $oldResult = $( '#qunit-completenesstest' ),
67 $result = $oldResult.length ? $oldResult : $( '<div id="qunit-completenesstest"></div>' );
68 return $result.css( style ).html( html );
71 if ( $.isEmptyObject( that.missingTests ) ) {
73 var $testResults = makeList(
74 { 'No missing tests!': true },
77 background: '#D2E0E6',
84 var $testResults = makeList(
88 background: '#EE5757',
95 $( '#qunit-testrunner-toolbar' ).prepend( $testResults );
102 CompletenessTest.ACTION_INJECT = 500;
103 CompletenessTest.ACTION_CHECK = 501;
106 CompletenessTest.fn = CompletenessTest.prototype = {
109 * CompletenessTest.fn.checkTests
111 * This function recursively walks through the given object, calling itself as it goes.
112 * Depending on the action it either injects our listener into the methods, or
113 * reads from our tracker and records which methods have not been called by the test suite.
115 * @param currName {String|Null} Name of the given object member (Initially this is null).
116 * @param currVar {mixed} The variable to check (initially an object,
117 * further down it could be anything).
118 * @param masterVariable {Object} Throughout our interation, always keep track of the master/root.
119 * Initially this is the same as currVar.
120 * @param parentPathArray {Array} Array of names that indicate our breadcrumb path starting at
121 * masterVariable. Not including currName.
122 * @param action {Number} What is this function supposed to do (ACTION_INJECT or ACTION_CHECK)
124 checkTests: function( currName, currVar, masterVariable, parentPathArray, action ) {
126 // Handle the lazy limit
128 if ( this.lazyCounter > this.lazyLimit ) {
129 console.log( 'CompletenessTest.fn.checkTests> Limit reached: ' + this.lazyCounter );
133 var type = $.type( currVar ),
137 if ( this.ignoreFn( currVar, that, parentPathArray ) ) {
141 } else if ( type === 'function' ) {
145 if ( action === CompletenessTest.ACTION_CHECK ) {
147 if ( !currVar.prototype || $.isEmptyObject( currVar.prototype ) ) {
149 that.hasTest( parentPathArray.join( '.' ) );
151 // We don't support checking object constructors yet...
154 // ...the prototypes are fine tho
155 $.each( currVar.prototype, function( key, value ) {
156 if ( key === 'constructor' ) return;
158 // Clone and break reference to parentPathArray
159 var tmpPathArray = $.extend( [], parentPathArray );
160 tmpPathArray.push( 'prototype' ); tmpPathArray.push( key );
162 that.hasTest( tmpPathArray.join( '.' ) );
168 } else if ( action === CompletenessTest.ACTION_INJECT ) {
170 if ( !currVar.prototype || $.isEmptyObject( currVar.prototype ) ) {
173 that.injectCheck( masterVariable, parentPathArray, function() {
174 that.methodCallTracker[ parentPathArray.join( '.' ) ] = true;
177 // We don't support checking object constructors yet...
180 // ... the prototypes are fine tho
181 $.each( currVar.prototype, function( key, value ) {
182 if ( key === 'constructor' ) return;
184 // Clone and break reference to parentPathArray
185 var tmpPathArray = $.extend( [], parentPathArray );
186 tmpPathArray.push( 'prototype' ); tmpPathArray.push( key );
188 that.checkTests( key, value, masterVariable, tmpPathArray, action );
194 // Recursively. After all, this *is* the completeness test
195 } else if ( type === 'object' ) {
197 $.each( currVar, function( key, value ) {
199 // Clone and break reference to parentPathArray
200 var tmpPathArray = $.extend( [], parentPathArray );
201 tmpPathArray.push( key );
203 that.checkTests( key, value, masterVariable, tmpPathArray, action );
211 * CompletenessTest.fn.hasTest
213 * Checks if the given method name (ie. 'my.foo.bar')
214 * was called during the test suite (as far as the tracker knows).
215 * If not it adds it to missingTests.
217 * @param fnName {String}
220 hasTest: function( fnName ) {
221 if ( !( fnName in this.methodCallTracker ) ) {
222 this.missingTests[fnName] = true;
229 * CompletenessTest.fn.injectCheck
231 * Injects a function (such as a spy that updates methodCallTracker when
232 * it's called) inside another function.
234 * @param masterVariable {Object}
235 * @param objectPathArray {Array}
236 * @param injectFn {Function}
238 injectCheck: function( masterVariable, objectPathArray, injectFn ) {
240 curr = masterVariable,
243 $.each( objectPathArray, function( i, memberName ) {
245 curr = prev[memberName];
246 lastMember = memberName;
249 // Objects are by reference, members (unless objects) are not.
250 prev[lastMember] = function() {
252 return curr.apply( this, arguments );
257 window.CompletenessTest = CompletenessTest;