Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*jslint node: true, nomen: true, sloppy: true, newcap: true */
- var _ = require('underscore');
- /**
- * Crawl a batch
- * @param {Object} ctx A vows context
- * @param {String} ctx_name The name of this context
- */
- function crawl(ctx, ctx_name) {
- /* We're dealing with a sub-batch here! */
- if (_.isObject(ctx) && ctx.topic) {
- /* Splay each test into it's own test, with a copy of the topic */
- return _.reduce(ctx, function (sub_ctx, sub_value, sub_ctx_name) {
- if (sub_ctx_name === 'topic') {
- return sub_ctx;
- }
- /* Crawl sub-context, duplicated this test for each one */
- _.each(crawl(sub_value, sub_ctx_name), function (sub_sub_vow) {
- var obj = {topic: ctx.topic};
- obj[sub_ctx_name] = sub_sub_vow;
- sub_ctx.push(obj);
- });
- return sub_ctx;
- }, []);
- }
- /**
- * Return the function wrapped in an array to make the return value
- * similar to as if it were an object that was crawled.
- */
- if (_.isFunction(ctx)) {
- var obj = {};
- obj[ctx_name] = ctx;
- return [obj];
- }
- throw new Error('Not sure how to parallelize this type');
- }
- /**
- * Topics are shared in vows' batches, so instead use topics as a separate setup
- * function for every new test.
- *
- * @param {Object} batch A vows batch object
- * @return {Object} Parallelize version of the batch
- */
- exports.parallelize = function (batch) {
- /**
- * Top level contexts in a batch are already parallelized, so let's crawl
- * sub-contexts to parallelize them.
- */
- return _.reduce(batch, function (parallel_batch, ctx, ctx_name) {
- _.each(crawl(ctx), function (vow, index) {
- /**
- * Can't have duplicate context names at the top level, so prepend them
- * with a number.
- */
- parallel_batch['#' + (index + 1) + ' ' + ctx_name] = vow;
- });
- return parallel_batch;
- }, {});
- };
Add Comment
Please, Sign In to add comment