Added a number of additional speed gains (we now hold our own against Dojo and DOMQue...
[jquery.git] / build / test / data / testrunner.js
index 0ff0ad5..337c0dd 100644 (file)
@@ -15,7 +15,10 @@ var _config = {
 
 $(function() {
        $('#userAgent').html(navigator.userAgent);
-       runTest();      
+       if($.browser.safari)
+               $("h1").append(" - Disabled for Safari");
+       else
+               runTest();      
 });
 
 function synchronize(callback) {
@@ -63,9 +66,18 @@ function runTest() {
        });
 }
 
-function test(name, callback) {
+function test(name, callback, nowait) {
+       // safari seems to have some memory problems, so we need to slow it down
+       if($.browser.safari && !nowait) {
+               test("", function() {
+                       stop();
+                       setTimeout(start, 250);
+               }, true);
+       }
+
        if(_config.currentModule)
                name = _config.currentModule + " module: " + name;
+               
        synchronize(function() {
                _config.Test = [];
                try {
@@ -82,6 +94,9 @@ function test(name, callback) {
        synchronize(function() {
                reset();
                
+               // don't output pause tests
+               if(nowait) return;
+               
                if(_config.expected && _config.expected != _config.Test.length) {
                        _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
                }
@@ -157,17 +172,33 @@ function ok(a, msg) {
 function isSet(a, b, msg) {
        var ret = true;
        if ( a && b && a.length == b.length ) {
-               for ( var i in a )
+               for ( var i = 0; i < a.length; i++ )
                        if ( a[i] != b[i] )
                                ret = false;
        } else
                ret = false;
        if ( !ret )
-               _config.Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
+               _config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] );
        else 
                _config.Test.push( [ ret, msg ] );
 }
 
+function serialArray( a ) {
+       var r = [];
+       for ( var i = 0; i < a.length; i++ ) {
+               var str = a[i].nodeName;
+               if ( str ) {
+                       str = str.toLowerCase();
+                       if ( a[i].id )
+                               str += "#" + a[i].id;
+               } else
+                       str = a[i];
+               r.push( str );
+       }
+
+       return "[ " + r.join(", ") + " ]"
+}
+
 /**
  * Returns an array of elements with the given IDs, eg.
  * @example q("main", "foo", "bar")
@@ -186,7 +217,7 @@ function q() {
  * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
  */
 function t(a,b,c) {
-       var f = jQuery.find(b);
+       var f = jQuery(b);
        var s = "";
        for ( var i = 0; i < f.length; i++ )
                s += (s && ",") + '"' + f[i].id + '"';
@@ -204,4 +235,41 @@ function t(a,b,c) {
  */
 function url(value) {
        return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
-}
\ No newline at end of file
+}
+
+/**
+ * Checks that the first two arguments are equal, with an optional message.
+ * Prints out both expected and actual values on failure.
+ *
+ * Prefered to ok( expected == actual, message )
+ *
+ * @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) );
+ *
+ * @param Object expected
+ * @param Object actual
+ * @param String message (optional)
+ */
+function equals(expected, actual, message) {
+       var result = expected == actual;
+       message = message || (result ? "okay" : "failed");
+       _config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] );
+}
+
+/**
+ * Trigger an event on an element.
+ *
+ * @example triggerEvent( document.body, "click" );
+ *
+ * @param DOMElement elem
+ * @param String type
+ */
+function triggerEvent( elem, type, event ) {
+       if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
+               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 ( jQuery.browser.msie ) {
+               elem.fireEvent("on"+type);
+       }
+}