Commit ada90f6a ada90f6a9c99a7f8bf7034ab8fa95a95a414d61d by Nicolas Perriault

fixed Casper#each didn't pass the correct index argument, added tests

1 parent 214ed0c3
...@@ -231,13 +231,25 @@ ...@@ -231,13 +231,25 @@
231 return this.exit(Number(status) > 0 ? Number(status) : 1); 231 return this.exit(Number(status) > 0 ? Number(status) : 1);
232 }, 232 },
233 233
234 /**
235 * Iterates over the values of a provided array and execute a callback
236 * for each item.
237 *
238 * @param Array array
239 * @param Function fn Callback: function(self, item, index)
240 * @return Casper
241 */
234 each: function(array, fn) { 242 each: function(array, fn) {
235 var i = 0; 243 if (array.constructor !== Array) {
236 (function(self, i) { 244 self.log("each() only works with arrays", "error");
237 array.forEach(function(item) { 245 return this;
246 }
247
248 (function(self) {
249 array.forEach(function(item, i) {
238 fn(self, item, i); 250 fn(self, item, i);
239 }); 251 });
240 })(this, i); 252 })(this);
241 return this; 253 return this;
242 }, 254 },
243 255
...@@ -1033,9 +1045,7 @@ ...@@ -1033,9 +1045,7 @@
1033 * @param String message Test description 1045 * @param String message Test description
1034 */ 1046 */
1035 this.assertTitle = function(expected, message) { 1047 this.assertTitle = function(expected, message) {
1036 return this.assertEvalEquals(function() { 1048 return this.assertEquals(casper.getTitle(), expected, message);
1037 return document.title;
1038 }, expected, message);
1039 }; 1049 };
1040 1050
1041 /** 1051 /**
......
1 phantom.injectJs('casper.js'); 1 phantom.injectJs('casper.js');
2 2
3 var casper = new phantom.Casper({ 3 var casper = new phantom.Casper({
4 faultTolerant: false 4 faultTolerant: false,
5 verbose: true
5 }); 6 });
6 7
7 var save = null; 8 var save = null;
...@@ -99,6 +100,11 @@ casper.then(function(self) { ...@@ -99,6 +100,11 @@ casper.then(function(self) {
99 self.test.assertUrlMatch(/topic=bar/, 'fill() select field was submitted'); 100 self.test.assertUrlMatch(/topic=bar/, 'fill() select field was submitted');
100 }); 101 });
101 102
103 // Casper#each()
104 casper.each([1, 2, 3], function(self, item, i) {
105 self.test.assertEquals(i, item - 1, 'each() passes a contextualized index');
106 });
107
102 // Casper.XUnitExporter 108 // Casper.XUnitExporter
103 casper.test.comment('phantom.Casper.XUnitExporter'); 109 casper.test.comment('phantom.Casper.XUnitExporter');
104 xunit = new phantom.Casper.XUnitExporter(); 110 xunit = new phantom.Casper.XUnitExporter();
......