raw
mp-wp_genesis           1 // script.aculo.us unittest.js v1.8.0, Tue Nov 06 15:01:40 +0300 2007
mp-wp_genesis 2
mp-wp_genesis 3 // Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
mp-wp_genesis 4 // (c) 2005-2007 Jon Tirsen (http://www.tirsen.com)
mp-wp_genesis 5 // (c) 2005-2007 Michael Schuerig (http://www.schuerig.de/michael/)
mp-wp_genesis 6 //
mp-wp_genesis 7 // script.aculo.us is freely distributable under the terms of an MIT-style license.
mp-wp_genesis 8 // For details, see the script.aculo.us web site: http://script.aculo.us/
mp-wp_genesis 9
mp-wp_genesis 10 // experimental, Firefox-only
mp-wp_genesis 11 Event.simulateMouse = function(element, eventName) {
mp-wp_genesis 12 var options = Object.extend({
mp-wp_genesis 13 pointerX: 0,
mp-wp_genesis 14 pointerY: 0,
mp-wp_genesis 15 buttons: 0,
mp-wp_genesis 16 ctrlKey: false,
mp-wp_genesis 17 altKey: false,
mp-wp_genesis 18 shiftKey: false,
mp-wp_genesis 19 metaKey: false
mp-wp_genesis 20 }, arguments[2] || {});
mp-wp_genesis 21 var oEvent = document.createEvent("MouseEvents");
mp-wp_genesis 22 oEvent.initMouseEvent(eventName, true, true, document.defaultView,
mp-wp_genesis 23 options.buttons, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
mp-wp_genesis 24 options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, 0, $(element));
mp-wp_genesis 25
mp-wp_genesis 26 if(this.mark) Element.remove(this.mark);
mp-wp_genesis 27 this.mark = document.createElement('div');
mp-wp_genesis 28 this.mark.appendChild(document.createTextNode(" "));
mp-wp_genesis 29 document.body.appendChild(this.mark);
mp-wp_genesis 30 this.mark.style.position = 'absolute';
mp-wp_genesis 31 this.mark.style.top = options.pointerY + "px";
mp-wp_genesis 32 this.mark.style.left = options.pointerX + "px";
mp-wp_genesis 33 this.mark.style.width = "5px";
mp-wp_genesis 34 this.mark.style.height = "5px;";
mp-wp_genesis 35 this.mark.style.borderTop = "1px solid red;"
mp-wp_genesis 36 this.mark.style.borderLeft = "1px solid red;"
mp-wp_genesis 37
mp-wp_genesis 38 if(this.step)
mp-wp_genesis 39 alert('['+new Date().getTime().toString()+'] '+eventName+'/'+Test.Unit.inspect(options));
mp-wp_genesis 40
mp-wp_genesis 41 $(element).dispatchEvent(oEvent);
mp-wp_genesis 42 };
mp-wp_genesis 43
mp-wp_genesis 44 // Note: Due to a fix in Firefox 1.0.5/6 that probably fixed "too much", this doesn't work in 1.0.6 or DP2.
mp-wp_genesis 45 // You need to downgrade to 1.0.4 for now to get this working
mp-wp_genesis 46 // See https://bugzilla.mozilla.org/show_bug.cgi?id=289940 for the fix that fixed too much
mp-wp_genesis 47 Event.simulateKey = function(element, eventName) {
mp-wp_genesis 48 var options = Object.extend({
mp-wp_genesis 49 ctrlKey: false,
mp-wp_genesis 50 altKey: false,
mp-wp_genesis 51 shiftKey: false,
mp-wp_genesis 52 metaKey: false,
mp-wp_genesis 53 keyCode: 0,
mp-wp_genesis 54 charCode: 0
mp-wp_genesis 55 }, arguments[2] || {});
mp-wp_genesis 56
mp-wp_genesis 57 var oEvent = document.createEvent("KeyEvents");
mp-wp_genesis 58 oEvent.initKeyEvent(eventName, true, true, window,
mp-wp_genesis 59 options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
mp-wp_genesis 60 options.keyCode, options.charCode );
mp-wp_genesis 61 $(element).dispatchEvent(oEvent);
mp-wp_genesis 62 };
mp-wp_genesis 63
mp-wp_genesis 64 Event.simulateKeys = function(element, command) {
mp-wp_genesis 65 for(var i=0; i<command.length; i++) {
mp-wp_genesis 66 Event.simulateKey(element,'keypress',{charCode:command.charCodeAt(i)});
mp-wp_genesis 67 }
mp-wp_genesis 68 };
mp-wp_genesis 69
mp-wp_genesis 70 var Test = {}
mp-wp_genesis 71 Test.Unit = {};
mp-wp_genesis 72
mp-wp_genesis 73 // security exception workaround
mp-wp_genesis 74 Test.Unit.inspect = Object.inspect;
mp-wp_genesis 75
mp-wp_genesis 76 Test.Unit.Logger = Class.create();
mp-wp_genesis 77 Test.Unit.Logger.prototype = {
mp-wp_genesis 78 initialize: function(log) {
mp-wp_genesis 79 this.log = $(log);
mp-wp_genesis 80 if (this.log) {
mp-wp_genesis 81 this._createLogTable();
mp-wp_genesis 82 }
mp-wp_genesis 83 },
mp-wp_genesis 84 start: function(testName) {
mp-wp_genesis 85 if (!this.log) return;
mp-wp_genesis 86 this.testName = testName;
mp-wp_genesis 87 this.lastLogLine = document.createElement('tr');
mp-wp_genesis 88 this.statusCell = document.createElement('td');
mp-wp_genesis 89 this.nameCell = document.createElement('td');
mp-wp_genesis 90 this.nameCell.className = "nameCell";
mp-wp_genesis 91 this.nameCell.appendChild(document.createTextNode(testName));
mp-wp_genesis 92 this.messageCell = document.createElement('td');
mp-wp_genesis 93 this.lastLogLine.appendChild(this.statusCell);
mp-wp_genesis 94 this.lastLogLine.appendChild(this.nameCell);
mp-wp_genesis 95 this.lastLogLine.appendChild(this.messageCell);
mp-wp_genesis 96 this.loglines.appendChild(this.lastLogLine);
mp-wp_genesis 97 },
mp-wp_genesis 98 finish: function(status, summary) {
mp-wp_genesis 99 if (!this.log) return;
mp-wp_genesis 100 this.lastLogLine.className = status;
mp-wp_genesis 101 this.statusCell.innerHTML = status;
mp-wp_genesis 102 this.messageCell.innerHTML = this._toHTML(summary);
mp-wp_genesis 103 this.addLinksToResults();
mp-wp_genesis 104 },
mp-wp_genesis 105 message: function(message) {
mp-wp_genesis 106 if (!this.log) return;
mp-wp_genesis 107 this.messageCell.innerHTML = this._toHTML(message);
mp-wp_genesis 108 },
mp-wp_genesis 109 summary: function(summary) {
mp-wp_genesis 110 if (!this.log) return;
mp-wp_genesis 111 this.logsummary.innerHTML = this._toHTML(summary);
mp-wp_genesis 112 },
mp-wp_genesis 113 _createLogTable: function() {
mp-wp_genesis 114 this.log.innerHTML =
mp-wp_genesis 115 '<div id="logsummary"></div>' +
mp-wp_genesis 116 '<table id="logtable">' +
mp-wp_genesis 117 '<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>' +
mp-wp_genesis 118 '<tbody id="loglines"></tbody>' +
mp-wp_genesis 119 '</table>';
mp-wp_genesis 120 this.logsummary = $('logsummary')
mp-wp_genesis 121 this.loglines = $('loglines');
mp-wp_genesis 122 },
mp-wp_genesis 123 _toHTML: function(txt) {
mp-wp_genesis 124 return txt.escapeHTML().replace(/\n/g,"<br/>");
mp-wp_genesis 125 },
mp-wp_genesis 126 addLinksToResults: function(){
mp-wp_genesis 127 $$("tr.failed .nameCell").each( function(td){ // todo: limit to children of this.log
mp-wp_genesis 128 td.title = "Run only this test"
mp-wp_genesis 129 Event.observe(td, 'click', function(){ window.location.search = "?tests=" + td.innerHTML;});
mp-wp_genesis 130 });
mp-wp_genesis 131 $$("tr.passed .nameCell").each( function(td){ // todo: limit to children of this.log
mp-wp_genesis 132 td.title = "Run all tests"
mp-wp_genesis 133 Event.observe(td, 'click', function(){ window.location.search = "";});
mp-wp_genesis 134 });
mp-wp_genesis 135 }
mp-wp_genesis 136 }
mp-wp_genesis 137
mp-wp_genesis 138 Test.Unit.Runner = Class.create();
mp-wp_genesis 139 Test.Unit.Runner.prototype = {
mp-wp_genesis 140 initialize: function(testcases) {
mp-wp_genesis 141 this.options = Object.extend({
mp-wp_genesis 142 testLog: 'testlog'
mp-wp_genesis 143 }, arguments[1] || {});
mp-wp_genesis 144 this.options.resultsURL = this.parseResultsURLQueryParameter();
mp-wp_genesis 145 this.options.tests = this.parseTestsQueryParameter();
mp-wp_genesis 146 if (this.options.testLog) {
mp-wp_genesis 147 this.options.testLog = $(this.options.testLog) || null;
mp-wp_genesis 148 }
mp-wp_genesis 149 if(this.options.tests) {
mp-wp_genesis 150 this.tests = [];
mp-wp_genesis 151 for(var i = 0; i < this.options.tests.length; i++) {
mp-wp_genesis 152 if(/^test/.test(this.options.tests[i])) {
mp-wp_genesis 153 this.tests.push(new Test.Unit.Testcase(this.options.tests[i], testcases[this.options.tests[i]], testcases["setup"], testcases["teardown"]));
mp-wp_genesis 154 }
mp-wp_genesis 155 }
mp-wp_genesis 156 } else {
mp-wp_genesis 157 if (this.options.test) {
mp-wp_genesis 158 this.tests = [new Test.Unit.Testcase(this.options.test, testcases[this.options.test], testcases["setup"], testcases["teardown"])];
mp-wp_genesis 159 } else {
mp-wp_genesis 160 this.tests = [];
mp-wp_genesis 161 for(var testcase in testcases) {
mp-wp_genesis 162 if(/^test/.test(testcase)) {
mp-wp_genesis 163 this.tests.push(
mp-wp_genesis 164 new Test.Unit.Testcase(
mp-wp_genesis 165 this.options.context ? ' -> ' + this.options.titles[testcase] : testcase,
mp-wp_genesis 166 testcases[testcase], testcases["setup"], testcases["teardown"]
mp-wp_genesis 167 ));
mp-wp_genesis 168 }
mp-wp_genesis 169 }
mp-wp_genesis 170 }
mp-wp_genesis 171 }
mp-wp_genesis 172 this.currentTest = 0;
mp-wp_genesis 173 this.logger = new Test.Unit.Logger(this.options.testLog);
mp-wp_genesis 174 setTimeout(this.runTests.bind(this), 1000);
mp-wp_genesis 175 },
mp-wp_genesis 176 parseResultsURLQueryParameter: function() {
mp-wp_genesis 177 return window.location.search.parseQuery()["resultsURL"];
mp-wp_genesis 178 },
mp-wp_genesis 179 parseTestsQueryParameter: function(){
mp-wp_genesis 180 if (window.location.search.parseQuery()["tests"]){
mp-wp_genesis 181 return window.location.search.parseQuery()["tests"].split(',');
mp-wp_genesis 182 };
mp-wp_genesis 183 },
mp-wp_genesis 184 // Returns:
mp-wp_genesis 185 // "ERROR" if there was an error,
mp-wp_genesis 186 // "FAILURE" if there was a failure, or
mp-wp_genesis 187 // "SUCCESS" if there was neither
mp-wp_genesis 188 getResult: function() {
mp-wp_genesis 189 var hasFailure = false;
mp-wp_genesis 190 for(var i=0;i<this.tests.length;i++) {
mp-wp_genesis 191 if (this.tests[i].errors > 0) {
mp-wp_genesis 192 return "ERROR";
mp-wp_genesis 193 }
mp-wp_genesis 194 if (this.tests[i].failures > 0) {
mp-wp_genesis 195 hasFailure = true;
mp-wp_genesis 196 }
mp-wp_genesis 197 }
mp-wp_genesis 198 if (hasFailure) {
mp-wp_genesis 199 return "FAILURE";
mp-wp_genesis 200 } else {
mp-wp_genesis 201 return "SUCCESS";
mp-wp_genesis 202 }
mp-wp_genesis 203 },
mp-wp_genesis 204 postResults: function() {
mp-wp_genesis 205 if (this.options.resultsURL) {
mp-wp_genesis 206 new Ajax.Request(this.options.resultsURL,
mp-wp_genesis 207 { method: 'get', parameters: 'result=' + this.getResult(), asynchronous: false });
mp-wp_genesis 208 }
mp-wp_genesis 209 },
mp-wp_genesis 210 runTests: function() {
mp-wp_genesis 211 var test = this.tests[this.currentTest];
mp-wp_genesis 212 if (!test) {
mp-wp_genesis 213 // finished!
mp-wp_genesis 214 this.postResults();
mp-wp_genesis 215 this.logger.summary(this.summary());
mp-wp_genesis 216 return;
mp-wp_genesis 217 }
mp-wp_genesis 218 if(!test.isWaiting) {
mp-wp_genesis 219 this.logger.start(test.name);
mp-wp_genesis 220 }
mp-wp_genesis 221 test.run();
mp-wp_genesis 222 if(test.isWaiting) {
mp-wp_genesis 223 this.logger.message("Waiting for " + test.timeToWait + "ms");
mp-wp_genesis 224 setTimeout(this.runTests.bind(this), test.timeToWait || 1000);
mp-wp_genesis 225 } else {
mp-wp_genesis 226 this.logger.finish(test.status(), test.summary());
mp-wp_genesis 227 this.currentTest++;
mp-wp_genesis 228 // tail recursive, hopefully the browser will skip the stackframe
mp-wp_genesis 229 this.runTests();
mp-wp_genesis 230 }
mp-wp_genesis 231 },
mp-wp_genesis 232 summary: function() {
mp-wp_genesis 233 var assertions = 0;
mp-wp_genesis 234 var failures = 0;
mp-wp_genesis 235 var errors = 0;
mp-wp_genesis 236 var messages = [];
mp-wp_genesis 237 for(var i=0;i<this.tests.length;i++) {
mp-wp_genesis 238 assertions += this.tests[i].assertions;
mp-wp_genesis 239 failures += this.tests[i].failures;
mp-wp_genesis 240 errors += this.tests[i].errors;
mp-wp_genesis 241 }
mp-wp_genesis 242 return (
mp-wp_genesis 243 (this.options.context ? this.options.context + ': ': '') +
mp-wp_genesis 244 this.tests.length + " tests, " +
mp-wp_genesis 245 assertions + " assertions, " +
mp-wp_genesis 246 failures + " failures, " +
mp-wp_genesis 247 errors + " errors");
mp-wp_genesis 248 }
mp-wp_genesis 249 }
mp-wp_genesis 250
mp-wp_genesis 251 Test.Unit.Assertions = Class.create();
mp-wp_genesis 252 Test.Unit.Assertions.prototype = {
mp-wp_genesis 253 initialize: function() {
mp-wp_genesis 254 this.assertions = 0;
mp-wp_genesis 255 this.failures = 0;
mp-wp_genesis 256 this.errors = 0;
mp-wp_genesis 257 this.messages = [];
mp-wp_genesis 258 },
mp-wp_genesis 259 summary: function() {
mp-wp_genesis 260 return (
mp-wp_genesis 261 this.assertions + " assertions, " +
mp-wp_genesis 262 this.failures + " failures, " +
mp-wp_genesis 263 this.errors + " errors" + "\n" +
mp-wp_genesis 264 this.messages.join("\n"));
mp-wp_genesis 265 },
mp-wp_genesis 266 pass: function() {
mp-wp_genesis 267 this.assertions++;
mp-wp_genesis 268 },
mp-wp_genesis 269 fail: function(message) {
mp-wp_genesis 270 this.failures++;
mp-wp_genesis 271 this.messages.push("Failure: " + message);
mp-wp_genesis 272 },
mp-wp_genesis 273 info: function(message) {
mp-wp_genesis 274 this.messages.push("Info: " + message);
mp-wp_genesis 275 },
mp-wp_genesis 276 error: function(error) {
mp-wp_genesis 277 this.errors++;
mp-wp_genesis 278 this.messages.push(error.name + ": "+ error.message + "(" + Test.Unit.inspect(error) +")");
mp-wp_genesis 279 },
mp-wp_genesis 280 status: function() {
mp-wp_genesis 281 if (this.failures > 0) return 'failed';
mp-wp_genesis 282 if (this.errors > 0) return 'error';
mp-wp_genesis 283 return 'passed';
mp-wp_genesis 284 },
mp-wp_genesis 285 assert: function(expression) {
mp-wp_genesis 286 var message = arguments[1] || 'assert: got "' + Test.Unit.inspect(expression) + '"';
mp-wp_genesis 287 try { expression ? this.pass() :
mp-wp_genesis 288 this.fail(message); }
mp-wp_genesis 289 catch(e) { this.error(e); }
mp-wp_genesis 290 },
mp-wp_genesis 291 assertEqual: function(expected, actual) {
mp-wp_genesis 292 var message = arguments[2] || "assertEqual";
mp-wp_genesis 293 try { (expected == actual) ? this.pass() :
mp-wp_genesis 294 this.fail(message + ': expected "' + Test.Unit.inspect(expected) +
mp-wp_genesis 295 '", actual "' + Test.Unit.inspect(actual) + '"'); }
mp-wp_genesis 296 catch(e) { this.error(e); }
mp-wp_genesis 297 },
mp-wp_genesis 298 assertInspect: function(expected, actual) {
mp-wp_genesis 299 var message = arguments[2] || "assertInspect";
mp-wp_genesis 300 try { (expected == actual.inspect()) ? this.pass() :
mp-wp_genesis 301 this.fail(message + ': expected "' + Test.Unit.inspect(expected) +
mp-wp_genesis 302 '", actual "' + Test.Unit.inspect(actual) + '"'); }
mp-wp_genesis 303 catch(e) { this.error(e); }
mp-wp_genesis 304 },
mp-wp_genesis 305 assertEnumEqual: function(expected, actual) {
mp-wp_genesis 306 var message = arguments[2] || "assertEnumEqual";
mp-wp_genesis 307 try { $A(expected).length == $A(actual).length &&
mp-wp_genesis 308 expected.zip(actual).all(function(pair) { return pair[0] == pair[1] }) ?
mp-wp_genesis 309 this.pass() : this.fail(message + ': expected ' + Test.Unit.inspect(expected) +
mp-wp_genesis 310 ', actual ' + Test.Unit.inspect(actual)); }
mp-wp_genesis 311 catch(e) { this.error(e); }
mp-wp_genesis 312 },
mp-wp_genesis 313 assertNotEqual: function(expected, actual) {
mp-wp_genesis 314 var message = arguments[2] || "assertNotEqual";
mp-wp_genesis 315 try { (expected != actual) ? this.pass() :
mp-wp_genesis 316 this.fail(message + ': got "' + Test.Unit.inspect(actual) + '"'); }
mp-wp_genesis 317 catch(e) { this.error(e); }
mp-wp_genesis 318 },
mp-wp_genesis 319 assertIdentical: function(expected, actual) {
mp-wp_genesis 320 var message = arguments[2] || "assertIdentical";
mp-wp_genesis 321 try { (expected === actual) ? this.pass() :
mp-wp_genesis 322 this.fail(message + ': expected "' + Test.Unit.inspect(expected) +
mp-wp_genesis 323 '", actual "' + Test.Unit.inspect(actual) + '"'); }
mp-wp_genesis 324 catch(e) { this.error(e); }
mp-wp_genesis 325 },
mp-wp_genesis 326 assertNotIdentical: function(expected, actual) {
mp-wp_genesis 327 var message = arguments[2] || "assertNotIdentical";
mp-wp_genesis 328 try { !(expected === actual) ? this.pass() :
mp-wp_genesis 329 this.fail(message + ': expected "' + Test.Unit.inspect(expected) +
mp-wp_genesis 330 '", actual "' + Test.Unit.inspect(actual) + '"'); }
mp-wp_genesis 331 catch(e) { this.error(e); }
mp-wp_genesis 332 },
mp-wp_genesis 333 assertNull: function(obj) {
mp-wp_genesis 334 var message = arguments[1] || 'assertNull'
mp-wp_genesis 335 try { (obj==null) ? this.pass() :
mp-wp_genesis 336 this.fail(message + ': got "' + Test.Unit.inspect(obj) + '"'); }
mp-wp_genesis 337 catch(e) { this.error(e); }
mp-wp_genesis 338 },
mp-wp_genesis 339 assertMatch: function(expected, actual) {
mp-wp_genesis 340 var message = arguments[2] || 'assertMatch';
mp-wp_genesis 341 var regex = new RegExp(expected);
mp-wp_genesis 342 try { (regex.exec(actual)) ? this.pass() :
mp-wp_genesis 343 this.fail(message + ' : regex: "' + Test.Unit.inspect(expected) + ' did not match: ' + Test.Unit.inspect(actual) + '"'); }
mp-wp_genesis 344 catch(e) { this.error(e); }
mp-wp_genesis 345 },
mp-wp_genesis 346 assertHidden: function(element) {
mp-wp_genesis 347 var message = arguments[1] || 'assertHidden';
mp-wp_genesis 348 this.assertEqual("none", element.style.display, message);
mp-wp_genesis 349 },
mp-wp_genesis 350 assertNotNull: function(object) {
mp-wp_genesis 351 var message = arguments[1] || 'assertNotNull';
mp-wp_genesis 352 this.assert(object != null, message);
mp-wp_genesis 353 },
mp-wp_genesis 354 assertType: function(expected, actual) {
mp-wp_genesis 355 var message = arguments[2] || 'assertType';
mp-wp_genesis 356 try {
mp-wp_genesis 357 (actual.constructor == expected) ? this.pass() :
mp-wp_genesis 358 this.fail(message + ': expected "' + Test.Unit.inspect(expected) +
mp-wp_genesis 359 '", actual "' + (actual.constructor) + '"'); }
mp-wp_genesis 360 catch(e) { this.error(e); }
mp-wp_genesis 361 },
mp-wp_genesis 362 assertNotOfType: function(expected, actual) {
mp-wp_genesis 363 var message = arguments[2] || 'assertNotOfType';
mp-wp_genesis 364 try {
mp-wp_genesis 365 (actual.constructor != expected) ? this.pass() :
mp-wp_genesis 366 this.fail(message + ': expected "' + Test.Unit.inspect(expected) +
mp-wp_genesis 367 '", actual "' + (actual.constructor) + '"'); }
mp-wp_genesis 368 catch(e) { this.error(e); }
mp-wp_genesis 369 },
mp-wp_genesis 370 assertInstanceOf: function(expected, actual) {
mp-wp_genesis 371 var message = arguments[2] || 'assertInstanceOf';
mp-wp_genesis 372 try {
mp-wp_genesis 373 (actual instanceof expected) ? this.pass() :
mp-wp_genesis 374 this.fail(message + ": object was not an instance of the expected type"); }
mp-wp_genesis 375 catch(e) { this.error(e); }
mp-wp_genesis 376 },
mp-wp_genesis 377 assertNotInstanceOf: function(expected, actual) {
mp-wp_genesis 378 var message = arguments[2] || 'assertNotInstanceOf';
mp-wp_genesis 379 try {
mp-wp_genesis 380 !(actual instanceof expected) ? this.pass() :
mp-wp_genesis 381 this.fail(message + ": object was an instance of the not expected type"); }
mp-wp_genesis 382 catch(e) { this.error(e); }
mp-wp_genesis 383 },
mp-wp_genesis 384 assertRespondsTo: function(method, obj) {
mp-wp_genesis 385 var message = arguments[2] || 'assertRespondsTo';
mp-wp_genesis 386 try {
mp-wp_genesis 387 (obj[method] && typeof obj[method] == 'function') ? this.pass() :
mp-wp_genesis 388 this.fail(message + ": object doesn't respond to [" + method + "]"); }
mp-wp_genesis 389 catch(e) { this.error(e); }
mp-wp_genesis 390 },
mp-wp_genesis 391 assertReturnsTrue: function(method, obj) {
mp-wp_genesis 392 var message = arguments[2] || 'assertReturnsTrue';
mp-wp_genesis 393 try {
mp-wp_genesis 394 var m = obj[method];
mp-wp_genesis 395 if(!m) m = obj['is'+method.charAt(0).toUpperCase()+method.slice(1)];
mp-wp_genesis 396 m() ? this.pass() :
mp-wp_genesis 397 this.fail(message + ": method returned false"); }
mp-wp_genesis 398 catch(e) { this.error(e); }
mp-wp_genesis 399 },
mp-wp_genesis 400 assertReturnsFalse: function(method, obj) {
mp-wp_genesis 401 var message = arguments[2] || 'assertReturnsFalse';
mp-wp_genesis 402 try {
mp-wp_genesis 403 var m = obj[method];
mp-wp_genesis 404 if(!m) m = obj['is'+method.charAt(0).toUpperCase()+method.slice(1)];
mp-wp_genesis 405 !m() ? this.pass() :
mp-wp_genesis 406 this.fail(message + ": method returned true"); }
mp-wp_genesis 407 catch(e) { this.error(e); }
mp-wp_genesis 408 },
mp-wp_genesis 409 assertRaise: function(exceptionName, method) {
mp-wp_genesis 410 var message = arguments[2] || 'assertRaise';
mp-wp_genesis 411 try {
mp-wp_genesis 412 method();
mp-wp_genesis 413 this.fail(message + ": exception expected but none was raised"); }
mp-wp_genesis 414 catch(e) {
mp-wp_genesis 415 ((exceptionName == null) || (e.name==exceptionName)) ? this.pass() : this.error(e);
mp-wp_genesis 416 }
mp-wp_genesis 417 },
mp-wp_genesis 418 assertElementsMatch: function() {
mp-wp_genesis 419 var expressions = $A(arguments), elements = $A(expressions.shift());
mp-wp_genesis 420 if (elements.length != expressions.length) {
mp-wp_genesis 421 this.fail('assertElementsMatch: size mismatch: ' + elements.length + ' elements, ' + expressions.length + ' expressions');
mp-wp_genesis 422 return false;
mp-wp_genesis 423 }
mp-wp_genesis 424 elements.zip(expressions).all(function(pair, index) {
mp-wp_genesis 425 var element = $(pair.first()), expression = pair.last();
mp-wp_genesis 426 if (element.match(expression)) return true;
mp-wp_genesis 427 this.fail('assertElementsMatch: (in index ' + index + ') expected ' + expression.inspect() + ' but got ' + element.inspect());
mp-wp_genesis 428 }.bind(this)) && this.pass();
mp-wp_genesis 429 },
mp-wp_genesis 430 assertElementMatches: function(element, expression) {
mp-wp_genesis 431 this.assertElementsMatch([element], expression);
mp-wp_genesis 432 },
mp-wp_genesis 433 benchmark: function(operation, iterations) {
mp-wp_genesis 434 var startAt = new Date();
mp-wp_genesis 435 (iterations || 1).times(operation);
mp-wp_genesis 436 var timeTaken = ((new Date())-startAt);
mp-wp_genesis 437 this.info((arguments[2] || 'Operation') + ' finished ' +
mp-wp_genesis 438 iterations + ' iterations in ' + (timeTaken/1000)+'s' );
mp-wp_genesis 439 return timeTaken;
mp-wp_genesis 440 },
mp-wp_genesis 441 _isVisible: function(element) {
mp-wp_genesis 442 element = $(element);
mp-wp_genesis 443 if(!element.parentNode) return true;
mp-wp_genesis 444 this.assertNotNull(element);
mp-wp_genesis 445 if(element.style && Element.getStyle(element, 'display') == 'none')
mp-wp_genesis 446 return false;
mp-wp_genesis 447
mp-wp_genesis 448 return this._isVisible(element.parentNode);
mp-wp_genesis 449 },
mp-wp_genesis 450 assertNotVisible: function(element) {
mp-wp_genesis 451 this.assert(!this._isVisible(element), Test.Unit.inspect(element) + " was not hidden and didn't have a hidden parent either. " + ("" || arguments[1]));
mp-wp_genesis 452 },
mp-wp_genesis 453 assertVisible: function(element) {
mp-wp_genesis 454 this.assert(this._isVisible(element), Test.Unit.inspect(element) + " was not visible. " + ("" || arguments[1]));
mp-wp_genesis 455 },
mp-wp_genesis 456 benchmark: function(operation, iterations) {
mp-wp_genesis 457 var startAt = new Date();
mp-wp_genesis 458 (iterations || 1).times(operation);
mp-wp_genesis 459 var timeTaken = ((new Date())-startAt);
mp-wp_genesis 460 this.info((arguments[2] || 'Operation') + ' finished ' +
mp-wp_genesis 461 iterations + ' iterations in ' + (timeTaken/1000)+'s' );
mp-wp_genesis 462 return timeTaken;
mp-wp_genesis 463 }
mp-wp_genesis 464 }
mp-wp_genesis 465
mp-wp_genesis 466 Test.Unit.Testcase = Class.create();
mp-wp_genesis 467 Object.extend(Object.extend(Test.Unit.Testcase.prototype, Test.Unit.Assertions.prototype), {
mp-wp_genesis 468 initialize: function(name, test, setup, teardown) {
mp-wp_genesis 469 Test.Unit.Assertions.prototype.initialize.bind(this)();
mp-wp_genesis 470 this.name = name;
mp-wp_genesis 471
mp-wp_genesis 472 if(typeof test == 'string') {
mp-wp_genesis 473 test = test.gsub(/(\.should[^\(]+\()/,'#{0}this,');
mp-wp_genesis 474 test = test.gsub(/(\.should[^\(]+)\(this,\)/,'#{1}(this)');
mp-wp_genesis 475 this.test = function() {
mp-wp_genesis 476 eval('with(this){'+test+'}');
mp-wp_genesis 477 }
mp-wp_genesis 478 } else {
mp-wp_genesis 479 this.test = test || function() {};
mp-wp_genesis 480 }
mp-wp_genesis 481
mp-wp_genesis 482 this.setup = setup || function() {};
mp-wp_genesis 483 this.teardown = teardown || function() {};
mp-wp_genesis 484 this.isWaiting = false;
mp-wp_genesis 485 this.timeToWait = 1000;
mp-wp_genesis 486 },
mp-wp_genesis 487 wait: function(time, nextPart) {
mp-wp_genesis 488 this.isWaiting = true;
mp-wp_genesis 489 this.test = nextPart;
mp-wp_genesis 490 this.timeToWait = time;
mp-wp_genesis 491 },
mp-wp_genesis 492 run: function() {
mp-wp_genesis 493 try {
mp-wp_genesis 494 try {
mp-wp_genesis 495 if (!this.isWaiting) this.setup.bind(this)();
mp-wp_genesis 496 this.isWaiting = false;
mp-wp_genesis 497 this.test.bind(this)();
mp-wp_genesis 498 } finally {
mp-wp_genesis 499 if(!this.isWaiting) {
mp-wp_genesis 500 this.teardown.bind(this)();
mp-wp_genesis 501 }
mp-wp_genesis 502 }
mp-wp_genesis 503 }
mp-wp_genesis 504 catch(e) { this.error(e); }
mp-wp_genesis 505 }
mp-wp_genesis 506 });
mp-wp_genesis 507
mp-wp_genesis 508 // *EXPERIMENTAL* BDD-style testing to please non-technical folk
mp-wp_genesis 509 // This draws many ideas from RSpec http://rspec.rubyforge.org/
mp-wp_genesis 510
mp-wp_genesis 511 Test.setupBDDExtensionMethods = function(){
mp-wp_genesis 512 var METHODMAP = {
mp-wp_genesis 513 shouldEqual: 'assertEqual',
mp-wp_genesis 514 shouldNotEqual: 'assertNotEqual',
mp-wp_genesis 515 shouldEqualEnum: 'assertEnumEqual',
mp-wp_genesis 516 shouldBeA: 'assertType',
mp-wp_genesis 517 shouldNotBeA: 'assertNotOfType',
mp-wp_genesis 518 shouldBeAn: 'assertType',
mp-wp_genesis 519 shouldNotBeAn: 'assertNotOfType',
mp-wp_genesis 520 shouldBeNull: 'assertNull',
mp-wp_genesis 521 shouldNotBeNull: 'assertNotNull',
mp-wp_genesis 522
mp-wp_genesis 523 shouldBe: 'assertReturnsTrue',
mp-wp_genesis 524 shouldNotBe: 'assertReturnsFalse',
mp-wp_genesis 525 shouldRespondTo: 'assertRespondsTo'
mp-wp_genesis 526 };
mp-wp_genesis 527 var makeAssertion = function(assertion, args, object) {
mp-wp_genesis 528 this[assertion].apply(this,(args || []).concat([object]));
mp-wp_genesis 529 }
mp-wp_genesis 530
mp-wp_genesis 531 Test.BDDMethods = {};
mp-wp_genesis 532 $H(METHODMAP).each(function(pair) {
mp-wp_genesis 533 Test.BDDMethods[pair.key] = function() {
mp-wp_genesis 534 var args = $A(arguments);
mp-wp_genesis 535 var scope = args.shift();
mp-wp_genesis 536 makeAssertion.apply(scope, [pair.value, args, this]); };
mp-wp_genesis 537 });
mp-wp_genesis 538
mp-wp_genesis 539 [Array.prototype, String.prototype, Number.prototype, Boolean.prototype].each(
mp-wp_genesis 540 function(p){ Object.extend(p, Test.BDDMethods) }
mp-wp_genesis 541 );
mp-wp_genesis 542 }
mp-wp_genesis 543
mp-wp_genesis 544 Test.context = function(name, spec, log){
mp-wp_genesis 545 Test.setupBDDExtensionMethods();
mp-wp_genesis 546
mp-wp_genesis 547 var compiledSpec = {};
mp-wp_genesis 548 var titles = {};
mp-wp_genesis 549 for(specName in spec) {
mp-wp_genesis 550 switch(specName){
mp-wp_genesis 551 case "setup":
mp-wp_genesis 552 case "teardown":
mp-wp_genesis 553 compiledSpec[specName] = spec[specName];
mp-wp_genesis 554 break;
mp-wp_genesis 555 default:
mp-wp_genesis 556 var testName = 'test'+specName.gsub(/\s+/,'-').camelize();
mp-wp_genesis 557 var body = spec[specName].toString().split('\n').slice(1);
mp-wp_genesis 558 if(/^\{/.test(body[0])) body = body.slice(1);
mp-wp_genesis 559 body.pop();
mp-wp_genesis 560 body = body.map(function(statement){
mp-wp_genesis 561 return statement.strip()
mp-wp_genesis 562 });
mp-wp_genesis 563 compiledSpec[testName] = body.join('\n');
mp-wp_genesis 564 titles[testName] = specName;
mp-wp_genesis 565 }
mp-wp_genesis 566 }
mp-wp_genesis 567 new Test.Unit.Runner(compiledSpec, { titles: titles, testLog: log || 'testlog', context: name });
mp-wp_genesis 568 };