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) { ...@@ -41,6 +41,28 @@ exports.parse = function(phantomArgs) {
41 var extract = { 41 var extract = {
42 args: [], 42 args: [],
43 options: {}, 43 options: {},
44 drop: function(what) {
45 if (utils.isNumber(what)) {
46 // deleting an arg by its position
47 this.args = this.args.filter(function(arg, index) {
48 return index !== what;
49 });
50 } else if (utils.isString(what)) {
51 // deleting an arg by its value
52 this.args = this.args.filter(function(arg) {
53 return arg !== what;
54 });
55 // deleting an option by its name (key)
56 var self = this;
57 Object.keys(this.options).forEach(function(option) {
58 if (option === what) {
59 delete self.options[what];
60 }
61 });
62 } else {
63 throw new CasperError("cannot drop argument of type " + typeof what);
64 }
65 },
44 has: function(what) { 66 has: function(what) {
45 if (utils.isNumber(what)) { 67 if (utils.isNumber(what)) {
46 return what in this.args; 68 return what in this.args;
......
1 (function(t) { 1 (function(t) {
2 var cli = require('cli'); 2 var cli = require('cli');
3 3
4 t.comment('parse()'); 4 t.comment('parse(), get(), has()');
5 5
6 (function(parsed) { 6 (function(parsed) {
7 t.assertEquals(parsed.args, [], 'parse() returns expected positional args array'); 7 t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
8 t.assertEquals(parsed.options, {}, 'parse() returns expected options object'); 8 t.assertEquals(parsed.options, {}, 'parse() returns expected options object');
9 t.assertEquals(parsed.get(0), undefined, 'parse() does not return inexistant positional arg'); 9 t.assertEquals(parsed.get(0), undefined, 'parse() does not return inexistant positional arg');
10 t.assertEquals(parsed.get('blah'), undefined, 'parse() does not return inexistant option'); 10 t.assertEquals(parsed.get('blah'), undefined, 'parse() does not return inexistant option');
11 t.assert(!parsed.has(0), 'has() checks if an arg is set');
12 t.assert(!parsed.has('blah'), 'has() checks if an option is set');
11 })(cli.parse([])); 13 })(cli.parse([]));
12 14
13 (function(parsed) { 15 (function(parsed) {
...@@ -15,6 +17,9 @@ ...@@ -15,6 +17,9 @@
15 t.assertEquals(parsed.options, {}, 'parse() returns expected options object'); 17 t.assertEquals(parsed.options, {}, 'parse() returns expected options object');
16 t.assertEquals(parsed.get(0), 'foo', 'parse() retrieve first positional arg'); 18 t.assertEquals(parsed.get(0), 'foo', 'parse() retrieve first positional arg');
17 t.assertEquals(parsed.get(1), 'bar', 'parse() retrieve second positional arg'); 19 t.assertEquals(parsed.get(1), 'bar', 'parse() retrieve second positional arg');
20 t.assert(parsed.has(0), 'has() checks if an arg is set');
21 t.assert(parsed.has(1), 'has() checks if an arg is set');
22 t.assert(!parsed.has(2), 'has() checks if an arg is not set');
18 })(cli.parse(['foo', 'bar'])); 23 })(cli.parse(['foo', 'bar']));
19 24
20 (function(parsed) { 25 (function(parsed) {
...@@ -22,12 +27,15 @@ ...@@ -22,12 +27,15 @@
22 t.assertEquals(parsed.options, {foo: 'bar', baz: true}, 'parse() returns expected options object'); 27 t.assertEquals(parsed.options, {foo: 'bar', baz: true}, 'parse() returns expected options object');
23 t.assertEquals(parsed.get('foo'), 'bar', 'parse() retrieve an option value'); 28 t.assertEquals(parsed.get('foo'), 'bar', 'parse() retrieve an option value');
24 t.assert(parsed.get('baz'), 'parse() retrieve boolean option flag'); 29 t.assert(parsed.get('baz'), 'parse() retrieve boolean option flag');
30 t.assert(parsed.has("foo"), 'has() checks if an option is set');
31 t.assert(parsed.has("baz"), 'has() checks if an option is set');
25 })(cli.parse(['--foo=bar', '--baz'])); 32 })(cli.parse(['--foo=bar', '--baz']));
26 33
27 (function(parsed) { 34 (function(parsed) {
28 t.assertEquals(parsed.args, [], 'parse() returns expected positional args array'); 35 t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
29 t.assertEquals(parsed.options, { '&é"à': 42 }, 'parse() returns expected options object'); 36 t.assertEquals(parsed.options, { '&é"à': 42 }, 'parse() returns expected options object');
30 t.assertEquals(parsed.get('&é"à'), 42, 'parse() handles options with exotic names'); 37 t.assertEquals(parsed.get('&é"à'), 42, 'parse() handles options with exotic names');
38 t.assert(parsed.has('&é"à'), 'has() checks if an option is set');
31 })(cli.parse(['--&é"à=42'])); 39 })(cli.parse(['--&é"à=42']));
32 40
33 (function(parsed) { 41 (function(parsed) {
...@@ -39,6 +47,25 @@ ...@@ -39,6 +47,25 @@
39 t.assert(parsed.get('chucknorris'), 'parse() can get a flag value by its option name'); 47 t.assert(parsed.get('chucknorris'), 'parse() can get a flag value by its option name');
40 t.assertType(parsed.get('oops'), "boolean", 'parse() can cast a boolean value'); 48 t.assertType(parsed.get('oops'), "boolean", 'parse() can cast a boolean value');
41 t.assertEquals(parsed.get('oops'), false, 'parse() can cast a boolean value'); 49 t.assertEquals(parsed.get('oops'), false, 'parse() can cast a boolean value');
50 t.assert(parsed.has(0), 'has() checks if an arg is set');
51 t.assert(parsed.has(1), 'has() checks if an arg is set');
52 t.assert(parsed.has("universe"), 'has() checks if an option is set');
53 t.assert(parsed.has("lap"), 'has() checks if an option is set');
54 t.assert(parsed.has("chucknorris"), 'has() checks if an option is set');
55 t.assert(parsed.has("oops"), 'has() checks if an option is set');
56
57 t.comment('drop()');
58
59 parsed.drop(0);
60 t.assertEquals(parsed.get(0), 'baz & boz', 'drop() dropped arg');
61 parsed.drop("universe");
62 t.assert(!parsed.has("universe"), 'drop() dropped option');
63 t.assertEquals(parsed.args, ["baz & boz"], 'drop() did not affect other args');
64 t.assertEquals(parsed.options, {
65 lap: 13.37,
66 chucknorris: true,
67 oops: false
68 }, 'drop() did not affect other options');
42 })(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false'])); 69 })(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false']));
43 70
44 t.done(); 71 t.done();
......