1 function test(name, fn){
8 if ( expected != -1 && expected != numTests )
9 log( false, "Wrong number of tests run. " + numTests + " ran, expected " + expected );
12 var orig = document.getElementById('main').innerHTML;
15 * Resets the test setup. Useful for tests that modify the DOM.
18 document.getElementById('main').innerHTML = orig;
21 var currentModule = "";
23 // call on start of module test to prepend name to all tests
24 function module(moduleName) {
25 currentModule = moduleName;
31 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
33 function expect(asserts) {
39 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
46 * Asserts that two arrays are the same
48 function isSet(a, b, msg) {
50 if ( a && b && a.length != undefined && a.length == b.length ) {
51 for ( var i = 0; i < a.length; i++ ) {
58 log( ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) );
64 * Asserts that two objects are equivalent
66 function isObj(a, b, msg) {
83 function serialArray( a ) {
87 for ( var i = 0; i < a.length; i++ ) {
88 var str = a[i] ? a[i].nodeName : "";
90 str = str.toLowerCase();
98 return "[ " + r.join(", ") + " ]"
102 * Returns an array of elements with the given IDs, eg.
103 * @example q("main", "foo", "bar")
104 * @result [<div id="main">, <span id="foo">, <input id="bar">]
108 for ( var i = 0; i < arguments.length; i++ )
109 r.push( document.getElementById( arguments[i] ) );
114 * Asserts that a select matches the given IDs
115 * @example t("Check for something", "//[a]", ["foo", "baar"]);
116 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
121 for ( var i = 0; i < f.length; i++ )
122 s += (s && ",") + '"' + f[i].id + '"';
123 isSet(f, q.apply(q,c), a + " (" + b + ")");
127 * Checks that the first two arguments are equal, with an optional message.
128 * Prints out both expected and actual values on failure.
130 * Prefered to ok( expected == actual, message )
132 * @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) );
134 * @param Object expected
135 * @param Object actual
136 * @param String message (optional)
138 function equals(expected, actual, message) {
139 var result = expected == actual;
140 message = message || (result ? "okay" : "failed");
141 log( result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual );
144 var numTests = 0, total = 0, pass = 0, fail = 0;
146 function log(state, msg){
147 print( (state ? "PASS" : "FAIL") + " (" + (++total) + ") " +
148 (currentModule ? "[" + currentModule + "] " : "") + msg );
159 print( pass + " Passed, " + fail + " Failed" );
166 * Trigger an event on an element.
168 * @example triggerEvent( document.body, "click" );
170 * @param DOMElement elem
173 function triggerEvent( elem, type, event ) {
175 if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
176 event = document.createEvent("MouseEvents");
177 event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
178 0, 0, 0, 0, 0, false, false, false, false, 0, null);
179 elem.dispatchEvent( event );
180 } else if ( jQuery.browser.msie ) {
181 elem.fireEvent("on"+type);
187 * Add random number to url to stop IE from caching
189 * @example url("data/test.html")
190 * @result "data/test.html?10538358428943"
192 * @example url("data/test.php?foo=bar")
193 * @result "data/test.php?foo=bar&10538358345554"
195 function url(value) {
196 return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);