migrated cli tests
Showing
5 changed files
with
60 additions
and
41 deletions
... | @@ -1074,7 +1074,7 @@ Casper.extend = function(proto) { | ... | @@ -1074,7 +1074,7 @@ Casper.extend = function(proto) { |
1074 | if (!utils.isType(proto, "object")) { | 1074 | if (!utils.isType(proto, "object")) { |
1075 | throw new Error("extends() only accept objects as prototypes"); | 1075 | throw new Error("extends() only accept objects as prototypes"); |
1076 | } | 1076 | } |
1077 | mergeObjects(Casper.prototype, proto); | 1077 | utils.mergeObjects(Casper.prototype, proto); |
1078 | }; | 1078 | }; |
1079 | 1079 | ||
1080 | exports.Casper = Casper; | 1080 | exports.Casper = Casper; | ... | ... |
... | @@ -36,9 +36,6 @@ var utils = require('utils'); | ... | @@ -36,9 +36,6 @@ var utils = require('utils'); |
36 | * @return Object | 36 | * @return Object |
37 | */ | 37 | */ |
38 | exports.parse = function(phantomArgs) { | 38 | exports.parse = function(phantomArgs) { |
39 | if (!utils.isType(phantomArgs, "runtimearray")) { | ||
40 | throw new Error('parse() can only process a phantomjs arguments array'); | ||
41 | } | ||
42 | var extract = { | 39 | var extract = { |
43 | args: [], | 40 | args: [], |
44 | options: {}, | 41 | options: {}, |
... | @@ -57,7 +54,7 @@ exports.parse = function(phantomArgs) { | ... | @@ -57,7 +54,7 @@ exports.parse = function(phantomArgs) { |
57 | // named option | 54 | // named option |
58 | var optionMatch = arg.match(/^--(.*)=(.*)/i); | 55 | var optionMatch = arg.match(/^--(.*)=(.*)/i); |
59 | if (optionMatch) { | 56 | if (optionMatch) { |
60 | extract.options[optionMatch[1]] = optionMatch[2]; | 57 | extract.options[optionMatch[1]] = castArgument(optionMatch[2]); |
61 | } else { | 58 | } else { |
62 | // flag | 59 | // flag |
63 | var flagMatch = arg.match(/^--(.*)/); | 60 | var flagMatch = arg.match(/^--(.*)/); |
... | @@ -72,3 +69,15 @@ exports.parse = function(phantomArgs) { | ... | @@ -72,3 +69,15 @@ exports.parse = function(phantomArgs) { |
72 | }); | 69 | }); |
73 | return extract; | 70 | return extract; |
74 | }; | 71 | }; |
72 | |||
73 | function castArgument(arg) { | ||
74 | if (arg.match(/^-?\d+$/)) { | ||
75 | return parseInt(arg, 10); | ||
76 | } else if (arg.match(/^-?\d+\.\d+$/)) { | ||
77 | return parseFloat(arg); | ||
78 | } else if (arg.match(/^(true|false)$/i)) { | ||
79 | return arg.trim().toLowerCase() === "true" ? true : false; | ||
80 | } else { | ||
81 | return arg; | ||
82 | } | ||
83 | } | ... | ... |
... | @@ -94,8 +94,8 @@ var Tester = function(casper, options) { | ... | @@ -94,8 +94,8 @@ var Tester = function(casper, options) { |
94 | exporter.addSuccess("unknown", message); | 94 | exporter.addSuccess("unknown", message); |
95 | } else { | 95 | } else { |
96 | casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); | 96 | casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); |
97 | this.comment(' got: ' + serialize(testValue)); | 97 | this.comment(' got: ' + utils.serialize(testValue)); |
98 | this.comment(' expected: ' + serialize(expected)); | 98 | this.comment(' expected: ' + utils.serialize(expected)); |
99 | this.testResults.failed++; | 99 | this.testResults.failed++; |
100 | exporter.addFailure("unknown", message, "test failed; expected: " + expected + "; got: " + testValue, "assertEquals"); | 100 | exporter.addFailure("unknown", message, "test failed; expected: " + expected + "; got: " + testValue, "assertEquals"); |
101 | } | 101 | } |
... | @@ -236,7 +236,7 @@ var Tester = function(casper, options) { | ... | @@ -236,7 +236,7 @@ var Tester = function(casper, options) { |
236 | }; | 236 | }; |
237 | 237 | ||
238 | this.bar = function(text, style) { | 238 | this.bar = function(text, style) { |
239 | casper.echo(fillBlanks(text), style); | 239 | casper.echo(utils.fillBlanks(text), style); |
240 | }; | 240 | }; |
241 | 241 | ||
242 | /** | 242 | /** |
... | @@ -281,10 +281,10 @@ var Tester = function(casper, options) { | ... | @@ -281,10 +281,10 @@ var Tester = function(casper, options) { |
281 | * @param String file Absolute path to some js/coffee file | 281 | * @param String file Absolute path to some js/coffee file |
282 | */ | 282 | */ |
283 | this.exec = function(file) { | 283 | this.exec = function(file) { |
284 | if (!fs.isFile(file) || !isJsFile(file)) { | 284 | if (!fs.isFile(file) || !utils.isJsFile(file)) { |
285 | throw new Error("Can only exec() files with .js or .coffee extensions"); | 285 | throw new Error("Can only exec() files with .js or .coffee extensions"); |
286 | } | 286 | } |
287 | if (fileExt(file) === "coffee") { | 287 | if (utils.fileExt(file) === "coffee") { |
288 | phantom.injectJs(file); // FIXME: syntax validation? | 288 | phantom.injectJs(file); // FIXME: syntax validation? |
289 | } else { | 289 | } else { |
290 | var testContents = fs.read(file); | 290 | var testContents = fs.read(file); |
... | @@ -328,7 +328,7 @@ var Tester = function(casper, options) { | ... | @@ -328,7 +328,7 @@ var Tester = function(casper, options) { |
328 | } | 328 | } |
329 | }); | 329 | }); |
330 | return entries.filter(function(entry) { | 330 | return entries.filter(function(entry) { |
331 | return isJsFile(fs.absolute(fs.pathJoin(dir, entry))); | 331 | return utils.isJsFile(fs.absolute(fs.pathJoin(dir, entry))); |
332 | }); | 332 | }); |
333 | }; | 333 | }; |
334 | 334 | ||
... | @@ -380,7 +380,7 @@ var Tester = function(casper, options) { | ... | @@ -380,7 +380,7 @@ var Tester = function(casper, options) { |
380 | style = 'GREEN_BAR'; | 380 | style = 'GREEN_BAR'; |
381 | } | 381 | } |
382 | result = statusText + ' ' + total + ' tests executed, ' + this.testResults.passed + ' passed, ' + this.testResults.failed + ' failed.'; | 382 | result = statusText + ' ' + total + ' tests executed, ' + this.testResults.passed + ' passed, ' + this.testResults.failed + ' failed.'; |
383 | casper.echo(this.colorize(fillBlanks(result), style)); | 383 | casper.echo(this.colorize(utils.fillBlanks(result), style)); |
384 | if (save && utils.isType(require, "function")) { | 384 | if (save && utils.isType(require, "function")) { |
385 | try { | 385 | try { |
386 | fs.write(save, exporter.getXML(), 'w'); | 386 | fs.write(save, exporter.getXML(), 'w'); | ... | ... |
1 | (function(t, phantom) { | 1 | (function(t, phantom) { |
2 | t.comment('phantom.extractCasperArgs()'); | 2 | var cli = require('cli'); |
3 | 3 | ||
4 | t.assertEquals(phantom.extractCasperArgs([]), { | 4 | t.comment('parse()'); |
5 | args: [], | ||
6 | options: {} | ||
7 | }, 'extractCasperArgs() returns a formatted object'); | ||
8 | 5 | ||
9 | t.assertEquals(phantom.extractCasperArgs(['foo', 'bar']), { | 6 | (function(parsed) { |
10 | args: ['foo', 'bar'], | 7 | t.assertEquals(parsed.args, [], 'parse() returns expected positional args array'); |
11 | options: {} | 8 | t.assertEquals(parsed.options, {}, 'parse() returns expected options object'); |
12 | }, 'extractCasperArgs() can extract args'); | 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'); | ||
11 | })(cli.parse([])); | ||
13 | 12 | ||
14 | t.assertEquals(phantom.extractCasperArgs(['--foo=bar', '--baz']), { | 13 | (function(parsed) { |
15 | args: [], | 14 | t.assertEquals(parsed.args, ['foo', 'bar'], 'parse() returns expected positional args array'); |
16 | options: { foo: 'bar', baz: true } | 15 | t.assertEquals(parsed.options, {}, 'parse() returns expected options object'); |
17 | }, 'extractCasperArgs() can extract options and flags'); | 16 | t.assertEquals(parsed.get(0), 'foo', 'parse() retrieve first positional arg'); |
17 | t.assertEquals(parsed.get(1), 'bar', 'parse() retrieve second positional arg'); | ||
18 | })(cli.parse(['foo', 'bar'])); | ||
18 | 19 | ||
19 | t.assertEquals(phantom.extractCasperArgs(['--&é"à=42']), { | 20 | (function(parsed) { |
20 | args: [], | 21 | t.assertEquals(parsed.args, [], 'parse() returns expected positional args array'); |
21 | options: { '&é"à': '42' } | 22 | t.assertEquals(parsed.options, {foo: 'bar', baz: true}, 'parse() returns expected options object'); |
22 | }, 'extractCasperArgs() can extract exotic option name'); | 23 | t.assertEquals(parsed.get('foo'), 'bar', 'parse() retrieve an option value'); |
24 | t.assert(parsed.get('baz'), 'parse() retrieve boolean option flag'); | ||
25 | })(cli.parse(['--foo=bar', '--baz'])); | ||
23 | 26 | ||
24 | t.assertEquals(phantom.extractCasperArgs(['foo', 'bar', '--universe=42', '--chucknorris']), { | 27 | (function(parsed) { |
25 | args: ['foo', 'bar'], | 28 | t.assertEquals(parsed.args, [], 'parse() returns expected positional args array'); |
26 | options: { universe: '42', chucknorris: true } | 29 | t.assertEquals(parsed.options, { '&é"à': 42 }, 'parse() returns expected options object'); |
27 | }, 'extractCasperArgs() can extract args, options and flags'); | 30 | t.assertEquals(parsed.get('&é"à'), 42, 'parse() handles options with exotic names'); |
31 | })(cli.parse(['--&é"à=42'])); | ||
28 | 32 | ||
29 | t.assertEquals(phantom.extractCasperArgs(['bar', '--universe=42', '--chucknorris', 'foo']), { | 33 | (function(parsed) { |
30 | args: ['bar', 'foo'], | 34 | t.assertEquals(parsed.args, ['foo & bar', 'baz & boz'], 'parse() returns expected positional args array'); |
31 | options: { universe: '42', chucknorris: true } | 35 | t.assertEquals(parsed.options, { universe: 42, lap: 13.37, chucknorris: true, oops: false }, 'parse() returns expected options object'); |
32 | }, 'extractCasperArgs() positional args order is preserved, option one has no importance'); | 36 | t.assertEquals(parsed.get('universe'), 42, 'parse() can cast a numeric option value'); |
37 | t.assertEquals(parsed.get('lap'), 13.37, 'parse() can cast a float option value'); | ||
38 | t.assert(parsed.get('chucknorris'), 'parse() can get a flag value by its option name'); | ||
39 | t.assert(!parsed.get('oops'), 'parse() can cast a boolean value'); | ||
40 | })(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false'])); | ||
33 | 41 | ||
34 | t.done(); | 42 | t.done(); |
35 | })(casper.test, phantom); | 43 | })(casper.test, phantom); | ... | ... |
1 | (function(t) { | 1 | (function(t) { |
2 | var utils = require('utils'); | ||
3 | |||
2 | t.comment('fileExt()'); | 4 | t.comment('fileExt()'); |
3 | 5 | ||
4 | (function() { | 6 | (function() { |
... | @@ -12,7 +14,7 @@ | ... | @@ -12,7 +14,7 @@ |
12 | }; | 14 | }; |
13 | 15 | ||
14 | for (var testCase in testCases) { | 16 | for (var testCase in testCases) { |
15 | t.assertEquals(fileExt(testCase), testCases[testCase], 'fileExt() extract file extension'); | 17 | t.assertEquals(utils.fileExt(testCase), testCases[testCase], 'fileExt() extract file extension'); |
16 | } | 18 | } |
17 | })(); | 19 | })(); |
18 | 20 | ||
... | @@ -26,7 +28,7 @@ | ... | @@ -26,7 +28,7 @@ |
26 | }; | 28 | }; |
27 | 29 | ||
28 | for (var testCase in testCases) { | 30 | for (var testCase in testCases) { |
29 | t.assertEquals(fillBlanks(testCase, 10), testCases[testCase], 'fillBlanks() fills blanks'); | 31 | t.assertEquals(utils.fillBlanks(testCase, 10), testCases[testCase], 'fillBlanks() fills blanks'); |
30 | } | 32 | } |
31 | })(); | 33 | })(); |
32 | 34 | ||
... | @@ -42,7 +44,7 @@ | ... | @@ -42,7 +44,7 @@ |
42 | }; | 44 | }; |
43 | 45 | ||
44 | for (var testCase in testCases) { | 46 | for (var testCase in testCases) { |
45 | t.assertEquals(isJsFile(testCase), testCases[testCase], 'isJsFile() checks for js file'); | 47 | t.assertEquals(utils.isJsFile(testCase), testCases[testCase], 'isJsFile() checks for js file'); |
46 | } | 48 | } |
47 | })(); | 49 | })(); |
48 | 50 | ||
... | @@ -74,7 +76,7 @@ | ... | @@ -74,7 +76,7 @@ |
74 | ]; | 76 | ]; |
75 | 77 | ||
76 | testCases.forEach(function(testCase) { | 78 | testCases.forEach(function(testCase) { |
77 | t.assertEquals(mergeObjects(testCase.obj1, testCase.obj2), testCase.merged, 'mergeObjects() can merge objects'); | 79 | t.assertEquals(utils.mergeObjects(testCase.obj1, testCase.obj2), testCase.merged, 'mergeObjects() can merge objects'); |
78 | }); | 80 | }); |
79 | })(); | 81 | })(); |
80 | 82 | ... | ... |
-
Please register or sign in to post a comment