Added all the tests for isFunction, fixed bug #1026.
authorJohn Resig <jeresig@gmail.com>
Fri, 16 Mar 2007 00:00:46 +0000 (00:00 +0000)
committerJohn Resig <jeresig@gmail.com>
Fri, 16 Mar 2007 00:00:46 +0000 (00:00 +0000)
src/jquery/coreTest.js
src/jquery/jquery.js

index c20d406..aa2932b 100644 (file)
@@ -19,6 +19,82 @@ test("$()", function() {
        $('<p>\r\n</p>');\r
 });\r
 \r
+test("isFunction", function() {\r
+       expect(20);\r
+\r
+       // Make sure that false values return false\r
+       ok( !jQuery.isFunction(), "No Value" );\r
+       ok( !jQuery.isFunction( null ), "null Value" );\r
+       ok( !jQuery.isFunction( undefined ), "undefined Value" );\r
+       ok( !jQuery.isFunction( "" ), "Empty String Value" );\r
+       ok( !jQuery.isFunction( 0 ), "0 Value" );\r
+\r
+       // Check built-ins\r
+       // Safari uses "(Internal Function)"\r
+       ok( jQuery.isFunction(String), "String Function" );\r
+       ok( jQuery.isFunction(Array), "Array Function" );\r
+       ok( jQuery.isFunction(Object), "Object Function" );\r
+       ok( jQuery.isFunction(Function), "Function Function" );\r
+\r
+       // When stringified, this could be misinterpreted\r
+       var mystr = "function";\r
+       ok( !jQuery.isFunction(mystr), "Function String" );\r
+\r
+       // When stringified, this could be misinterpreted\r
+       var myarr = [ "function" ];\r
+       ok( !jQuery.isFunction(myarr), "Function Array" );\r
+\r
+       // When stringified, this could be misinterpreted\r
+       var myfunction = { "function": "test" };\r
+       ok( !jQuery.isFunction(myfunction), "Function Object" );\r
+\r
+       // Make sure normal functions still work\r
+       var fn = function(){};\r
+       ok( jQuery.isFunction(fn), "Normal Function" );\r
+\r
+       var obj = document.createElement("object");\r
+\r
+       // Firefox says this is a function\r
+       ok( !jQuery.isFunction(obj), "Object Element" );\r
+\r
+       // IE says this is an object\r
+       ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );\r
+\r
+       var nodes = document.body.childNodes;\r
+\r
+       // Safari says this is a function\r
+       ok( !jQuery.isFunction(nodes), "childNodes Property" );\r
+\r
+       var first = document.body.firstChild;\r
+       \r
+       // Normal elements are reported ok everywhere\r
+       ok( !jQuery.isFunction(first), "A normal DOM Element" );\r
+\r
+       var input = document.createElement("input");\r
+       input.type = "text";\r
+       document.body.appendChild( input );\r
+\r
+       // IE says this is an object\r
+       ok( jQuery.isFunction(input.focus), "A default function property" );\r
+\r
+       document.body.removeChild( input );\r
+\r
+       // Recursive function calls have lengths and array-like properties\r
+       function callme(callback){\r
+               function fn(response){\r
+                       callback(response);\r
+               }\r
+\r
+               ok( jQuery.isFunction(fn), "Recursive Function Call" );\r
+\r
+               fn({ some: "data" });\r
+       };\r
+\r
+       callme(function(){\r
+               callme(function(){});\r
+       });\r
+});\r
+\r
 test("length", function() {\r
        ok( $("div").length == 2, "Get Number of Elements Found" );\r
 });\r
index cc89fff..e81b82b 100644 (file)
@@ -1255,7 +1255,7 @@ jQuery.extend({
        // is the only cross-browser way to do this. --John
        isFunction: function( fn ) {
                return !!fn && typeof fn != "string" && !fn.nodeName && 
-                       typeof fn[0] == "undefined" && /function/i.test( fn + "" );
+                       fn.constructor != Array && /function/i.test( fn + "" );
        },
        
        // check if an element is in a XML document