From 59ec78e6020cc963c1f95cb96a28eaaf20e37b3e Mon Sep 17 00:00:00 2001 From: Alexander Lisianoi Date: Sat, 5 Mar 2016 10:48:52 +0100 Subject: [PATCH] Tests: Restrict "p > * > *" selection in selector.js to #qunit-fixture Add `match` and `QUnit.assert.selectInFixture` functions that mimic `QUnit.assert.t`. Ref gh-2880 Closes gh-2973 --- test/data/testinit.js | 40 ++++++++++++++++++++++++++++++++-------- test/unit/selector.js | 2 +- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/test/data/testinit.js b/test/data/testinit.js index ca8f433d7..073dc349f 100644 --- a/test/data/testinit.js +++ b/test/data/testinit.js @@ -32,14 +32,14 @@ this.q = function() { /** * Asserts that a select matches the given IDs - * @param {String} a - Assertion name - * @param {String} b - Sizzle selector - * @param {String} c - Array of ids to construct what is expected - * @example t("Check for something", "//[a]", ["foo", "bar"]); - * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'bar' + * @param {String} message - Assertion name + * @param {String} selector - Sizzle selector + * @param {String} expectedIds - Array of ids to construct what is expected + * @param {(String|Node)=document} context - Selector context + * @example match("Check for something", "p", ["foo", "bar"]); */ -QUnit.assert.t = function( a, b, c ) { - var f = jQuery( b ).get(), +function match( message, selector, expectedIds, context ) { + var f = jQuery( selector, context ).get(), s = "", i = 0; @@ -47,7 +47,31 @@ QUnit.assert.t = function( a, b, c ) { s += ( s && "," ) + '"' + f[ i ].id + '"'; } - this.deepEqual( f, q.apply( q, c ), a + " (" + b + ")" ); + this.deepEqual( f, q.apply( q, expectedIds ), message + " (" + selector + ")" ); +} + +/** + * Asserts that a select matches the given IDs. + * The select is not bound by a context. + * @param {String} message - Assertion name + * @param {String} selector - Sizzle selector + * @param {String} expectedIds - Array of ids to construct what is expected + * @example t("Check for something", "p", ["foo", "bar"]); + */ +QUnit.assert.t = function( message, selector, expectedIds ) { + match( message, selector, expectedIds, undefined ); +}; + +/** + * Asserts that a select matches the given IDs. + * The select is performed within the `#qunit-fixture` context. + * @param {String} message - Assertion name + * @param {String} selector - Sizzle selector + * @param {String} expectedIds - Array of ids to construct what is expected + * @example selectInFixture("Check for something", "p", ["foo", "bar"]); + */ +QUnit.assert.selectInFixture = function( message, selector, expectedIds ) { + match( message, selector, expectedIds, "#qunit-fixture" ); }; this.createDashboardXML = function() { diff --git a/test/unit/selector.js b/test/unit/selector.js index c3394e380..1263c7cf6 100644 --- a/test/unit/selector.js +++ b/test/unit/selector.js @@ -109,7 +109,7 @@ QUnit.test( "child and adjacent", function( assert ) { assert.t( "Child", "p>a", [ "simon1","google","groups","mark","yahoo","simon" ] ); assert.t( "Child w/ Class", "p > a.blog", [ "mark","simon" ] ); assert.t( "All Children", "code > *", [ "anchor1","anchor2" ] ); - assert.t( "All Grandchildren", "p > * > *", [ "anchor1","anchor2" ] ); + assert.selectInFixture( "All Grandchildren", "p > * > *", [ "anchor1","anchor2" ] ); assert.t( "Adjacent", "p + p", [ "ap","en","sap" ] ); assert.t( "Adjacent", "p#firstp + p", [ "ap" ] ); assert.t( "Adjacent", "p[lang=en] + p", [ "sap" ] ); -- 2.39.5