Skip to content

Commit

Permalink
Updated ArrayAssert functionality. Fixes #2528142
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas C. Zakas committed Aug 3, 2009
1 parent 4e10ce0 commit b044439
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 7 deletions.
4 changes: 2 additions & 2 deletions build/test/test-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ YUI.add('test', function(Y) {
throw new TypeError("ArrayAssert.containsMatch(): First argument must be a function.");
}

if (!Y.Array.some(matcher)){
if (!Y.Array.some(haystack, matcher)){
Y.Assert.fail(Y.Assert._formatMessage(message, "No match found in array [" + haystack + "]."));
}
},
Expand Down Expand Up @@ -1899,7 +1899,7 @@ YUI.add('test', function(Y) {
throw new TypeError("ArrayAssert.doesNotContainMatch(): First argument must be a function.");
}

if (Y.Array.some(matcher)){
if (Y.Array.some(haystack, matcher)){
Y.Assert.fail(Y.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
}
},
Expand Down
2 changes: 1 addition & 1 deletion build/test/test-min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions build/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ YUI.add('test', function(Y) {
throw new TypeError("ArrayAssert.containsMatch(): First argument must be a function.");
}

if (!Y.Array.some(matcher)){
if (!Y.Array.some(haystack, matcher)){
Y.Assert.fail(Y.Assert._formatMessage(message, "No match found in array [" + haystack + "]."));
}
},
Expand Down Expand Up @@ -1899,7 +1899,7 @@ YUI.add('test', function(Y) {
throw new TypeError("ArrayAssert.doesNotContainMatch(): First argument must be a function.");
}

if (Y.Array.some(matcher)){
if (Y.Array.some(haystack, matcher)){
Y.Assert.fail(Y.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
}
},
Expand Down
1 change: 1 addition & 0 deletions src/test/README
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Documentation: http://developer.yahoo.com/yui/3/test

3.0.0
* Added missing space in assert failure message (trac #2528058)
* Fixed ArrayAssert errors (trac #2528142).

3.0.0b1

Expand Down
4 changes: 2 additions & 2 deletions src/test/js/ArrayAssert.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
throw new TypeError("ArrayAssert.containsMatch(): First argument must be a function.");
}

if (!Y.Array.some(matcher)){
if (!Y.Array.some(haystack, matcher)){
Y.Assert.fail(Y.Assert._formatMessage(message, "No match found in array [" + haystack + "]."));
}
},
Expand Down Expand Up @@ -135,7 +135,7 @@
throw new TypeError("ArrayAssert.doesNotContainMatch(): First argument must be a function.");
}

if (Y.Array.some(matcher)){
if (Y.Array.some(haystack, matcher)){
Y.Assert.fail(Y.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
}
},
Expand Down
286 changes: 286 additions & 0 deletions src/test/tests/arrayassert.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
<html>
<head>
<title>array assert tests</title>
<link type="text/css" rel="stylesheet" href="../../../build/logreader/assets/skins/sam/logreader.css" />
<script type="text/javascript" src="../../../build/yui/yui.js"></script>
</head>
<body class="yui-skin-sam">
<h1>Array Assert Tests</h1>
<div id="c"></div>
<script type="text/javascript">

YUI({
base: '../../../build/',
filter: "debug",
logInclude: { TestRunner: true }
}).use('test', 'console', function (Y) {

Y.namespace("Tests");

Y.Tests.ArrayAssert = (function(){

var Assert = Y.Assert,
ArrayAssert = Y.ArrayAssert;

//-------------------------------------------------------------------------
// Base Test Suite
//-------------------------------------------------------------------------

var suite = new Y.Test.Suite("Array Assert Tests");

//-------------------------------------------------------------------------
// Test Case for contains()
//-------------------------------------------------------------------------

suite.add(new Y.Test.Case({

name: "Contains Assert Tests",

_should: {
fail: {
"contains() should fail when a similar item exists": new Y.Assert.Error("Value 1 (number) not found in array [1,0,false,text]."),
"contains() should throw a custom error message during failure": new Y.Assert.Error("True should not be there: Value 1 (number) not found in array [1,0,false,text]."),
"contains() should fail when the item doesn't exist": new Y.Assert.Error("Value true (boolean) not found in array [1,0,false,text].")
}
},

setUp: function(){
this.testArray = ["1", 0, false, "text"];
},

tearDown: function(){
delete this.testArray;
},

"contains() should pass when the given item exists": function () {
ArrayAssert.contains("1", this.testArray);
},

"contains() should fail when a similar item exists": function () {
ArrayAssert.contains(1, this.testArray);
},

"contains() should fail when the item doesn't exist": function() {
ArrayAssert.contains(true, this.testArray);
},

"contains() should throw a custom error message during failure": function(){
ArrayAssert.contains(true, this.testArray, "True should not be there: {message}");
}
}));

//-------------------------------------------------------------------------
// Test Case for contains()
//-------------------------------------------------------------------------

suite.add(new Y.Test.Case({

name: "ContainsItems Assert Tests",

_should: {
fail: {
testSimilarItems: new Y.Assert.Error("Value 1 (number) not found in array [1,0,false,text]."),
testNonExistingItems: new Y.Assert.Error("Value true (boolean) not found in array [1,0,false,text].")
}
},

setUp: function(){
this.testArray = ["1", 0, false, "text"];
},

tearDown: function(){
delete this.testArray;
},

testExistingItems: function () {
ArrayAssert.containsItems(["1",0], this.testArray);
},

testSimilarItems: function () {
ArrayAssert.containsItems([1,0], this.testArray);
},

testNonExistingItems: function() {
ArrayAssert.containsItems([true], this.testArray);
}
}));

//-------------------------------------------------------------------------
// Test Case for containsMatch()
//-------------------------------------------------------------------------

suite.add(new Y.Test.Case({

name: "ContainsMatch Assert Tests",

_should: {
fail: {
testNonExistingItems: new Y.Assert.Error("No match found in array [1,0,false,text].")
}
},

setUp: function(){
this.testArray = ["1", 0, false, "text"];
},

tearDown: function(){
delete this.testArray;
},

testExistingItems: function () {
ArrayAssert.containsMatch(function(value){
return Y.Lang.isString(value);
}, this.testArray);
},

testNonExistingItems: function() {
ArrayAssert.containsMatch(function(value){
return Y.Lang.isObject(value);
}, this.testArray);
}
}));

//-------------------------------------------------------------------------
// Test Case for itemsAreSame()
//-------------------------------------------------------------------------

suite.add(new Y.Test.Case({

name: "itemsAreSame Assert Tests",

_should: {
fail: {
testMissingItem: new Y.Assert.Error("Values in position 3 are not the same."),
testArrayAgainstObject: new Y.Assert.Error("Values in position 0 are not the same.")
}
},

setUp: function(){
this.testArray = ["1", 0, false, "text"];
},

tearDown: function(){
delete this.testArray;
},

testItemsAreSame: function () {
ArrayAssert.itemsAreSame(this.testArray,["1", 0, false, "text"]);
},

testMissingItem: function() {
ArrayAssert.itemsAreSame(this.testArray, ["1", 0, false]);
},

testArrayAgainstObject: function(){
ArrayAssert.itemsAreSame(this.testArray, {});
}
}));

//-------------------------------------------------------------------------
// Test Case for itemsAreEqual()
//-------------------------------------------------------------------------

suite.add(new Y.Test.Case({

name: "itemsAreEqual Assert Tests",

_should: {
fail: {
testMissingItem: new Y.Assert.Error("Values in position 3 are not equal."),
testArrayAgainstObject: new Y.Assert.Error("Values in position 0 are not equal.")
}
},

setUp: function(){
this.testArray = ["1", 0, false, "text"];
},

tearDown: function(){
delete this.testArray;
},

testItemsAreEqual: function () {
ArrayAssert.itemsAreEqual(this.testArray,["1", 0, false, "text"]);
},

testMissingItem: function() {
ArrayAssert.itemsAreEqual(this.testArray, ["1", 0, false]);
},

testArrayAgainstObject: function(){
ArrayAssert.itemsAreEqual(this.testArray, {});
}
}));

//-------------------------------------------------------------------------
// Test Case for itemsAreEquivalent()
//-------------------------------------------------------------------------

suite.add(new Y.Test.Case({

name: "itemsAreEquivalent Assert Tests",

_should: {
fail: {
testMissingItem: new Y.Assert.Error("Values in position 3 are not equal."),
testArrayAgainstObject: new Y.Assert.Error("Values in position 0 are not equal.")
}
},

setUp: function(){
this.testArray = ["1", 0, false, "text"];
this.comparator = function(a,b){
return a == b;
};
},

tearDown: function(){
delete this.testArray;
delete this.comparator;
},

testItemsAreEqual: function () {
ArrayAssert.itemsAreEquivalent(this.testArray,["1", 0, false, "text"], this.comparator);
},

testMissingItem: function() {
ArrayAssert.itemsAreEquivalent(this.testArray, ["1", 0, false], this.comparator);
},

testArrayAgainstObject: function(){
ArrayAssert.itemsAreEquivalent(this.testArray, {}, this.comparator);
}
}));


//return it
return suite;

})();


var r = new Y.Console({
verbose : true,
//consoleLimit : 10,
newestOnTop : false
});

r.render('#c');


//add to the testrunner and run
Y.Test.Runner.add(Y.Tests.ArrayAssert);
Y.Test.Runner.run();

/*if (parent && parent != window) {
YAHOO.tool.TestManager.load();
} else {
YAHOO.tool.TestRunner.run();
}*/

});


</script>
</body>
</html>

0 comments on commit b044439

Please sign in to comment.