|
/* |
|
* QUnit - A JavaScript Unit Testing Framework |
|
* |
|
* http://docs.jquery.com/QUnit |
|
* |
|
* Copyright (c) 2009 John Resig, Jörn Zaefferer |
|
* Dual licensed under the MIT (MIT-LICENSE.txt) |
|
* and GPL (GPL-LICENSE.txt) licenses. |
|
*/ |
|
|
|
(function(window) { |
|
|
|
var QUnit = { |
|
|
|
// Initialize the configuration options |
|
init: function() { |
|
config = { |
|
stats: { all: 0, bad: 0 }, |
|
moduleStats: { all: 0, bad: 0 }, |
|
started: +new Date, |
|
blocking: false, |
|
autorun: false, |
|
assertions: [], |
|
filters: [], |
|
queue: [] |
|
}; |
|
|
|
var tests = id("qunit-tests"), |
|
banner = id("qunit-banner"), |
|
result = id("qunit-testresult"); |
|
|
|
if ( tests ) { |
|
tests.innerHTML = ""; |
|
} |
|
|
|
if ( banner ) { |
|
banner.className = ""; |
|
} |
|
|
|
if ( result ) { |
|
result.parentNode.removeChild( result ); |
|
} |
|
}, |
|
|
|
// call on start of module test to prepend name to all tests |
|
module: function(name, testEnvironment) { |
|
config.currentModule = name; |
|
|
|
synchronize(function() { |
|
if ( config.currentModule ) { |
|
QUnit.moduleDone( config.currentModule, config.moduleStats.bad, config.moduleStats.all ); |
|
} |
|
|
|
config.currentModule = name; |
|
config.moduleTestEnvironment = testEnvironment; |
|
config.moduleStats = { all: 0, bad: 0 }; |
|
|
|
QUnit.moduleStart( name, testEnvironment ); |
|
}); |
|
}, |
|
|
|
asyncTest: function(testName, expected, callback) { |
|
if ( arguments.length === 2 ) { |
|
callback = expected; |
|
expected = 0; |
|
} |
|
|
|
QUnit.test(testName, expected, callback, true); |
|
}, |
|
|
|
test: function(testName, expected, callback, async) { |
|
var name = testName, testEnvironment, testEnvironmentArg; |
|
|
|
if ( arguments.length === 2 ) { |
|
callback = expected; |
|
expected = null; |
|
} |
|
// is 2nd argument a testEnvironment? |
|
if ( expected && typeof expected === 'object') { |
|
testEnvironmentArg = expected; |
|
expected = null; |
|
} |
|
|
|
if ( config.currentModule ) { |
|
name = config.currentModule + " module: " + name; |
|
} |
|
|
|
if ( !validTest(name) ) { |
|
return; |
|
} |
|
|
|
synchronize(function() { |
|
QUnit.testStart( testName ); |
|
|
|
testEnvironment = extend({ |
|
setup: function() {}, |
|
teardown: function() {} |
|
}, config.moduleTestEnvironment); |
|
if (testEnvironmentArg) { |
|
extend(testEnvironment,testEnvironmentArg); |
|
} |
|
|
|
// allow utility functions to access the current test environment |
|
QUnit.current_testEnvironment = testEnvironment; |
|
|
|
config.assertions = []; |
|
config.expected = expected; |
|
|
|
try { |
|
if ( !config.pollution ) { |
|
saveGlobal(); |
|
} |
|
|
|
testEnvironment.setup.call(testEnvironment); |
|
} catch(e) { |
|
QUnit.ok( false, "Setup failed on " + name + ": " + e.message ); |
|
} |
|
|
|
if ( async ) { |
|
QUnit.stop(); |
|
} |
|
|
|
try { |
|
callback.call(testEnvironment); |
|
} catch(e) { |
|
fail("Test " + name + " died, exception and test follows", e, callback); |
|
QUnit.ok( false, "Died on test #" + (config.assertions.length + 1) + ": " + e.message ); |
|
// else next test will carry the responsibility |
|
saveGlobal(); |
|
|
|
// Restart the tests if they're blocking |
|
if ( config.blocking ) { |
|
start(); |
|
} |
|
} |
|
}); |
|
|
|
synchronize(function() { |
|
try { |
|
checkPollution(); |
|
testEnvironment.teardown.call(testEnvironment); |
|
} catch(e) { |
|
QUnit.ok( false, "Teardown failed on " + name + ": " + e.message ); |
|
} |
|
|
|
try { |
|
QUnit.reset(); |
|
} catch(e) { |
|
fail("reset() failed, following Test " + name + ", exception and reset fn follows", e, reset); |
|
} |
|
|
|
if ( config.expected && config.expected != config.assertions.length ) { |
|
QUnit.ok( false, "Expected " + config.expected + " assertions, but " + config.assertions.length + " were run" ); |
|
} |
|
|
|
var good = 0, bad = 0, |
|
tests = id("qunit-tests"); |
|
|
|
config.stats.all += config.assertions.length; |
|
config.moduleStats.all += config.assertions.length; |
|
|
|
if ( tests ) { |
|
var ol = document.createElement("ol"); |
|
ol.style.display = "none"; |
|
|
|
for ( var i = 0; i < config.assertions.length; i++ ) { |
|
var assertion = config.assertions[i]; |
|
|
|
var li = document.createElement("li"); |
|
li.className = assertion.result ? "pass" : "fail"; |
|
li.appendChild(document.createTextNode(assertion.message || "(no message)")); |
|
ol.appendChild( li ); |
|
|
|
if ( assertion.result ) { |
|
good++; |
|
} else { |
|
bad++; |
|
config.stats.bad++; |
|
config.moduleStats.bad++; |
|
} |
|
} |
|
|
|
var b = document.createElement("strong"); |
|
b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + config.assertions.length + ")</b>"; |
|
|
|
addEvent(b, "click", function() { |
|
var next = b.nextSibling, display = next.style.display; |
|
next.style.display = display === "none" ? "block" : "none"; |
|
}); |
|
|
|
addEvent(b, "dblclick", function(e) { |
|
var target = e && e.target ? e.target : window.event.srcElement; |
|
if ( target.nodeName.toLowerCase() === "strong" ) { |
|
var text = "", node = target.firstChild; |
|
|
|
while ( node.nodeType === 3 ) { |
|
text += node.nodeValue; |
|
node = node.nextSibling; |
|
} |
|
|
|
text = text.replace(/(^\s*|\s*$)/g, ""); |
|
|
|
if ( window.location ) { |
|
window.location.href = window.location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent(text); |
|
} |
|
} |
|
}); |
|
|
|
var li = document.createElement("li"); |
|
li.className = bad ? "fail" : "pass"; |
|
li.appendChild( b ); |
|
li.appendChild( ol ); |
|
tests.appendChild( li ); |
|
|
|
if ( bad ) { |
|
var toolbar = id("qunit-testrunner-toolbar"); |
|
if ( toolbar ) { |
|
toolbar.style.display = "block"; |
|
id("qunit-filter-pass").disabled = null; |
|
id("qunit-filter-missing").disabled = null; |
|
} |
|
} |
|
|
|
} else { |
|
for ( var i = 0; i < config.assertions.length; i++ ) { |
|
if ( !config.assertions[i].result ) { |
|
bad++; |
|
config.stats.bad++; |
|
config.moduleStats.bad++; |
|
} |
|
} |
|
} |
|
|
|
QUnit.testDone( testName, bad, config.assertions.length ); |
|
|
|
if ( !window.setTimeout && !config.queue.length ) { |
|
done(); |
|
} |
|
}); |
|
|
|
if ( window.setTimeout && !config.doneTimer ) { |
|
config.doneTimer = window.setTimeout(function(){ |
|
if ( !config.queue.length ) { |
|
done(); |
|
} else { |
|
synchronize( done ); |
|
} |
|
}, 13); |
|
} |
|
}, |
|
|
|
/** |
|
* Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through. |
|
*/ |
|
expect: function(asserts) { |
|
config.expected = asserts; |
|
}, |
|
|
|
/** |
|
* Asserts true. |
|
* @example ok( "asdfasdf".length > 5, "There must be at least 5 chars" ); |
|
*/ |
|
ok: function(a, msg) { |
|
QUnit.log(a, msg); |
|
|
|
config.assertions.push({ |
|
result: !!a, |
|
message: msg |
|
}); |
|
}, |
|
|
|
/** |
|
* Checks that the first two arguments are equal, with an optional message. |
|
* Prints out both actual and expected values. |
|
* |
|
* Prefered to ok( actual == expected, message ) |
|
* |
|
* @example equal( format("Received {0} bytes.", 2), "Received 2 bytes." ); |
|
* |
|
* @param Object actual |
|
* @param Object expected |
|
* @param String message (optional) |
|
*/ |
|
equal: function(actual, expected, message) { |
|
push(expected == actual, actual, expected, message); |
|
}, |
|
|
|
notEqual: function(actual, expected, message) { |
|
push(expected != actual, actual, expected, message); |
|
}, |
|
|
|
deepEqual: function(a, b, message) { |
|
push(QUnit.equiv(a, b), a, b, message); |
|
}, |
|
|
|
notDeepEqual: function(a, b, message) { |
|
push(!QUnit.equiv(a, b), a, b, message); |
|
}, |
|
|
|
strictEqual: function(actual, expected, message) { |
|
push(expected === actual, actual, expected, message); |
|
}, |
|
|
|
notStrictEqual: function(actual, expected, message) { |
|
push(expected !== actual, actual, expected, message); |
|
}, |
|
|
|
start: function() { |
|
// A slight delay, to avoid any current callbacks |
|
if ( window.setTimeout ) { |
|
window.setTimeout(function() { |
|
if ( config.timeout ) { |
|
clearTimeout(config.timeout); |
|
} |
|
|
|
config.blocking = false; |
|
process(); |
|
}, 13); |
|
} else { |
|
config.blocking = false; |
|
process(); |
|
} |
|
}, |
|
|
|
stop: function(timeout) { |
|
config.blocking = true; |
|
|
|
if ( timeout && window.setTimeout ) { |
|
config.timeout = window.setTimeout(function() { |
|
QUnit.ok( false, "Test timed out" ); |
|
QUnit.start(); |
|
}, timeout); |
|
} |
|
}, |
|
|
|
/** |
|
* Resets the test setup. Useful for tests that modify the DOM. |
|
*/ |
|
reset: function() { |
|
if ( window.jQuery ) { |
|
jQuery("#main").html( config.fixture ); |
|
jQuery.event.global = {}; |
|
jQuery.ajaxSettings = extend({}, config.ajaxSettings); |
|
} |
|
}, |
|
|
|
/** |
|
* Trigger an event on an element. |
|
* |
|
* @example triggerEvent( document.body, "click" ); |
|
* |
|
* @param DOMElement elem |
|
* @param String type |
|
*/ |
|
triggerEvent: function( elem, type, event ) { |
|
if ( document.createEvent ) { |
|
event = document.createEvent("MouseEvents"); |
|
event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView, |
|
0, 0, 0, 0, 0, false, false, false, false, 0, null); |
|
elem.dispatchEvent( event ); |
|
|
|
} else if ( elem.fireEvent ) { |
|
elem.fireEvent("on"+type); |
|
} |
|
}, |
|
|
|
// Safe object type checking |
|
is: function( type, obj ) { |
|
return Object.prototype.toString.call( obj ) === "[object "+ type +"]"; |
|
}, |
|
|
|
// Logging callbacks |
|
done: function(failures, total) {}, |
|
log: function(result, message) {}, |
|
testStart: function(name) {}, |
|
testDone: function(name, failures, total) {}, |
|
moduleStart: function(name, testEnvironment) {}, |
|
moduleDone: function(name, failures, total) {} |
|
}; |
|
|
|
// Backwards compatibility, deprecated |
|
QUnit.equals = QUnit.equal; |
|
QUnit.same = QUnit.deepEqual; |
|
|
|
// Maintain internal state |
|