Commit 18a56414 18a564143a75a0ef0b914360a5c8420f5fb6f5ba by Nicolas Perriault

added drop() method to casper cli args object

1 parent cab579d1
......@@ -41,6 +41,28 @@ exports.parse = function(phantomArgs) {
var extract = {
args: [],
options: {},
drop: function(what) {
if (utils.isNumber(what)) {
// deleting an arg by its position
this.args = this.args.filter(function(arg, index) {
return index !== what;
});
} else if (utils.isString(what)) {
// deleting an arg by its value
this.args = this.args.filter(function(arg) {
return arg !== what;
});
// deleting an option by its name (key)
var self = this;
Object.keys(this.options).forEach(function(option) {
if (option === what) {
delete self.options[what];
}
});
} else {
throw new CasperError("cannot drop argument of type " + typeof what);
}
},
has: function(what) {
if (utils.isNumber(what)) {
return what in this.args;
......
(function(t) {
var cli = require('cli');
t.comment('parse()');
t.comment('parse(), get(), has()');
(function(parsed) {
t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
t.assertEquals(parsed.options, {}, 'parse() returns expected options object');
t.assertEquals(parsed.get(0), undefined, 'parse() does not return inexistant positional arg');
t.assertEquals(parsed.get('blah'), undefined, 'parse() does not return inexistant option');
t.assert(!parsed.has(0), 'has() checks if an arg is set');
t.assert(!parsed.has('blah'), 'has() checks if an option is set');
})(cli.parse([]));
(function(parsed) {
......@@ -15,6 +17,9 @@
t.assertEquals(parsed.options, {}, 'parse() returns expected options object');
t.assertEquals(parsed.get(0), 'foo', 'parse() retrieve first positional arg');
t.assertEquals(parsed.get(1), 'bar', 'parse() retrieve second positional arg');
t.assert(parsed.has(0), 'has() checks if an arg is set');
t.assert(parsed.has(1), 'has() checks if an arg is set');
t.assert(!parsed.has(2), 'has() checks if an arg is not set');
})(cli.parse(['foo', 'bar']));
(function(parsed) {
......@@ -22,12 +27,15 @@
t.assertEquals(parsed.options, {foo: 'bar', baz: true}, 'parse() returns expected options object');
t.assertEquals(parsed.get('foo'), 'bar', 'parse() retrieve an option value');
t.assert(parsed.get('baz'), 'parse() retrieve boolean option flag');
t.assert(parsed.has("foo"), 'has() checks if an option is set');
t.assert(parsed.has("baz"), 'has() checks if an option is set');
})(cli.parse(['--foo=bar', '--baz']));
(function(parsed) {
t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
t.assertEquals(parsed.options, { '&é"à': 42 }, 'parse() returns expected options object');
t.assertEquals(parsed.get('&é"à'), 42, 'parse() handles options with exotic names');
t.assert(parsed.has('&é"à'), 'has() checks if an option is set');
})(cli.parse(['--&é"à=42']));
(function(parsed) {
......@@ -39,6 +47,25 @@
t.assert(parsed.get('chucknorris'), 'parse() can get a flag value by its option name');
t.assertType(parsed.get('oops'), "boolean", 'parse() can cast a boolean value');
t.assertEquals(parsed.get('oops'), false, 'parse() can cast a boolean value');
t.assert(parsed.has(0), 'has() checks if an arg is set');
t.assert(parsed.has(1), 'has() checks if an arg is set');
t.assert(parsed.has("universe"), 'has() checks if an option is set');
t.assert(parsed.has("lap"), 'has() checks if an option is set');
t.assert(parsed.has("chucknorris"), 'has() checks if an option is set');
t.assert(parsed.has("oops"), 'has() checks if an option is set');
t.comment('drop()');
parsed.drop(0);
t.assertEquals(parsed.get(0), 'baz & boz', 'drop() dropped arg');
parsed.drop("universe");
t.assert(!parsed.has("universe"), 'drop() dropped option');
t.assertEquals(parsed.args, ["baz & boz"], 'drop() did not affect other args');
t.assertEquals(parsed.options, {
lap: 13.37,
chucknorris: true,
oops: false
}, 'drop() did not affect other options');
})(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false']));
t.done();
......