dom/tests/mochitest/ajax/scriptaculous/src/unittest.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

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

mercurial