From 97ea47492fe13ddb1108045c244d5af6570ace92 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=B6rn=20Zaefferer?= Date: Mon, 4 Sep 2006 04:51:57 +0000 Subject: [PATCH] Improved test suite to reset fixture after each test, added selects to test ':selected'; Added docs for index(obj); Modified is(expr) to return false for an invalid expression, changed docs, too; Added lots of testcases; Changed filename of testfiles: adding leading zeros --- build/test/index.html | 36 +++++--- build/test/js/test.js | 132 ++++++++++++++++++---------- build/test/test.js | 11 +-- src/jquery/jquery.js | 194 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 305 insertions(+), 68 deletions(-) diff --git a/build/test/index.html b/build/test/index.html index dad650224..7d10fc77f 100644 --- a/build/test/index.html +++ b/build/test/index.html @@ -4,7 +4,7 @@ @@ -24,15 +24,12 @@

-

Everything inside the red border is inside a div with - id="foo".

-

This is a normal link: - Yahoo

-

This link has class="blog": - Simon Willison's Weblog

+

Everything inside the red border is inside a div with id="foo".

+

This is a normal link: Yahoo

+

This link has class="blog": Simon Willison's Weblog

-
-

Try them out:

+ +

Try them out:

    @@ -45,7 +42,26 @@ - + + + + +
    diff --git a/build/test/js/test.js b/build/test/js/test.js index 44a405fdb..82469f720 100644 --- a/build/test/js/test.js +++ b/build/test/js/test.js @@ -1,53 +1,97 @@ -function runTests(files) { - runTest( files, 0 ); +var queue = []; +var blocking = false; + +function reset() { + synchronize(function() { + blocking = true; + $.get('index.html', function(content) { + var div = $(document.createElement('div')).html(content) + // search for main div + .find('[@id=main]').html(); + $('#main').html(div); + blocking = false; + process(); + }); + }); } -function runTest( files, num ) { - $.get(files[num],function(js){ - js = js.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&"); +function synchronize(callback) { + queue[queue.length] = callback; + if(!blocking) { + process(); + } +} - try { - eval(js); - } catch(e) { - Test.push( [ false, "Died on test #" + Test.length + ": " + e ] ); - } +function process() { + while(queue.length && !blocking) { + var call = queue[0]; + queue = queue.slice(1); + call(); + } +} - var good = 0, bad = 0; - var ol = document.createElement("ol"); +function runTests(files) { + var startTime = new Date(); + for( var i=0; i < files.length; i++) { + runTest( files, i ); + reset(); + } + synchronize(function() { + var runTime = new Date() - startTime; + $('body').append('
    Tests completed in ' + runTime + ' milliseconds.'); + }); +} - var li = "", state = "pass"; - for ( var i = 0; i < Test.length; i++ ) { +function runTest( files, num ) { + synchronize(function() { + blocking = true; + $.get(files[num],function(js){ + js = js.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&"); + + try { + eval(js); + } catch(e) { + Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] ); + } + + var good = 0, bad = 0; + var ol = document.createElement("ol"); + + var li = "", state = "pass"; + for ( var i = 0; i < Test.length; i++ ) { + var li = document.createElement("li"); + li.className = Test[i][0] ? "pass" : "fail"; + li.innerHTML = Test[i][1]; + ol.appendChild( li ); + + if ( !Test[i][0] ) { + state = "fail"; + bad++; + } else good++; + } + var li = document.createElement("li"); - li.className = Test[i][0] ? "pass" : "fail"; - li.innerHTML = Test[i][1]; - ol.appendChild( li ); - - if ( !Test[i][0] ) { - state = "fail"; - bad++; - } else good++; - } - - var li = document.createElement("li"); - li.className = state; - - var b = document.createElement("b"); - b.innerHTML = files[num] + " (" + bad + ", " + good + ", " + Test.length + ")"; - b.onclick = function(){ - var n = this.nextSibling; - if ( jQuery.css( n, "display" ) == "none" ) - n.style.display = "block"; - else - n.style.display = "none"; - }; - li.appendChild( b ); - - li.appendChild( ol ); - - document.getElementById("tests").appendChild( li ); - - Test = []; - if ( ++num < files.length ) runTest( files, num ); + li.className = state; + + var b = document.createElement("b"); + b.innerHTML = files[num] + " (" + bad + ", " + good + ", " + Test.length + ")"; + b.onclick = function(){ + var n = this.nextSibling; + if ( jQuery.css( n, "display" ) == "none" ) + n.style.display = "block"; + else + n.style.display = "none"; + }; + li.appendChild( b ); + + li.appendChild( ol ); + + document.getElementById("tests").appendChild( li ); + + Test = []; + blocking = false; + process(); + }); }); } diff --git a/build/test/test.js b/build/test/test.js index 6aeeb6b97..96773ff7f 100644 --- a/build/test/test.js +++ b/build/test/test.js @@ -13,11 +13,12 @@ var count = 1; for ( var i = 0; i < jq.length; i++ ) { if ( jq[i].tests.length > 0 ) { var name = count + "-" + jq[i].name; - - var myFile = testFile - .replace( /{TITLE}/g, jq[i].name ) - .replace( /{NUM}/g, jq[i].tests.length ) - .replace( /{TESTS}/g, jq[i].tests.join("\n") ); + if(count < 100) { + name = "0" + name; + } + if(count < 10) { + name = "0" + name; + } var fileName = "tests/" + name + ".js"; diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index c025c8168..65732b8f7 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -325,6 +325,38 @@ jQuery.fn = jQuery.prototype = { return jQuery.each( this, fn, args ); }, + /** + * Searches every matched element for the object and returns + * the index of the element, if found, starting with zero. + * Returns -1 if the object wasn't found. + * + * @example $("*").index(document.getElementById('foobar')) + * @before
    + * @result 0 + * + * @example $("*").index(document.getElementById('foo')) + * @before
    + * @result 2 + * + * @example $("*").index(document.getElementById('bar')) + * @before
    + * @result -1 + * + * @test ok( $([window, document]).index(window) == 0, "Check for index of elements" ); + * ok( $([window, document]).index(document) == 1, "Check for index of elements" ); + * var inputElements = $('#radio1,#radio2,#check1,#check2'); + * ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" ); + * ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" ); + * ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" ); + * ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" ); + * ok( inputElements.index(window) == -1, "Check for not found index" ); + * ok( inputElements.index(document) == -1, "Check for not found index" ); + * + * @name index + * @type Number + * @param Object obj Object to search for + * @cat Core + */ index: function( obj ) { var pos = -1; this.each(function(i){ @@ -342,6 +374,11 @@ jQuery.fn = jQuery.prototype = { * @before * @result test.jpg * + * @test ok( $('#text1').attr('value') == "Test", 'Check for value attribute' ); + * @test ok( $('#text1').attr('type') == "text", 'Check for type attribute' ); + * @test ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' ); + * @test ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' ); + * * @name attr * @type Object * @param String name The name of the property to access. @@ -424,6 +461,8 @@ jQuery.fn = jQuery.prototype = { * @before

    Test Paragraph.

    * @result red * + * @test ok( $('#foo').css("display") == 'block', 'Check for css property "display"'); + * * @name css * @type Object * @param String name The name of the property to access. @@ -439,6 +478,12 @@ jQuery.fn = jQuery.prototype = { * @before

    Test Paragraph.

    * @result

    Test Paragraph.

    * + * @test ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible'); + * $('#foo').css({display: 'none'}); + * ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden'); + * $('#foo').css({display: 'block'}); + * ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible'); + * * @name css * @type jQuery * @param Hash prop A set of key/value pairs to set as style properties. @@ -452,6 +497,12 @@ jQuery.fn = jQuery.prototype = { * @before

    Test Paragraph.

    * @result

    Test Paragraph.

    * + * @test ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible'); + * $('#foo').css('display', 'none'); + * ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden'); + * $('#foo').css('display', 'block'); + * ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible'); + * * @name css * @type jQuery * @param String key The name of the property to set. @@ -471,6 +522,9 @@ jQuery.fn = jQuery.prototype = { * @before

    Test Paragraph.

    * @result Test Paragraph. * + * @test var expected = "This link has class=\"blog\": Simon Willison's Weblog"; + * ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' ); + * * @name text * @type String * @cat DOM @@ -503,6 +557,11 @@ jQuery.fn = jQuery.prototype = { * @before

    Test Paragraph.

    * @result

    Test Paragraph.

    * + * @test var defaultText = 'Try them out:' + * var result = $('#first').wrap('
    xxyy
    ').text() + * ok( 'xx' + defaultText + 'yy' == result, 'Check for wrapping' ); + * ok( $('#first').parent().parent().is('.red'), 'Check if wrapper div has class "red"' ); + * * @name wrap * @type jQuery * @param String html A string of HTML, that will be created on the fly and wrapped around the target. @@ -559,6 +618,10 @@ jQuery.fn = jQuery.prototype = { * @before

    I would like to say:

    * @result

    I would like to say: Hello

    * + * @test var defaultText = 'Try them out:' + * var result = $('#first').append('buga'); + * ok( result.text() == defaultText + 'buga', 'Check if text appending works' ); + * * @name append * @type jQuery * @param String html A string of HTML, that will be created on the fly and appended to the target. @@ -574,6 +637,10 @@ jQuery.fn = jQuery.prototype = { * @before

    I would like to say:

    Hello * @result

    I would like to say: Hello

    * + * @test var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:"; + * $('#sap').append(document.getElementById('first')); + * ok( expected == $('#sap').text(), "Check for appending of element" ); + * * @name append * @type jQuery * @param Element elem A DOM element that will be appended. @@ -589,6 +656,10 @@ jQuery.fn = jQuery.prototype = { * @before

    I would like to say:

    Hello * @result

    I would like to say: Hello

    * + * @test var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; + * $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]); + * ok( expected == $('#sap').text(), "Check for appending of array of elements" ); + * * @name append * @type jQuery * @param Array elems An array of elements, all of which will be appended. @@ -610,6 +681,10 @@ jQuery.fn = jQuery.prototype = { * @before

    I would like to say:

    * @result

    HelloI would like to say:

    * + * @test var defaultText = 'Try them out:' + * var result = $('#first').prepend('buga'); + * ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' ); + * * @name prepend * @type jQuery * @param String html A string of HTML, that will be created on the fly and appended to the target. @@ -617,13 +692,17 @@ jQuery.fn = jQuery.prototype = { */ /** - * Append an element to the inside of all matched elements. + * Prepend an element to the inside of all matched elements. * This operation is the best way to insert an element inside, at the * beginning, of all the matched element. * * @example $("p").prepend( $("#foo")[0] ); * @before

    I would like to say:

    Hello * @result

    HelloI would like to say:

    + * + * @test var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog"; + * $('#sap').prepend(document.getElementById('first')); + * ok( expected == $('#sap').text(), "Check for prepending of element" ); * * @name prepend * @type jQuery @@ -632,7 +711,7 @@ jQuery.fn = jQuery.prototype = { */ /** - * Append any number of elements to the inside of all matched elements. + * Prepend any number of elements to the inside of all matched elements. * This operation is the best way to insert a set of elements inside, at the * beginning, of all the matched element. * @@ -640,6 +719,10 @@ jQuery.fn = jQuery.prototype = { * @before

    I would like to say:

    Hello * @result

    HelloI would like to say:

    * + * @test var expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; + * $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]); + * ok( expected == $('#sap').text(), "Check for prepending of array of elements" ); + * * @name prepend * @type jQuery * @param Array elems An array of elements, all of which will be appended. @@ -659,6 +742,10 @@ jQuery.fn = jQuery.prototype = { * @before

    I would like to say:

    * @result Hello

    I would like to say:

    * + * @test var expected = 'This is a normal link: bugaYahoo'; + * $('#yahoo').before('buga'); + * ok( expected == $('#en').text(), 'Insert String before' ); + * * @name before * @type jQuery * @param String html A string of HTML, that will be created on the fly and appended to the target. @@ -672,6 +759,10 @@ jQuery.fn = jQuery.prototype = { * @before

    I would like to say:

    Hello * @result Hello

    I would like to say:

    * + * @test var expected = "This is a normal link: Try them out:Yahoo"; + * $('#yahoo').before(document.getElementById('first')); + * ok( expected == $('#en').text(), "Insert element before" ); + * * @name before * @type jQuery * @param Element elem A DOM element that will be appended. @@ -685,6 +776,10 @@ jQuery.fn = jQuery.prototype = { * @before

    I would like to say:

    Hello * @result Hello

    I would like to say:

    * + * @test var expected = "This is a normal link: Try them out:diveintomarkYahoo"; + * $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]); + * ok( expected == $('#en').text(), "Insert array of elements before" ); + * * @name before * @type jQuery * @param Array elems An array of elements, all of which will be appended. @@ -704,6 +799,10 @@ jQuery.fn = jQuery.prototype = { * @before

    I would like to say:

    * @result

    I would like to say:

    Hello * + * @test var expected = 'This is a normal link: Yahoobuga'; + * $('#yahoo').after('buga'); + * ok( expected == $('#en').text(), 'Insert String after' ); + * * @name after * @type jQuery * @param String html A string of HTML, that will be created on the fly and appended to the target. @@ -717,6 +816,10 @@ jQuery.fn = jQuery.prototype = { * @before Hello

    I would like to say:

    * @result

    I would like to say:

    Hello * + * @test var expected = "This is a normal link: YahooTry them out:"; + * $('#yahoo').after(document.getElementById('first')); + * ok( expected == $('#en').text(), "Insert element after" ); + * * @name after * @type jQuery * @param Element elem A DOM element that will be appended. @@ -730,6 +833,10 @@ jQuery.fn = jQuery.prototype = { * @before Hello

    I would like to say:

    * @result

    I would like to say:

    Hello * + * @test var expected = "This is a normal link: YahooTry them out:diveintomark"; + * $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]); + * ok( expected == $('#en').text(), "Insert array of elements after" ); + * * @name after * @type jQuery * @param Array elems An array of elements, all of which will be appended. @@ -750,6 +857,8 @@ jQuery.fn = jQuery.prototype = { * @before

    Hello, how are you?

    * @result $("p").find("span").end() == [

    ...

    ] * + * @test ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' ); + * * @name end * @type jQuery * @cat DOM/Traversing @@ -770,6 +879,8 @@ jQuery.fn = jQuery.prototype = { * @before

    Hello, how are you?

    * @result $("p").find("span") == [ Hello ] * + * @test ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' ); + * * @name find * @type jQuery * @param String expr An expression to search with. @@ -792,6 +903,11 @@ jQuery.fn = jQuery.prototype = { * @before Hello

    , how are you?

    * @result Hello

    Hello, how are you?

    * + * @test ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' ); + * var clone = $('#yahoo').clone(); + * ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' ); + * ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' ); + * * @name clone * @type jQuery * @cat DOM/Manipulation @@ -938,17 +1054,55 @@ jQuery.fn = jQuery.prototype = { }, /** - * A wrapper function for each() to be used by append and prepend. - * Handles cases where you're trying to modify the inner contents of - * a table, when you actually need to work with the tbody. - * - * @member jQuery - * @param {String} expr The expression with which to filter + * Checks the current selection against an expression and returns true, + * if the selection fits the given expression. Does return false, if the + * selection does not fit or the expression is not valid. + * + * @example $("input[@type='checkbox']").parent().is("form") + * @before
    + * @result true + * @desc Returns true, because the parent of the input is a form element + * + * @example $("input[@type='checkbox']").parent().is("form") + * @before

    + * @result false + * @desc Returns false, because the parent of the input is a p element + * + * @example $("form").is(null) + * @before
    + * @result false + * @desc An invalid expression always returns false. + * + * @test ok( $('#form').is('form'), 'Check for element: A form must be a form' ); + * @test ok( !$('#form').is('div'), 'Check for element: A form is not a div' ); + * @test ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' ); + * @test ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' ); + * @test ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' ); + * @test ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' ); + * @test ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' ); + * @test ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' ); + * @test ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' ); + * @test ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' ); + * @test ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' ); + * @test ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' ); + * @test ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' ); + * @test ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' ); + * @test ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' ); + * @test ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' ); + * @test ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' ); + * @test ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' ); + * @test ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' ); + * @test ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' ); + * @test ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' ); + * @test ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' ); + * + * @name is * @type Boolean + * @param String expr The expression with which to filter * @cat DOM/Traversing */ is: function(expr) { - return expr ? jQuery.filter(expr,this).r.length > 0 : this.length > 0; + return expr ? jQuery.filter(expr,this).r.length > 0 : false; }, /** @@ -1029,6 +1183,19 @@ jQuery.fn = jQuery.prototype = { /** * Extend one object with another, returning the original, * modified, object. This is a great utility for simple inheritance. + * + * @example var settings = { validate: false, limit: 5, name: "foo" }; + * var options = { validate: true, name: "bar" }; + * jQuery.extend(settings, options); + * @result settings == { validate: true, limit: 5, name: "bar" } + * + * @test var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }; + * var options = { xnumber2: 1, xstring2: "x", xxx: "newstring" }; + * var optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" }; + * var merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" }; + * jQuery.extend(settings, options); + * isSet( settings, merged, "Check if extended: settings must be extended" ); + * isSet ( options, optionsCopy, "Check if not modified: options must not be modified" ); * * @name $.extend * @param Object obj The object to extend @@ -1380,6 +1547,7 @@ jQuery.extend({ * @test t( "Enabled UI Element", "input:enabled", ["text1","radio1","radio2","check1","check2","hidden1","hidden2"] ); * @test t( "Disabled UI Element", "input:disabled", ["text2"] ); * @test t( "Checked UI Element", "input:checked", ["radio2","check1"] ); + * @test t( "Selected Option Element", "option:selected", ["option1a","option2d","option3b","option3c"] ); * @test t( "Text Contains", "a:contains('Google')", ["google","groups"] ); * @test t( "Text Contains", "a:contains('Google Groups')", ["groups"] ); * @test t( "Element Preceded By", "p ~ div", ["foo"] ); @@ -2289,6 +2457,9 @@ jQuery.macros = { * @before * @result "some text" * + * @test ok( $("#text1").val() == "Test", "Check for value of input element" ); + * @test ok( !$("#text1").val() == "", "Check for value of input element" ); + * * @name val * @type String * @cat DOM/Attributes @@ -2301,6 +2472,11 @@ jQuery.macros = { * @before * @result * + * @test document.getElementById('text1').value = "bla"; + * ok( $("#text1").val() == "bla", "Check for modified value of input element" ); + * $("#text1").val('test'); + * ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" ); + * * @name val * @type jQuery * @param String val Set the property to the specified value. -- 2.39.5