Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

prototype: Test.Unit refactoring. Allow running multiple instances of…

… Test.Unit.Runner on the same page. Allow rake to run specific testcases. Closes #10704, #10705, #10706.
  • Loading branch information...
commit 0008fe4aedf3550086caa8e686889597a07e5c0a 1 parent 7c86e29
@tobie tobie authored
View
2  CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Test.Unit refactoring. Allow running multiple instances of Test.Unit.Runner on the same page. Allow rake to run specific testcases (e.g.: rake test BROWSERS=firefox TESTS=array TESTCASES=testUniq,test$w). Closes #10704, #10705, #10706. [nicwilliams, Tobie Langel]
+
* Optimize property detection of outerHTML. Avoids triggering FOUC in Safari 3.0.4. Closes #10702. [subimage, Tobie Langel]
* Add document.loaded, a boolean that is set to true once dom:loaded is fired. Setting document.loaded to true before the document is loaded prevents dom:loaded from being fired. [Tobie Langel]
View
9 Rakefile
@@ -37,16 +37,17 @@ task :test => [:dist, :test_units]
require 'test/lib/jstest'
desc "Runs all the JavaScript unit tests and collects the results"
JavaScriptTestTask.new(:test_units) do |t|
+ testcases = ENV['TESTCASES']
tests_to_run = ENV['TESTS'] && ENV['TESTS'].split(',')
browsers_to_test = ENV['BROWSERS'] && ENV['BROWSERS'].split(',')
-
+
t.mount("/dist")
t.mount("/test")
Dir["test/unit/*.html"].sort.each do |test_file|
- test_file = "/#{test_file}"
- test_name = test_file[/.*\/(.+?)\.html/, 1]
- t.run(test_file) unless tests_to_run && !tests_to_run.include?(test_name)
+ tests = testcases ? { :url => "/#{test_file}", :testcases => testcases } : "/#{test_file}"
+ test_filename = test_file[/.*\/(.+?)\.html/, 1]
+ t.run(tests) unless tests_to_run && !tests_to_run.include?(test_filename)
end
%w( safari firefox ie konqueror opera ).each do |browser|
View
2  src/dom.js
@@ -185,7 +185,7 @@ Element.Methods = {
},
descendants: function(element) {
- return $(element).getElementsBySelector("*");
+ return $(element).select("*");
},
firstDescendant: function(element) {
View
10 test/lib/jstest.rb
@@ -308,7 +308,12 @@ def define
browser.setup
puts "\nStarted tests in #{browser}"
@tests.each do |test|
- browser.visit("http://localhost:4711#{test}?resultsURL=http://localhost:4711/results&t=" + ("%.6f" % Time.now.to_f))
+ params = "resultsURL=http://localhost:4711/results&t=" + ("%.6f" % Time.now.to_f)
+ if test.is_a?(Hash)
+ params << "&tests=#{test[:testcases]}" if test[:testcases]
+ test = test[:url]
+ end
+ browser.visit("http://localhost:4711#{test}?#{params}")
result = @queue.pop
result.each { |k, v| results[k] += v }
@@ -349,7 +354,8 @@ def mount(path, dir=nil)
@server.mount(path, NonCachingFileHandler, dir)
end
- # test should be specified as a url
+ # test should be specified as a url or as a hash of the form
+ # {:url => "url", :testcases => "testFoo,testBar"}
def run(test)
@tests<<test
end
View
350 test/lib/unittest.js
@@ -35,16 +35,14 @@ Event.simulateMouse = function(element, eventName) {
false, false, false, false, 0, $(element));
if(this.mark) Element.remove(this.mark);
- this.mark = document.createElement('div');
+
+ 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 });
this.mark.appendChild(document.createTextNode(" "));
document.body.appendChild(this.mark);
- this.mark.style.position = 'absolute';
- this.mark.style.top = options.pointerY + "px";
- this.mark.style.left = options.pointerX + "px";
- this.mark.style.width = "5px";
- this.mark.style.height = "5px;";
- this.mark.style.borderTop = "1px solid red;"
- this.mark.style.borderLeft = "1px solid red;"
if(this.step)
alert('['+new Date().getTime().toString()+'] '+eventName+'/'+Test.Unit.inspect(options));
@@ -78,104 +76,110 @@ Event.simulateKeys = function(element, command) {
}
};
-var Test = {}
-Test.Unit = {};
-
-// security exception workaround
-Test.Unit.inspect = Object.inspect;
+var Test = {
+ Unit: {
+ inspect: Object.inspect // security exception workaround
+ }
+};
-Test.Unit.Logger = Class.create();
-Test.Unit.Logger.prototype = {
- initialize: function(log) {
- this.log = $(log);
- if (this.log) {
- this._createLogTable();
- }
+Test.Unit.Logger = Class.create({
+ initialize: function(element) {
+ this.element = $(element);
+ if (this.element) this._createLogTable();
},
+
start: function(testName) {
- if (!this.log) return;
- this.testName = testName;
- this.lastLogLine = document.createElement('tr');
- this.statusCell = document.createElement('td');
- this.nameCell = document.createElement('td');
- this.nameCell.appendChild(document.createTextNode(testName));
- this.messageCell = document.createElement('td');
- this.lastLogLine.appendChild(this.statusCell);
- this.lastLogLine.appendChild(this.nameCell);
- this.lastLogLine.appendChild(this.messageCell);
- this.loglines.appendChild(this.lastLogLine);
+ if (!this.element) return;
+ this.element.down('tbody').insert('<tr><td>' + testName + '</td><td></td><td></td></tr>');
+ },
+
+ setStatus: function(status) {
+ this.getLastLogLine().addClassName(status).down('td', 1).update(status);
},
+
finish: function(status, summary) {
- if (!this.log) return;
- this.lastLogLine.className = status;
- this.statusCell.innerHTML = status;
- this.messageCell.innerHTML = this._toHTML(summary);
+ if (!this.element) return;
+ this.setStatus(status);
+ this.message(summary);
},
+
message: function(message) {
- if (!this.log) return;
- this.messageCell.innerHTML = this._toHTML(message);
+ if (!this.element) return;
+ this.getMessageCell().update(this._toHTML(message));
},
+
summary: function(summary) {
- if (!this.log) return;
- this.logsummary.innerHTML = this._toHTML(summary);
+ if (!this.element) return;
+ this.element.down('div').update(this._toHTML(summary));
+ },
+
+ getLastLogLine: function() {
+ return this.element.select('tr').last()
+ },
+
+ getMessageCell: function() {
+ return this.getLastLogLine().down('td', 2);
},
+
_createLogTable: function() {
- this.log.innerHTML =
- '<div id="logsummary"></div>' +
- '<table id="logtable">' +
+ var html = '<div class="logsummary">running...</div>' +
+ '<table class="logtable">' +
'<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>' +
- '<tbody id="loglines"></tbody>' +
+ '<tbody class="loglines"></tbody>' +
'</table>';
- this.logsummary = $('logsummary')
- this.loglines = $('loglines');
+ this.element.update(html)
+
+ },
+
+ 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);
},
+
_toHTML: function(txt) {
return txt.escapeHTML().replace(/\n/g,"<br/>");
}
-}
+});
-Test.Unit.Runner = Class.create();
-Test.Unit.Runner.prototype = {
+Test.Unit.Runner = Class.create({
initialize: function(testcases) {
- this.options = Object.extend({
+ var options = this.options = Object.extend({
testLog: 'testlog'
}, arguments[1] || {});
- this.options.resultsURL = this.parseResultsURLQueryParameter();
- if (this.options.testLog) {
- this.options.testLog = $(this.options.testLog) || null;
- }
- if(this.options.tests) {
- this.tests = [];
- for(var i = 0; i < this.options.tests.length; i++) {
- if(/^test/.test(this.options.tests[i])) {
- this.tests.push(new Test.Unit.Testcase(this.options.tests[i], testcases[this.options.tests[i]], testcases["setup"], testcases["teardown"]));
- }
- }
- } else {
- if (this.options.test) {
- this.tests = [new Test.Unit.Testcase(this.options.test, testcases[this.options.test], testcases["setup"], testcases["teardown"])];
- } else {
- this.tests = [];
- for(var testcase in testcases) {
- if(/^test/.test(testcase)) {
- this.tests.push(new Test.Unit.Testcase(testcase, testcases[testcase], testcases["setup"], testcases["teardown"]));
- }
- }
- }
- }
+
+ options.resultsURL = this.queryParams.resultsURL;
+ options.testLog = $(options.testLog);
+
+ this.tests = this.getTests(testcases, options);
this.currentTest = 0;
- this.logger = new Test.Unit.Logger(this.options.testLog);
- Event.observe(window, "load", function() {
- setTimeout(this.runTests.bind(this), 100);
+ this.logger = new Test.Unit.Logger(options.testLog);
+ Event.observe(window, "load", function() {
+ this.runTests.bind(this).delay(0.1);
}.bind(this));
},
- parseResultsURLQueryParameter: function() {
- return window.location.search.parseQuery()["resultsURL"];
+
+ queryParams: window.location.search.parseQuery(),
+
+ getTests: function(testcases, options) {
+ var tests;
+ 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();
},
- // Returns:
- // "ERROR" if there was an error,
- // "FAILURE" if there was a failure, or
- // "SUCCESS" if there was neither
+
getResult: function() {
var results = {
tests: this.tests.length,
@@ -198,100 +202,38 @@ Test.Unit.Runner.prototype = {
{ method: 'get', parameters: this.getResult(), asynchronous: false });
}
},
+
runTests: function() {
- var test = this.tests[this.currentTest];
- if (!test) {
- // finished!
- this.postResults();
- this.logger.summary(this.summary());
- return;
- }
- if(!test.isWaiting) {
- this.logger.start(test.name);
- }
+ var test = this.tests[this.currentTest], actions;
+
+ if (!test) return this.finish();
+ if (!test.isWaiting) this.logger.start(test.name);
test.run();
if(test.isWaiting) {
this.logger.message("Waiting for " + test.timeToWait + "ms");
setTimeout(this.runTests.bind(this), test.timeToWait || 1000);
- } else {
- this.logger.finish(test.status(), test.summary());
- var actionButtons = test.actionButtons();
- if (actionButtons)
- $(this.logger.lastLogLine).down('td', 2).appendChild(actionButtons);
-
- this.currentTest++;
- // tail recursive, hopefully the browser will skip the stackframe
- this.runTests();
+ return;
}
+
+ 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());
},
summary: function() {
return '#{tests} tests, #{assertions} assertions, #{failures} failures, #{errors} errors'
.interpolate(this.getResult());
}
-}
-
-Test.Unit.Assertions = Class.create();
-Test.Unit.Assertions.prototype = {
- initialize: function() {
- this.assertions = 0;
- this.failures = 0;
- this.errors = 0;
- this.messages = [];
- this.actions = {};
- },
- summary: function() {
- return (
- this.assertions + " assertions, " +
- this.failures + " failures, " +
- this.errors + " errors" + "\n" +
- this.messages.join("\n"));
- },
- actionButtons: function() {
- if (!Object.keys(this.actions).any()) return false;
- var div = $(document.createElement("div"));
- div.addClassName("action_buttons");
-
- for (var title in this.actions) {
- var button = $(document.createElement("input"));
- button.value = title;
- button.type = "button";
- button.observe("click", this.actions[title]);
- div.appendChild(button);
- }
- return div;
- },
- 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';
- },
- isRunningFromRake: (function() {
- return window.location.port == 4711;
- })(),
+Test.Unit.Assertions = {
assert: function(expression) {
var message = arguments[1] || 'assert: got "' + Test.Unit.inspect(expression) + '"';
try { expression ? this.pass() :
@@ -497,42 +439,41 @@ Test.Unit.Assertions.prototype = {
},
assertElementMatches: function(element, expression) {
this.assertElementsMatch([element], expression);
- },
- 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;
}
-}
+};
-Test.Unit.Testcase = Class.create();
-Object.extend(Object.extend(Test.Unit.Testcase.prototype, Test.Unit.Assertions.prototype), {
+Test.Unit.Testcase = Class.create(Test.Unit.Assertions, {
initialize: function(name, test, setup, teardown) {
- Test.Unit.Assertions.prototype.initialize.bind(this)();
this.name = name;
- this.test = test || function() {};
- this.setup = setup || function() {};
- this.teardown = teardown || function() {};
- this.isWaiting = false;
- this.timeToWait = 1000;
+ this.test = test || Prototype.emptyFunction;
+ this.setup = setup || Prototype.emptyFunction;
+ this.teardown = teardown || Prototype.emptyFunction;
+ this.messages = [];
+ this.actions = {};
},
+
+ isWaiting: false,
+ timeToWait: 1000,
+ assertions: 0,
+ failures: 0,
+ errors: 0,
+ isRunningFromRake: window.location.port == 4711,
+
wait: function(time, nextPart) {
this.isWaiting = true;
this.test = nextPart;
this.timeToWait = time;
},
+
run: function(rethrow) {
try {
try {
- if (!this.isWaiting) this.setup.bind(this)();
+ if (!this.isWaiting) this.setup();
this.isWaiting = false;
- this.test.bind(this)();
+ this.test();
} finally {
if(!this.isWaiting) {
- this.teardown.bind(this)();
+ this.teardown();
}
}
}
@@ -540,5 +481,50 @@ Object.extend(Object.extend(Test.Unit.Testcase.prototype, Test.Unit.Assertions.p
if (rethrow) throw e;
this.error(e, this);
}
+ },
+
+ 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;
}
});
View
15 test/test.css
@@ -12,38 +12,39 @@ body {
margin-bottom: 2em;
}
-#logsummary {
+.logsummary {
+ margin-top: 1em;
margin-bottom: 1em;
padding: 1ex;
border: 1px solid #000;
font-weight: bold;
}
-#logtable {
+.logtable {
width:100%;
border-collapse: collapse;
border: 1px dotted #666;
}
-#logtable td, #logtable th {
+.logtable td, .logtable th {
text-align: left;
padding: 3px 8px;
border: 1px dotted #666;
}
-#logtable .passed {
+.logtable .passed {
background-color: #cfc;
}
-#logtable .failed, #logtable .error {
+.logtable .failed, .logtable .error {
background-color: #fcc;
}
-#logtable td div.action_buttons {
+.logtable td div.action_buttons {
display: inline;
}
-#logtable td div.action_buttons input {
+.logtable td div.action_buttons input {
margin: 0 5px;
font-size: 10px;
}
View
2  test/unit/ajax.html
@@ -411,7 +411,7 @@
info(message);
}
}}
- }, 'testlog');
+ });
// ]]>
</script>
</body>
View
2  test/unit/array.html
@@ -218,7 +218,7 @@
assertEnumEqual(['a', 'b', 'c', 'd'], $w(' a b\nc\t\nd\n'));
}}
- }, 'testlog');
+ });
// ]]>
</script>
</body>
View
2  test/unit/base.html
@@ -623,7 +623,7 @@
assertEqual("valueOf", new Foo().valueOf());
}}
- }, 'testlog');
+ });
// ]]>
</script>
View
2  test/unit/dom.html
@@ -1683,7 +1683,7 @@
assertEqual(Node[pair.key], pair.value);
}, this);
}}
- }, 'testlog');
+ });
function preservingBrowserDimensions(callback) {
var original = document.viewport.getDimensions();
View
2  test/unit/element_mixins.html
@@ -66,7 +66,7 @@
return (input.focus != null);
}));
}}
- }, 'testlog');
+ });
// ]]>
</script>
</body>
View
2  test/unit/enumerable.html
@@ -318,7 +318,7 @@
assertEqual(26, Fixtures.Primes.size());
assertEqual(0, [].size());
}}
- }, 'testlog');
+ });
// ]]>
</script>
</body>
View
2  test/unit/event.html
@@ -242,7 +242,7 @@
}}
- }, 'testlog');
+ });
document.observe("dom:loaded", function(event) {
eventResults.contentLoaded = {
View
2  test/unit/hash.html
@@ -226,7 +226,7 @@
assertEqual("key=bar", new Hash(new Hash(foo)).toQueryString());
}}
- }, 'testlog');
+ });
// ]]>
</script>
</body>
View
2  test/unit/number.html
@@ -61,7 +61,7 @@
assertEqual('-293', (-293).toJSON());
}}
- }, 'testlog');
+ });
// ]]>
</script>
View
2  test/unit/position.html
@@ -81,7 +81,7 @@
assert(!Position.within($('body_absolute'), 10, 20), 'outside bottom');
}}
- }, 'testlog');
+ });
// ]]>
</script>
View
2  test/unit/range.html
@@ -86,7 +86,7 @@
);
}}
- }, 'testlog');
+ });
// ]]>
</script>
</body>
View
2  test/unit/selector.html
@@ -431,7 +431,7 @@ <h1 class="title">Some title <span>here</span></h1>
assert(typeof results[1].show == 'function');
assert(typeof results[2].show == 'function');
}}
- }, 'testlog');
+ });
// ]]>
</script>
</body>
View
2  test/unit/string.html
@@ -565,7 +565,7 @@
assertIdentical(false, 'false'.evalJSON());
assertEqual('"', '"\\""'.evalJSON());
}}
- }, 'testlog');
+ });
// ]]>
</script>
</body>
View
14 test/unit/unit_tests.html
@@ -22,6 +22,7 @@
<!-- Log output -->
<div id="testlog"> </div>
+<div id="testlog_2"> </div>
<!-- Test elements follow -->
<div id="test_1" class="a bbbbbbbbbbbb cccccccccc dddd"> </div>
@@ -173,7 +174,18 @@
assertNotVisible('testcss1_span'); // hidden by parent
}}
- }, "testlog");
+ });
+
+ new Test.Unit.Runner({
+ testDummy: function() { with(this) {
+ assert(true);
+ }},
+
+ testMultipleTestRunner: function() { with(this) {
+ assertEqual('passed', $('testlog_2').down('td', 1).innerHTML);
+ }}
+
+ }, {testLog: 'testlog_2'});
// ]]>
</script>
</body>
Please sign in to comment.
Something went wrong with that request. Please try again.