2007-01-18 22:24:27 +00:00
|
|
|
// Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
|
|
|
|
// (c) 2005 Jon Tirsen (http://www.tirsen.com)
|
|
|
|
// (c) 2005 Michael Schuerig (http://www.schuerig.de/michael/)
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
|
|
// experimental, Firefox-only
|
|
|
|
Event.simulateMouse = function(element, eventName) {
|
|
|
|
var options = Object.extend({
|
|
|
|
pointerX: 0,
|
|
|
|
pointerY: 0,
|
|
|
|
buttons: 0
|
|
|
|
}, arguments[2] || {});
|
|
|
|
var oEvent = document.createEvent("MouseEvents");
|
|
|
|
oEvent.initMouseEvent(eventName, true, true, document.defaultView,
|
|
|
|
options.buttons, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
|
|
|
|
false, false, false, false, 0, $(element));
|
|
|
|
|
|
|
|
if(this.mark) Element.remove(this.mark);
|
2008-01-06 00:34:39 +00:00
|
|
|
|
|
|
|
var style = 'position: absolute; width: 5px; height: 5px;' +
|
|
|
|
'top: #{pointerY}px; left: #{pointerX}px;'.interpolate(options) +
|
|
|
|
'border-top: 1px solid red; border-left: 1px solid red;'
|
|
|
|
|
|
|
|
this.mark = new Element('div', { style: style });
|
2007-01-18 22:24:27 +00:00
|
|
|
this.mark.appendChild(document.createTextNode(" "));
|
|
|
|
document.body.appendChild(this.mark);
|
|
|
|
|
|
|
|
if(this.step)
|
|
|
|
alert('['+new Date().getTime().toString()+'] '+eventName+'/'+Test.Unit.inspect(options));
|
|
|
|
|
|
|
|
$(element).dispatchEvent(oEvent);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
// You need to downgrade to 1.0.4 for now to get this working
|
|
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=289940 for the fix that fixed too much
|
|
|
|
Event.simulateKey = function(element, eventName) {
|
|
|
|
var options = Object.extend({
|
|
|
|
ctrlKey: false,
|
|
|
|
altKey: false,
|
|
|
|
shiftKey: false,
|
|
|
|
metaKey: false,
|
|
|
|
keyCode: 0,
|
|
|
|
charCode: 0
|
|
|
|
}, arguments[2] || {});
|
|
|
|
|
|
|
|
var oEvent = document.createEvent("KeyEvents");
|
|
|
|
oEvent.initKeyEvent(eventName, true, true, window,
|
|
|
|
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
|
|
|
|
options.keyCode, options.charCode );
|
|
|
|
$(element).dispatchEvent(oEvent);
|
|
|
|
};
|
|
|
|
|
|
|
|
Event.simulateKeys = function(element, command) {
|
|
|
|
for(var i=0; i<command.length; i++) {
|
|
|
|
Event.simulateKey(element,'keypress',{charCode:command.charCodeAt(i)});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-01-06 00:34:39 +00:00
|
|
|
var Test = {
|
|
|
|
Unit: {
|
|
|
|
inspect: Object.inspect // security exception workaround
|
|
|
|
}
|
|
|
|
};
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-01-06 00:34:39 +00:00
|
|
|
Test.Unit.Logger = Class.create({
|
|
|
|
initialize: function(element) {
|
|
|
|
this.element = $(element);
|
|
|
|
if (this.element) this._createLogTable();
|
2008-03-31 17:07:46 +00:00
|
|
|
this.tbody = $(this.element.getElementsByTagName('tbody')[0]);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
start: function(testName) {
|
2008-01-06 00:34:39 +00:00
|
|
|
if (!this.element) return;
|
2008-03-31 17:07:46 +00:00
|
|
|
this.tbody.insert('<tr><td>' + testName + '</td><td></td><td></td></tr>');
|
2008-01-06 00:34:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setStatus: function(status) {
|
2008-03-31 17:07:46 +00:00
|
|
|
this.getLastLogLine().addClassName(status);
|
|
|
|
$(this.getLastLogLine().getElementsByTagName('td')[1]).update(status);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
finish: function(status, summary) {
|
2008-01-06 00:34:39 +00:00
|
|
|
if (!this.element) return;
|
|
|
|
this.setStatus(status);
|
|
|
|
this.message(summary);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
message: function(message) {
|
2008-01-06 00:34:39 +00:00
|
|
|
if (!this.element) return;
|
|
|
|
this.getMessageCell().update(this._toHTML(message));
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
summary: function(summary) {
|
2008-01-06 00:34:39 +00:00
|
|
|
if (!this.element) return;
|
2008-03-31 17:07:46 +00:00
|
|
|
var div = $(this.element.getElementsByTagName('div')[0]);
|
|
|
|
div.update(this._toHTML(summary));
|
2008-01-06 00:34:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getLastLogLine: function() {
|
2008-03-31 17:51:44 +00:00
|
|
|
//return this.element.descendants('tr').last();
|
2008-03-31 17:07:46 +00:00
|
|
|
var trs = this.element.getElementsByTagName('tr');
|
|
|
|
return $(trs[trs.length - 1]);
|
2008-01-06 00:34:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getMessageCell: function() {
|
2008-03-31 17:51:44 +00:00
|
|
|
return this.getLastLogLine().down('td', 2);
|
2008-03-31 17:07:46 +00:00
|
|
|
var tds = this.getLastLogLine().getElementsByTagName('td');
|
|
|
|
return $(tds[2]);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
_createLogTable: function() {
|
2008-01-06 00:34:39 +00:00
|
|
|
var html = '<div class="logsummary">running...</div>' +
|
|
|
|
'<table class="logtable">' +
|
2007-01-18 22:24:27 +00:00
|
|
|
'<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>' +
|
2008-01-06 00:34:39 +00:00
|
|
|
'<tbody class="loglines"></tbody>' +
|
2007-01-18 22:24:27 +00:00
|
|
|
'</table>';
|
2008-01-06 00:44:18 +00:00
|
|
|
this.element.update(html);
|
2008-01-06 00:34:39 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
appendActionButtons: function(actions) {
|
|
|
|
actions = $H(actions);
|
|
|
|
if (!actions.any()) return;
|
|
|
|
var div = new Element("div", {className: 'action_buttons'});
|
|
|
|
actions.inject(div, function(container, action) {
|
|
|
|
var button = new Element("input").setValue(action.key).observe("click", action.value);
|
|
|
|
button.type = "button";
|
|
|
|
return container.insert(button);
|
|
|
|
});
|
|
|
|
this.getMessageCell().insert(div);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
_toHTML: function(txt) {
|
2008-03-31 17:07:46 +00:00
|
|
|
return txt.escapeHTML().replace(/\n/g,"<br />");
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
2008-01-06 00:34:39 +00:00
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-01-06 00:34:39 +00:00
|
|
|
Test.Unit.Runner = Class.create({
|
2007-01-18 22:24:27 +00:00
|
|
|
initialize: function(testcases) {
|
2008-01-06 00:34:39 +00:00
|
|
|
var options = this.options = Object.extend({
|
2007-01-18 22:24:27 +00:00
|
|
|
testLog: 'testlog'
|
|
|
|
}, arguments[1] || {});
|
2008-01-06 00:34:39 +00:00
|
|
|
|
|
|
|
options.resultsURL = this.queryParams.resultsURL;
|
|
|
|
|
2008-01-06 07:38:02 +00:00
|
|
|
this.tests = this.getTests(testcases);
|
2007-01-18 22:24:27 +00:00
|
|
|
this.currentTest = 0;
|
2008-03-16 20:00:07 +00:00
|
|
|
|
2008-01-06 00:34:39 +00:00
|
|
|
Event.observe(window, "load", function() {
|
2008-03-16 20:00:07 +00:00
|
|
|
this.logger = new Test.Unit.Logger($(options.testLog));
|
2008-01-06 00:34:39 +00:00
|
|
|
this.runTests.bind(this).delay(0.1);
|
2007-08-04 07:20:45 +00:00
|
|
|
}.bind(this));
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
|
|
|
queryParams: window.location.search.parseQuery(),
|
|
|
|
|
2008-01-06 07:38:02 +00:00
|
|
|
getTests: function(testcases) {
|
|
|
|
var tests, options = this.options;
|
2008-01-06 00:34:39 +00:00
|
|
|
if (this.queryParams.tests) tests = this.queryParams.tests.split(',');
|
|
|
|
else if (options.tests) tests = options.tests;
|
|
|
|
else if (options.test) tests = [option.test];
|
|
|
|
else tests = Object.keys(testcases).grep(/^test/);
|
|
|
|
|
|
|
|
return tests.map(function(test) {
|
|
|
|
if (testcases[test])
|
|
|
|
return new Test.Unit.Testcase(test, testcases[test], testcases.setup, testcases.teardown);
|
|
|
|
}).compact();
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
getResult: function() {
|
2007-10-21 01:47:30 +00:00
|
|
|
var results = {
|
|
|
|
tests: this.tests.length,
|
|
|
|
assertions: 0,
|
|
|
|
failures: 0,
|
|
|
|
errors: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.tests.inject(results, function(results, test) {
|
|
|
|
results.assertions += test.assertions;
|
|
|
|
results.failures += test.failures;
|
|
|
|
results.errors += test.errors;
|
|
|
|
return results;
|
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2007-10-21 01:47:30 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
postResults: function() {
|
|
|
|
if (this.options.resultsURL) {
|
|
|
|
new Ajax.Request(this.options.resultsURL,
|
2007-10-21 01:47:30 +00:00
|
|
|
{ method: 'get', parameters: this.getResult(), asynchronous: false });
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
runTests: function() {
|
2008-01-06 00:34:39 +00:00
|
|
|
var test = this.tests[this.currentTest], actions;
|
|
|
|
|
|
|
|
if (!test) return this.finish();
|
|
|
|
if (!test.isWaiting) this.logger.start(test.name);
|
2007-01-18 22:24:27 +00:00
|
|
|
test.run();
|
|
|
|
if(test.isWaiting) {
|
|
|
|
this.logger.message("Waiting for " + test.timeToWait + "ms");
|
|
|
|
setTimeout(this.runTests.bind(this), test.timeToWait || 1000);
|
2008-01-06 00:34:39 +00:00
|
|
|
return;
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
2008-01-06 00:34:39 +00:00
|
|
|
|
|
|
|
this.logger.finish(test.status(), test.summary());
|
|
|
|
if (actions = test.actions) this.logger.appendActionButtons(actions);
|
|
|
|
this.currentTest++;
|
|
|
|
// tail recursive, hopefully the browser will skip the stackframe
|
|
|
|
this.runTests();
|
|
|
|
},
|
|
|
|
|
|
|
|
finish: function() {
|
|
|
|
this.postResults();
|
|
|
|
this.logger.summary(this.summary());
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2007-10-21 01:47:30 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
summary: function() {
|
2007-10-21 01:47:30 +00:00
|
|
|
return '#{tests} tests, #{assertions} assertions, #{failures} failures, #{errors} errors'
|
|
|
|
.interpolate(this.getResult());
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
2008-01-06 00:34:39 +00:00
|
|
|
});
|
2007-08-09 21:48:14 +00:00
|
|
|
|
2008-01-14 14:16:27 +00:00
|
|
|
Test.Unit.MessageTemplate = Class.create({
|
|
|
|
initialize: function(string) {
|
|
|
|
var parts = [];
|
|
|
|
(string || '').scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/, function(part) {
|
|
|
|
parts.push(part[0]);
|
|
|
|
});
|
|
|
|
this.parts = parts;
|
|
|
|
},
|
|
|
|
|
|
|
|
evaluate: function(params) {
|
|
|
|
return this.parts.map(function(part) {
|
|
|
|
return part == '?' ? Test.Unit.inspect(params.shift()) : part.replace(/\\\?/, '?');
|
|
|
|
}).join('');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2008-01-06 00:34:39 +00:00
|
|
|
Test.Unit.Assertions = {
|
2008-01-14 14:16:27 +00:00
|
|
|
buildMessage: function(message, template) {
|
|
|
|
var args = $A(arguments).slice(2);
|
|
|
|
return (message ? message + '\n' : '') + new Test.Unit.MessageTemplate(template).evaluate(args);
|
|
|
|
},
|
|
|
|
|
|
|
|
flunk: function(message) {
|
|
|
|
this.assertBlock(message || 'Flunked', function() { return false });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertBlock: function(message, block) {
|
|
|
|
try {
|
|
|
|
block.call(this) ? this.pass() : this.fail(message);
|
|
|
|
} catch(e) { this.error(e) }
|
|
|
|
},
|
|
|
|
|
|
|
|
assert: function(expression, message) {
|
|
|
|
message = this.buildMessage(message || 'assert', 'got <?>', expression);
|
|
|
|
this.assertBlock(message, function() { return expression });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertEqual: function(expected, actual, message) {
|
|
|
|
message = this.buildMessage(message || 'assertEqual', 'expected <?>, actual: <?>', expected, actual);
|
|
|
|
this.assertBlock(message, function() { return expected == actual });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertNotEqual: function(expected, actual, message) {
|
|
|
|
message = this.buildMessage(message || 'assertNotEqual', 'expected <?>, actual: <?>', expected, actual);
|
|
|
|
this.assertBlock(message, function() { return expected != actual });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertEnumEqual: function(expected, actual, message) {
|
2007-03-09 04:12:13 +00:00
|
|
|
expected = $A(expected);
|
|
|
|
actual = $A(actual);
|
2008-01-14 14:16:27 +00:00
|
|
|
message = this.buildMessage(message || 'assertEnumEqual', 'expected <?>, actual: <?>', expected, actual);
|
|
|
|
this.assertBlock(message, function() {
|
|
|
|
return expected.length == actual.length && expected.zip(actual).all(function(pair) { return pair[0] == pair[1] });
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
assertEnumNotEqual: function(expected, actual, message) {
|
2007-03-27 20:55:56 +00:00
|
|
|
expected = $A(expected);
|
|
|
|
actual = $A(actual);
|
2008-01-14 14:16:27 +00:00
|
|
|
message = this.buildMessage(message || 'assertEnumNotEqual', '<?> was the same as <?>', expected, actual);
|
|
|
|
this.assertBlock(message, function() {
|
|
|
|
return expected.length != actual.length || expected.zip(actual).any(function(pair) { return pair[0] != pair[1] });
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
assertHashEqual: function(expected, actual, message) {
|
2007-03-27 20:55:56 +00:00
|
|
|
expected = $H(expected);
|
|
|
|
actual = $H(actual);
|
|
|
|
var expected_array = expected.toArray().sort(), actual_array = actual.toArray().sort();
|
2008-01-14 14:16:27 +00:00
|
|
|
message = this.buildMessage(message || 'assertHashEqual', 'expected <?>, actual: <?>', expected, actual);
|
2007-03-27 20:55:56 +00:00
|
|
|
// from now we recursively zip & compare nested arrays
|
2008-01-14 14:16:27 +00:00
|
|
|
var block = function() {
|
|
|
|
return expected_array.length == actual_array.length &&
|
|
|
|
expected_array.zip(actual_array).all(function(pair) {
|
|
|
|
return pair.all(Object.isArray) ?
|
|
|
|
pair[0].zip(pair[1]).all(arguments.callee) : pair[0] == pair[1];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
this.assertBlock(message, block);
|
|
|
|
},
|
|
|
|
|
|
|
|
assertHashNotEqual: function(expected, actual, message) {
|
2007-03-27 20:55:56 +00:00
|
|
|
expected = $H(expected);
|
|
|
|
actual = $H(actual);
|
|
|
|
var expected_array = expected.toArray().sort(), actual_array = actual.toArray().sort();
|
2008-01-14 14:16:27 +00:00
|
|
|
message = this.buildMessage(message || 'assertHashNotEqual', '<?> was the same as <?>', expected, actual);
|
2007-03-27 20:55:56 +00:00
|
|
|
// from now we recursively zip & compare nested arrays
|
2008-01-14 14:16:27 +00:00
|
|
|
var block = function() {
|
|
|
|
return !(expected_array.length == actual_array.length &&
|
|
|
|
expected_array.zip(actual_array).all(function(pair) {
|
|
|
|
return pair.all(Object.isArray) ?
|
|
|
|
pair[0].zip(pair[1]).all(arguments.callee) : pair[0] == pair[1];
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
this.assertBlock(message, block);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-14 14:16:27 +00:00
|
|
|
|
|
|
|
assertIdentical: function(expected, actual, message) {
|
|
|
|
message = this.buildMessage(message || 'assertIdentical', 'expected <?>, actual: <?>', expected, actual);
|
|
|
|
this.assertBlock(message, function() { return expected === actual });
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-14 14:16:27 +00:00
|
|
|
|
|
|
|
assertNotIdentical: function(expected, actual, message) {
|
|
|
|
message = this.buildMessage(message || 'assertNotIdentical', 'expected <?>, actual: <?>', expected, actual);
|
|
|
|
this.assertBlock(message, function() { return expected !== actual });
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-14 14:16:27 +00:00
|
|
|
|
|
|
|
assertNull: function(obj, message) {
|
|
|
|
message = this.buildMessage(message || 'assertNull', 'got <?>', obj);
|
|
|
|
this.assertBlock(message, function() { return obj === null });
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-14 14:16:27 +00:00
|
|
|
|
|
|
|
assertNotNull: function(obj, message) {
|
|
|
|
message = this.buildMessage(message || 'assertNotNull', 'got <?>', obj);
|
|
|
|
this.assertBlock(message, function() { return obj !== null });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertUndefined: function(obj, message) {
|
|
|
|
message = this.buildMessage(message || 'assertUndefined', 'got <?>', obj);
|
|
|
|
this.assertBlock(message, function() { return typeof obj == "undefined" });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertNotUndefined: function(obj, message) {
|
|
|
|
message = this.buildMessage(message || 'assertNotUndefined', 'got <?>', obj);
|
|
|
|
this.assertBlock(message, function() { return typeof obj != "undefined" });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertNullOrUndefined: function(obj, message) {
|
|
|
|
message = this.buildMessage(message || 'assertNullOrUndefined', 'got <?>', obj);
|
|
|
|
this.assertBlock(message, function() { return obj == null });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertNotNullOrUndefined: function(obj, message) {
|
|
|
|
message = this.buildMessage(message || 'assertNotNullOrUndefined', 'got <?>', obj);
|
|
|
|
this.assertBlock(message, function() { return obj != null });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertMatch: function(expected, actual, message) {
|
|
|
|
message = this.buildMessage(message || 'assertMatch', 'regex <?> did not match <?>', expected, actual);
|
|
|
|
this.assertBlock(message, function() { return new RegExp(expected).exec(actual) });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertNoMatch: function(expected, actual, message) {
|
|
|
|
message = this.buildMessage(message || 'assertNoMatch', 'regex <?> matched <?>', expected, actual);
|
|
|
|
this.assertBlock(message, function() { return !(new RegExp(expected).exec(actual)) });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertHidden: function(element, message) {
|
2008-01-14 15:37:30 +00:00
|
|
|
message = this.buildMessage(message || 'assertHidden', '? isn\'t hidden.', element);
|
2008-01-14 14:16:27 +00:00
|
|
|
this.assertBlock(message, function() { return element.style.display == 'none' });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertInstanceOf: function(expected, actual, message) {
|
|
|
|
message = this.buildMessage(message || 'assertInstanceOf', '<?> was not an instance of the expected type', actual);
|
|
|
|
this.assertBlock(message, function() { return actual instanceof expected });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertNotInstanceOf: function(expected, actual, message) {
|
|
|
|
message = this.buildMessage(message || 'assertNotInstanceOf', '<?> was an instance of the expected type', actual);
|
|
|
|
this.assertBlock(message, function() { return !(actual instanceof expected) });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertRespondsTo: function(method, obj, message) {
|
|
|
|
message = this.buildMessage(message || 'assertRespondsTo', 'object doesn\'t respond to <?>', method);
|
|
|
|
this.assertBlock(message, function() { return (method in obj && typeof obj[method] == 'function') });
|
|
|
|
},
|
|
|
|
|
|
|
|
assertRaise: function(exceptionName, method, message) {
|
|
|
|
message = this.buildMessage(message || 'assertRaise', '<?> exception expected but none was raised', exceptionName);
|
|
|
|
var block = function() {
|
|
|
|
try {
|
|
|
|
method();
|
|
|
|
return false;
|
|
|
|
} catch(e) {
|
|
|
|
if (e.name == exceptionName) return true;
|
|
|
|
else throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.assertBlock(message, block);
|
|
|
|
},
|
|
|
|
|
|
|
|
assertNothingRaised: function(method, message) {
|
|
|
|
try {
|
2007-01-18 22:24:27 +00:00
|
|
|
method();
|
2008-01-14 14:16:27 +00:00
|
|
|
this.assert(true, "Expected nothing to be thrown");
|
|
|
|
} catch(e) {
|
|
|
|
message = this.buildMessage(message || 'assertNothingRaised', '<?> was thrown when nothing was expected.', e);
|
|
|
|
this.flunk(message);
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
},
|
2008-01-14 14:16:27 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
_isVisible: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
if(!element.parentNode) return true;
|
|
|
|
this.assertNotNull(element);
|
|
|
|
if(element.style && Element.getStyle(element, 'display') == 'none')
|
|
|
|
return false;
|
|
|
|
|
2008-01-14 14:16:27 +00:00
|
|
|
return arguments.callee.call(this, element.parentNode);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-14 14:16:27 +00:00
|
|
|
|
|
|
|
assertVisible: function(element, message) {
|
|
|
|
message = this.buildMessage(message, '? was not visible.', element);
|
|
|
|
this.assertBlock(message, function() { return this._isVisible(element) });
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-14 14:16:27 +00:00
|
|
|
|
|
|
|
assertNotVisible: function(element, message) {
|
|
|
|
message = this.buildMessage(message, '? was not hidden and didn\'t have a hidden parent either.', element);
|
|
|
|
this.assertBlock(message, function() { return !this._isVisible(element) });
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-14 14:16:27 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
assertElementsMatch: function() {
|
2008-09-08 07:48:24 +00:00
|
|
|
var message, pass = true, expressions = $A(arguments), elements = $A(expressions.shift());
|
2007-01-18 22:24:27 +00:00
|
|
|
if (elements.length != expressions.length) {
|
2008-01-14 14:16:27 +00:00
|
|
|
message = this.buildMessage('assertElementsMatch', 'size mismatch: ? elements, ? expressions (?).', elements.length, expressions.length, expressions);
|
|
|
|
this.flunk(message);
|
|
|
|
pass = false;
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
elements.zip(expressions).all(function(pair, index) {
|
|
|
|
var element = $(pair.first()), expression = pair.last();
|
|
|
|
if (element.match(expression)) return true;
|
2008-01-14 14:16:27 +00:00
|
|
|
message = this.buildMessage('assertElementsMatch', 'In index <?>: expected <?> but got ?', index, expression, element);
|
|
|
|
this.flunk(message);
|
|
|
|
pass = false;
|
|
|
|
}.bind(this))
|
|
|
|
|
|
|
|
if (pass) this.assert(true, "Expected all elements to match.");
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-14 14:16:27 +00:00
|
|
|
|
|
|
|
assertElementMatches: function(element, expression, message) {
|
2007-01-18 22:24:27 +00:00
|
|
|
this.assertElementsMatch([element], expression);
|
|
|
|
}
|
2008-01-06 00:34:39 +00:00
|
|
|
};
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-01-06 00:34:39 +00:00
|
|
|
Test.Unit.Testcase = Class.create(Test.Unit.Assertions, {
|
2007-01-18 22:24:27 +00:00
|
|
|
initialize: function(name, test, setup, teardown) {
|
|
|
|
this.name = name;
|
2008-01-06 21:49:16 +00:00
|
|
|
this.test = test || Prototype.emptyFunction;
|
|
|
|
this.setup = setup || Prototype.emptyFunction;
|
2008-01-06 00:34:39 +00:00
|
|
|
this.teardown = teardown || Prototype.emptyFunction;
|
|
|
|
this.messages = [];
|
|
|
|
this.actions = {};
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
|
|
|
isWaiting: false,
|
|
|
|
timeToWait: 1000,
|
|
|
|
assertions: 0,
|
|
|
|
failures: 0,
|
|
|
|
errors: 0,
|
|
|
|
isRunningFromRake: window.location.port == 4711,
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
wait: function(time, nextPart) {
|
|
|
|
this.isWaiting = true;
|
|
|
|
this.test = nextPart;
|
|
|
|
this.timeToWait = time;
|
|
|
|
},
|
2008-01-06 00:34:39 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
run: function(rethrow) {
|
|
|
|
try {
|
|
|
|
try {
|
2008-01-06 00:34:39 +00:00
|
|
|
if (!this.isWaiting) this.setup();
|
2007-01-18 22:24:27 +00:00
|
|
|
this.isWaiting = false;
|
2008-01-06 00:34:39 +00:00
|
|
|
this.test();
|
2007-01-18 22:24:27 +00:00
|
|
|
} finally {
|
|
|
|
if(!this.isWaiting) {
|
2008-01-06 00:34:39 +00:00
|
|
|
this.teardown();
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
if (rethrow) throw e;
|
|
|
|
this.error(e, this);
|
|
|
|
}
|
2008-01-06 00:34:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
summary: function() {
|
|
|
|
var msg = '#{assertions} assertions, #{failures} failures, #{errors} errors\n';
|
|
|
|
return msg.interpolate(this) + this.messages.join("\n");
|
|
|
|
},
|
|
|
|
|
|
|
|
pass: function() {
|
|
|
|
this.assertions++;
|
|
|
|
},
|
|
|
|
|
|
|
|
fail: function(message) {
|
|
|
|
this.failures++;
|
|
|
|
var line = "";
|
|
|
|
try {
|
|
|
|
throw new Error("stack");
|
|
|
|
} catch(e){
|
|
|
|
line = (/\.html:(\d+)/.exec(e.stack || '') || ['',''])[1];
|
|
|
|
}
|
|
|
|
this.messages.push("Failure: " + message + (line ? " Line #" + line : ""));
|
|
|
|
},
|
|
|
|
|
|
|
|
info: function(message) {
|
|
|
|
this.messages.push("Info: " + message);
|
|
|
|
},
|
|
|
|
|
|
|
|
error: function(error, test) {
|
|
|
|
this.errors++;
|
|
|
|
this.actions['retry with throw'] = function() { test.run(true) };
|
|
|
|
this.messages.push(error.name + ": "+ error.message + "(" + Test.Unit.inspect(error) + ")");
|
|
|
|
},
|
|
|
|
|
|
|
|
status: function() {
|
|
|
|
if (this.failures > 0) return 'failed';
|
|
|
|
if (this.errors > 0) return 'error';
|
|
|
|
return 'passed';
|
|
|
|
},
|
|
|
|
|
|
|
|
benchmark: function(operation, iterations) {
|
|
|
|
var startAt = new Date();
|
|
|
|
(iterations || 1).times(operation);
|
|
|
|
var timeTaken = ((new Date())-startAt);
|
|
|
|
this.info((arguments[2] || 'Operation') + ' finished ' +
|
|
|
|
iterations + ' iterations in ' + (timeTaken/1000)+'s' );
|
|
|
|
return timeTaken;
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
});
|