Commit 6da694e7 6da694e7a58ef177d9d1dafb160e302a1eab3f58 by Nicolas Perriault

migrated cli tests

1 parent 34362eaa
......@@ -1074,7 +1074,7 @@ Casper.extend = function(proto) {
if (!utils.isType(proto, "object")) {
throw new Error("extends() only accept objects as prototypes");
}
mergeObjects(Casper.prototype, proto);
utils.mergeObjects(Casper.prototype, proto);
};
exports.Casper = Casper;
......
......@@ -36,9 +36,6 @@ var utils = require('utils');
* @return Object
*/
exports.parse = function(phantomArgs) {
if (!utils.isType(phantomArgs, "runtimearray")) {
throw new Error('parse() can only process a phantomjs arguments array');
}
var extract = {
args: [],
options: {},
......@@ -57,7 +54,7 @@ exports.parse = function(phantomArgs) {
// named option
var optionMatch = arg.match(/^--(.*)=(.*)/i);
if (optionMatch) {
extract.options[optionMatch[1]] = optionMatch[2];
extract.options[optionMatch[1]] = castArgument(optionMatch[2]);
} else {
// flag
var flagMatch = arg.match(/^--(.*)/);
......@@ -72,3 +69,15 @@ exports.parse = function(phantomArgs) {
});
return extract;
};
function castArgument(arg) {
if (arg.match(/^-?\d+$/)) {
return parseInt(arg, 10);
} else if (arg.match(/^-?\d+\.\d+$/)) {
return parseFloat(arg);
} else if (arg.match(/^(true|false)$/i)) {
return arg.trim().toLowerCase() === "true" ? true : false;
} else {
return arg;
}
}
......
......@@ -94,8 +94,8 @@ var Tester = function(casper, options) {
exporter.addSuccess("unknown", message);
} else {
casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING'));
this.comment(' got: ' + serialize(testValue));
this.comment(' expected: ' + serialize(expected));
this.comment(' got: ' + utils.serialize(testValue));
this.comment(' expected: ' + utils.serialize(expected));
this.testResults.failed++;
exporter.addFailure("unknown", message, "test failed; expected: " + expected + "; got: " + testValue, "assertEquals");
}
......@@ -236,7 +236,7 @@ var Tester = function(casper, options) {
};
this.bar = function(text, style) {
casper.echo(fillBlanks(text), style);
casper.echo(utils.fillBlanks(text), style);
};
/**
......@@ -281,10 +281,10 @@ var Tester = function(casper, options) {
* @param String file Absolute path to some js/coffee file
*/
this.exec = function(file) {
if (!fs.isFile(file) || !isJsFile(file)) {
if (!fs.isFile(file) || !utils.isJsFile(file)) {
throw new Error("Can only exec() files with .js or .coffee extensions");
}
if (fileExt(file) === "coffee") {
if (utils.fileExt(file) === "coffee") {
phantom.injectJs(file); // FIXME: syntax validation?
} else {
var testContents = fs.read(file);
......@@ -328,7 +328,7 @@ var Tester = function(casper, options) {
}
});
return entries.filter(function(entry) {
return isJsFile(fs.absolute(fs.pathJoin(dir, entry)));
return utils.isJsFile(fs.absolute(fs.pathJoin(dir, entry)));
});
};
......@@ -380,7 +380,7 @@ var Tester = function(casper, options) {
style = 'GREEN_BAR';
}
result = statusText + ' ' + total + ' tests executed, ' + this.testResults.passed + ' passed, ' + this.testResults.failed + ' failed.';
casper.echo(this.colorize(fillBlanks(result), style));
casper.echo(this.colorize(utils.fillBlanks(result), style));
if (save && utils.isType(require, "function")) {
try {
fs.write(save, exporter.getXML(), 'w');
......
(function(t, phantom) {
t.comment('phantom.extractCasperArgs()');
var cli = require('cli');
t.assertEquals(phantom.extractCasperArgs([]), {
args: [],
options: {}
}, 'extractCasperArgs() returns a formatted object');
t.comment('parse()');
t.assertEquals(phantom.extractCasperArgs(['foo', 'bar']), {
args: ['foo', 'bar'],
options: {}
}, 'extractCasperArgs() can extract args');
(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');
})(cli.parse([]));
t.assertEquals(phantom.extractCasperArgs(['--foo=bar', '--baz']), {
args: [],
options: { foo: 'bar', baz: true }
}, 'extractCasperArgs() can extract options and flags');
(function(parsed) {
t.assertEquals(parsed.args, ['foo', 'bar'], 'parse() returns expected positional args array');
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');
})(cli.parse(['foo', 'bar']));
t.assertEquals(phantom.extractCasperArgs(['--&é"à=42']), {
args: [],
options: { '&é"à': '42' }
}, 'extractCasperArgs() can extract exotic option name');
(function(parsed) {
t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
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');
})(cli.parse(['--foo=bar', '--baz']));
t.assertEquals(phantom.extractCasperArgs(['foo', 'bar', '--universe=42', '--chucknorris']), {
args: ['foo', 'bar'],
options: { universe: '42', chucknorris: true }
}, 'extractCasperArgs() can extract args, options and flags');
(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');
})(cli.parse(['--&é"à=42']));
t.assertEquals(phantom.extractCasperArgs(['bar', '--universe=42', '--chucknorris', 'foo']), {
args: ['bar', 'foo'],
options: { universe: '42', chucknorris: true }
}, 'extractCasperArgs() positional args order is preserved, option one has no importance');
(function(parsed) {
t.assertEquals(parsed.args, ['foo & bar', 'baz & boz'], 'parse() returns expected positional args array');
t.assertEquals(parsed.options, { universe: 42, lap: 13.37, chucknorris: true, oops: false }, 'parse() returns expected options object');
t.assertEquals(parsed.get('universe'), 42, 'parse() can cast a numeric option value');
t.assertEquals(parsed.get('lap'), 13.37, 'parse() can cast a float option value');
t.assert(parsed.get('chucknorris'), 'parse() can get a flag value by its option name');
t.assert(!parsed.get('oops'), 'parse() can cast a boolean value');
})(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false']));
t.done();
})(casper.test, phantom);
......
(function(t) {
var utils = require('utils');
t.comment('fileExt()');
(function() {
......@@ -12,7 +14,7 @@
};
for (var testCase in testCases) {
t.assertEquals(fileExt(testCase), testCases[testCase], 'fileExt() extract file extension');
t.assertEquals(utils.fileExt(testCase), testCases[testCase], 'fileExt() extract file extension');
}
})();
......@@ -26,7 +28,7 @@
};
for (var testCase in testCases) {
t.assertEquals(fillBlanks(testCase, 10), testCases[testCase], 'fillBlanks() fills blanks');
t.assertEquals(utils.fillBlanks(testCase, 10), testCases[testCase], 'fillBlanks() fills blanks');
}
})();
......@@ -42,7 +44,7 @@
};
for (var testCase in testCases) {
t.assertEquals(isJsFile(testCase), testCases[testCase], 'isJsFile() checks for js file');
t.assertEquals(utils.isJsFile(testCase), testCases[testCase], 'isJsFile() checks for js file');
}
})();
......@@ -74,7 +76,7 @@
];
testCases.forEach(function(testCase) {
t.assertEquals(mergeObjects(testCase.obj1, testCase.obj2), testCase.merged, 'mergeObjects() can merge objects');
t.assertEquals(utils.mergeObjects(testCase.obj1, testCase.obj2), testCase.merged, 'mergeObjects() can merge objects');
});
})();
......