r89865 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89864‎ | r89865 | r89866 >
Date:00:24, 11 June 2011
Author:krinkle
Status:ok
Tags:
Comment:
Follow-up r89853: Documentation for completenesstest + minor fixes/whitespace
Modified paths:
  • /trunk/phase3/resources/jquery/jquery.qunit.completenessTest.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/jquery/jquery.qunit.completenessTest.js
@@ -46,32 +46,55 @@
4747 this.lazyLimit = 1000;
4848 this.lazyCounter = 0;
4949
 50+ var that = this;
 51+
5052 // Bind begin and end to QUnit.
51 - var that = this;
5253 QUnit.begin = function(){
5354 that.checkTests( null, masterVariable, masterVariable, [], CompletenessTest.ACTION_INJECT );
5455 };
 56+
5557 QUnit.done = function(){
5658 that.checkTests( null, masterVariable, masterVariable, [], CompletenessTest.ACTION_CHECK );
5759 console.log( 'CompletenessTest.ACTION_CHECK', that );
5860
59 - // Insert HTML into header
 61+ // Build HTML representing the outcome from CompletenessTest
 62+ // And insert it into the header.
6063
6164 var makeList = function( blob, title, style ) {
6265 title = title || 'Values';
63 - var html = '<div style="'+style+'">'
64 - + '<strong>' + mw.html.escape(title) + '</strong>';
 66+ var html = '<strong>' + mw.html.escape(title) + '</strong>';
6567 $.each( blob, function( key ) {
6668 html += '<br />' + mw.html.escape(key);
6769 });
68 - return html + '<br /><br /><em>&mdash; CompletenessTest</em></div>';
 70+ html += '<br /><br /><em>&mdash; CompletenessTest</em>';
 71+ return $( '<div>' ).css( style ).append( html );
6972 };
70 - if ( $.isEmptyObject( that.missingTests ) ) {
71 - var testResults = makeList( { 'No missing tests!': true }, 'missingTests', 'background: #D2E0E6; color: #366097; padding:1em' );
 73+
 74+ if ( $.isEmptyObject( that.missingTests ) ) {
 75+ // Good
 76+ var $testResults = makeList(
 77+ { 'No missing tests!': true },
 78+ 'missingTests',
 79+ {
 80+ background: '#D2E0E6',
 81+ color: '#366097',
 82+ padding: '1em'
 83+ }
 84+ );
7285 } else {
73 - var testResults = makeList( that.missingTests, 'missingTests', 'background: #EE5757; color: black; padding: 1em' );
 86+ // Bad
 87+ var $testResults = makeList(
 88+ that.missingTests,
 89+ 'missingTests',
 90+ {
 91+ background: '#EE5757',
 92+ color: 'black',
 93+ padding: '1em'
 94+ }
 95+ );
7496 }
75 - $( '#qunit-testrunner-toolbar' ).prepend( testResults );
 97+
 98+ $( '#qunit-testrunner-toolbar' ).prepend( $testResults );
7699 };
77100
78101 return this;
@@ -87,11 +110,18 @@
88111 /**
89112 * CompletenessTest.fn.checkTests
90113 *
91 - * @param currName {String}
92 - * @param currVar {mixed}
93 - * @param masterVariable {Object}
94 - * @param parentPathArray {Array}
95 - * @param action {Number} What action is checkTests commanded to do ?
 114+ * This function recursively walks through the given object, calling itself as it goes.
 115+ * Depending on the action it either injects our listener into the methods, or
 116+ * reads from our tracker and records which methods have not been called by the test suite.
 117+ *
 118+ * @param currName {String|Null} Name of the given object member (Initially this is null).
 119+ * @param currVar {mixed} The variable to check (initially an object,
 120+ * further down it could be anything).
 121+ * @param masterVariable {Object} Throughout our interation, always keep track of the master/root.
 122+ * Initially this is the same as currVar.
 123+ * @param parentPathArray {Array} Array of names that indicate our breadcrumb path starting at
 124+ * masterVariable. Not including currName.
 125+ * @param action {Number} What is this function supposed to do (ACTION_INJECT or ACTION_CHECK)
96126 */
97127 checkTests: function( currName, currVar, masterVariable, parentPathArray, action ) {
98128
@@ -107,8 +137,8 @@
108138
109139 // Hard ignores
110140 if ( this.ignoreFn( currVar, that, parentPathArray ) ) {
 141+ return null;
111142
112 -
113143 // Functions
114144 } else if ( type === 'function' ) {
115145
@@ -127,14 +157,14 @@
128158 $.each( currVar.prototype, function( key, value ) {
129159
130160 // Clone and brake reference to parentPathArray
131 - var tmpPathArray = $.extend([], parentPathArray);
132 - tmpPathArray.push('prototype'); tmpPathArray.push(key);
 161+ var tmpPathArray = $.extend( [], parentPathArray );
 162+ tmpPathArray.push( 'prototype' ); tmpPathArray.push( key );
133163
134164 that.hasTest( tmpPathArray.join( '.' ) );
135165 } );
136166 }
137167
138 - /* INJET MODE */
 168+ /* INJECT MODE */
139169
140170 } else if ( action === CompletenessTest.ACTION_INJECT ) {
141171
@@ -154,14 +184,14 @@
155185 $.each( currVar.prototype, function( key, value ) {
156186
157187 // Clone and brake reference to parentPathArray
158 - var tmpPathArray = $.extend([], parentPathArray);
159 - tmpPathArray.push('prototype'); tmpPathArray.push(key);
 188+ var tmpPathArray = $.extend( [], parentPathArray );
 189+ tmpPathArray.push( 'prototype' ); tmpPathArray.push( key );
160190
161191 that.checkTests( key, value, masterVariable, tmpPathArray, action );
162192 } );
163193 }
164194
165 - } //else { }
 195+ }
166196
167197 // Recursively. After all, this *is* the completness test
168198 } else if ( type === 'object' ) {
@@ -169,14 +199,14 @@
170200 $.each( currVar, function( key, value ) {
171201
172202 // Clone and brake reference to parentPathArray
173 - var tmpPathArray = $.extend([], parentPathArray);
174 - tmpPathArray.push(key);
 203+ var tmpPathArray = $.extend( [], parentPathArray );
 204+ tmpPathArray.push( key );
175205
176206 that.checkTests( key, value, masterVariable, tmpPathArray, action );
177207
178208 } );
179209
180 - } // else { }
 210+ }
181211
182212 return 'End of checkTests';
183213 },
@@ -184,27 +214,37 @@
185215 /**
186216 * CompletenessTest.fn.hasTest
187217 *
 218+ * Checks if the given method name (ie. 'my.foo.bar')
 219+ * was called during the test suite (as far as the tracker knows).
 220+ * If so it adds it to missingTests.
 221+ *
188222 * @param fnName {String}
 223+ * @return {Boolean}
189224 */
190225 hasTest: function( fnName ) {
191226 if ( !(fnName in this.methodCallTracker) ) {
192227 this.missingTests[fnName] = true;
 228+ return false;
193229 }
 230+ return true;
194231 },
195232
196233 /**
197234 * CompletenessTest.fn.injectCheck
198235 *
 236+ * Injects a function (such as a spy that updates methodCallTracker when
 237+ * it's called) inside another function.
 238+ *
199239 * @param masterVariable {Object}
200240 * @param objectPathArray {Array}
201241 * @param injectFn {Function}
202242 */
203 - injectCheck: function( masterVariable, objectPathArray, injectFn, functionType ) {
 243+ injectCheck: function( masterVariable, objectPathArray, injectFn ) {
204244 var prev,
205245 curr = masterVariable,
206246 lastMember;
207247
208 - $.each(objectPathArray, function(i, memberName){
 248+ $.each( objectPathArray, function(i, memberName){
209249 prev = curr;
210250 curr = prev[memberName];
211251 lastMember = memberName;
@@ -213,7 +253,7 @@
214254 // Objects are by reference, members (unless objects) are not.
215255 prev[lastMember] = function(){
216256 injectFn();
217 - return curr.apply(this, arguments );
 257+ return curr.apply( this, arguments );
218258 };
219259 }
220260 };

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r89853Added jquery.qunit.completenessTest.js (A jQuery/QUnit test coverage utility)...krinkle22:15, 10 June 2011

Status & tagging log