Commit 73d31860 73d318606da0aa002db2a521192f84200787a041 by Nicolas Perriault

cli: drop() arg or option now reflected in raw version

1 parent 842e9089
......@@ -111,6 +111,7 @@ Last, all the casper test suites have been upgraded to use the new testing featu
- fixes [#410](https://github.com/n1k0/casperjs/issues/410) - trigger `mousedown` and `mousedown` events on click
- Added [`Tester#skip`](http://docs.casperjs.org/en/latest/modules/tester.html#skip) method
- Added [`Casper#eachThen()`](http://docs.casperjs.org/en/latest/modules/casper.html#eachThen)
- `cli`: Now dropping an arg or an option will be reflected in their *raw* equivalent
2013-02-08, v1.0.2
------------------
......
......@@ -166,7 +166,7 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
/**
* Patched require to allow loading of native casperjs modules.
* Every casperjs native module have to first call this function in order to
* Every casperjs module have to first call this function in order to
* load a native casperjs module:
*
* var require = patchRequire(require);
......
......@@ -56,20 +56,28 @@ exports.parse = function parse(phantomArgs) {
this.args = this.args.filter(function _filter(arg, index) {
return index !== what;
});
// raw
if ('raw' in this) {
this.raw.args = this.raw.args.filter(function _filter(arg, index) {
return index !== what;
});
}
} else if (utils.isString(what)) {
// deleting an arg by its value
this.args = this.args.filter(function _filter(arg) {
return arg !== what;
});
// deleting an option by its name (key)
var self = this;
Object.keys(this.options).forEach(function _forEach(option) {
if (option === what) {
delete self.options[what];
}
});
delete this.options[what];
// raw
if ('raw' in this) {
this.raw.args = this.raw.args.filter(function _filter(arg) {
return arg !== what;
});
delete this.raw.options[what];
}
} else {
throw new CasperError("cannot drop argument of type " + typeof what);
throw new CasperError("Cannot drop argument of type " + typeof what);
}
},
has: function has(what) {
......@@ -112,7 +120,9 @@ exports.parse = function parse(phantomArgs) {
}
});
extract.raw = utils.mergeObjects(extract.raw, {
drop: extract.drop,
drop: function() {
return extract.drop.apply(extract, arguments);
},
has: extract.has,
get: extract.get
});
......
......@@ -21,4 +21,10 @@ dump(casper.cli.args);
casper.echo("Casper CLI passed options:");
dump(casper.cli.options);
casper.echo("Casper CLI passed RAW args:");
dump(casper.cli.raw.args);
casper.echo("Casper CLI passed RAW options:");
dump(casper.cli.raw.options);
casper.exit();
......
......@@ -76,7 +76,7 @@ casper.test.begin('parsing an empty argument list', 8, function(test) {
test.done();
});
casper.test.begin('parsing commands containing args and options', 30, function(test) {
casper.test.begin('parsing commands containing args and options', 34, function(test) {
var parsed = cli.parse(['foo & bar', 'baz & boz', '--universe=42',
'--lap=13.37', '--chucknorris', '--oops=false']);
// clean
......@@ -104,6 +104,7 @@ casper.test.begin('parsing commands containing args and options', 30, function(t
test.assertEquals(parsed.get(0), 'baz & boz', 'drop() dropped arg');
parsed.drop("universe");
test.assert(!parsed.has("universe"), 'drop() dropped option');
test.assert(!parsed.raw.has("universe"), 'drop() dropped raw option');
test.assertEquals(parsed.args, ["baz & boz"], 'drop() did not affect other args');
test.assertEquals(parsed.options, {
lap: 13.37,
......@@ -112,14 +113,17 @@ casper.test.begin('parsing commands containing args and options', 30, function(t
}, 'drop() did not affect other options');
// raw
test.assertEquals(parsed.raw.args, ['foo & bar', 'baz & boz'],
test.assertEquals(parsed.args.length, parsed.raw.args.length,
'parse() cast and raw args share same length');
test.assertEquals(Object.keys(parsed.options).length, Object.keys(parsed.raw.options).length,
'parse() cast and raw options share same length');
test.assertEquals(parsed.raw.args, ['baz & boz'],
'parse() returns expected positional raw args array');
test.assertEquals(parsed.raw.options, {
universe: "42",
lap: "13.37",
chucknorris: true,
oops: "false" }, 'parse() returns expected options raw object');
test.assertEquals(parsed.raw.get('universe'), "42", 'parse() does not a raw numeric option value');
oops: "false"
}, 'parse() returns expected options raw object');
test.assertEquals(parsed.raw.get('lap'), "13.37", 'parse() does not cast a raw float option value');
test.assertType(parsed.raw.get('lap'), "string", 'parse() does not cast a numeric value');
test.assert(parsed.raw.get('chucknorris'), 'parse() can get a flag value by its option name');
......@@ -128,14 +132,18 @@ casper.test.begin('parsing commands containing args and options', 30, function(t
// drop() for raw
parsed.raw.drop(0);
test.assertEquals(parsed.raw.get(0), 'baz & boz', 'drop() dropped raw arg');
test.assertEquals(parsed.raw.get(0), undefined, 'drop() dropped raw arg');
parsed.raw.drop("universe");
test.assert(!parsed.raw.has("universe"), 'drop() dropped raw option');
test.assertEquals(parsed.raw.args, ["baz & boz"], 'drop() did not affect other raw args');
test.assertEquals(parsed.raw.args, [], 'drop() did not affect other raw args');
test.assertEquals(parsed.raw.options, {
lap: "13.37",
chucknorris: true,
oops: "false"
}, 'drop() did not affect other raw options');
parsed.raw.drop("lap");
test.assert(!parsed.raw.has("lap"), 'drop() dropped raw option');
test.assert(!parsed.has("lap"), 'drop() dropped cast option as well');
test.done();
});
......