Commit c1d3de20 c1d3de208aa9324112beeee9d89ff1e2a589c167 by Nicolas Perriault

refs #132 - include files when running test suites

A new `--includes` option has been added to the `casper test` command,
allowing to pass a list of js/coffee scripts to include before each
suite execution:

    $ casperjs test --includes=foo.js,bar.js my_test_suite.js
1 parent 75286e4d
CasperJS Changelog
==================
XXXX-XX-XX, v0.6.11
-------------------
- closed [#132](https://github.com/n1k0/casperjs/issues/132) - added ability to include js/coffee files using a dedicated option when using the `casper test` command
2012-06-04, v0.6.10
-------------------
......
Subproject commit 2a26b9af28b829c83bf374841b851bd0bb7a226a
Subproject commit 1429f8f52a334c4bef7a699c07cff43c66d81f29
......
......@@ -50,6 +50,7 @@ var Tester = function Tester(casper, options) {
this.currentTestFile = null;
this.exporter = require('xunit').create();
this.includes = [];
this.running = false;
this.suites = [];
this.options = utils.mergeObjects({
......@@ -687,6 +688,9 @@ var Tester = function Tester(casper, options) {
this.runTest = function runTest(testFile) {
this.bar(f('Test file: %s', testFile), 'INFO_BAR');
this.running = true; // this.running is set back to false with done()
this.includes.forEach(function(include) {
phantom.injectJs(include);
});
this.exec(testFile);
};
......
......@@ -354,3 +354,25 @@ function serialize(value) {
return JSON.stringify(value, null, 4);
}
exports.serialize = serialize;
/**
* Returns unique values from an array.
*
* Note: ugly code is ugly, but efficient: http://jsperf.com/array-unique2/8
*
* @param Array array
* @return Array
*/
function unique(array) {
var o = {},
r = [];
for (var i = 0, len = array.length; i !== len; i++) {
var d = array[i];
if (o[d] !== 1) {
o[d] = 1;
r[r.length] = d;
}
}
return r;
}
exports.unique = unique;
......
......@@ -6,15 +6,39 @@ if (!phantom.casperLoaded) {
var fs = require('fs');
var utils = require('utils');
var f = utils.format;
var includes = [];
var tests = [];
var casper = require('casper').create({
exitOnError: false
});
// Options from cli
// local utils
function checkIncludeFile(include) {
var absInclude = fs.absolute(include.trim());
if (!fs.exists(absInclude)) {
casper.warn("%s file not found, can't be included", absInclude);
return;
}
if (!utils.isJsFile(absInclude)) {
casper.warn("%s is not a supported file type, can't be included", absInclude);
return;
}
if (fs.isDirectory(absInclude)) {
casper.warn("%s is a directory, can't be included", absInclude);
return;
}
if (tests.indexOf(include) > -1 || tests.indexOf(absInclude) > -1) {
casper.warn("%s is a test file, can't be included", absInclude);
return;
}
return absInclude;
}
// parse some options from cli
casper.options.verbose = casper.cli.get('direct') || false;
casper.options.logLevel = casper.cli.get('log-level') || "error";
// Overriding Casper.open to prefix all test urls
// overriding Casper.open to prefix all test urls
casper.setFilter('open.location', function(location) {
if (!/^http/.test(location)) {
return f('file://%s/%s', phantom.casperPath, location);
......@@ -22,8 +46,7 @@ casper.setFilter('open.location', function(location) {
return location;
});
var tests = [];
// test paths are passed as args
if (casper.cli.args.length) {
tests = casper.cli.args.filter(function(path) {
return fs.isFile(path) || fs.isDirectory(path);
......@@ -33,8 +56,21 @@ if (casper.cli.args.length) {
casper.exit(1);
}
// includes handling
if (casper.cli.has('includes')) {
includes = casper.cli.get('includes').split(',').map(function(include) {
// we can't use filter() directly because of abspath transformation
return checkIncludeFile(include);
}).filter(function(include) {
return utils.isString(include);
});
casper.test.includes = utils.unique(includes);
}
// test suites completion listener
casper.test.on('tests.complete', function() {
this.renderResults(true, undefined, casper.cli.get('xunit') || undefined);
});
// run all the suites
casper.test.runSuites.apply(casper.test, tests);
......
......@@ -90,4 +90,29 @@ t.comment('mergeObjects()');
});
})();
t.comment('unique()');
(function() {
testCases = [
{
input: [1,2,3],
output: [1,2,3]
},
{
input: [1,2,3,2,1],
output: [1,2,3]
},
{
input: ["foo", "bar", "foo"],
output: ["foo", "bar"]
},
{
input: [],
output: []
}
];
testCases.forEach(function(testCase) {
t.assertEquals(utils.unique(testCase.input), testCase.output, 'unique() computes unique values of an array');
});
})();
t.done();
......