Guest User

Untitled

a guest
Oct 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. /*jslint node: true, nomen: true, sloppy: true, newcap: true */
  2.  
  3. var _ = require('underscore');
  4.  
  5. /**
  6. * Crawl a batch
  7. * @param {Object} ctx A vows context
  8. * @param {String} ctx_name The name of this context
  9. */
  10. function crawl(ctx, ctx_name) {
  11. /* We're dealing with a sub-batch here! */
  12. if (_.isObject(ctx) && ctx.topic) {
  13. /* Splay each test into it's own test, with a copy of the topic */
  14. return _.reduce(ctx, function (sub_ctx, sub_value, sub_ctx_name) {
  15. if (sub_ctx_name === 'topic') {
  16. return sub_ctx;
  17. }
  18. /* Crawl sub-context, duplicated this test for each one */
  19. _.each(crawl(sub_value, sub_ctx_name), function (sub_sub_vow) {
  20. var obj = {topic: ctx.topic};
  21. obj[sub_ctx_name] = sub_sub_vow;
  22. sub_ctx.push(obj);
  23. });
  24. return sub_ctx;
  25. }, []);
  26. }
  27.  
  28. /**
  29. * Return the function wrapped in an array to make the return value
  30. * similar to as if it were an object that was crawled.
  31. */
  32. if (_.isFunction(ctx)) {
  33. var obj = {};
  34. obj[ctx_name] = ctx;
  35. return [obj];
  36. }
  37.  
  38. throw new Error('Not sure how to parallelize this type');
  39. }
  40.  
  41. /**
  42. * Topics are shared in vows' batches, so instead use topics as a separate setup
  43. * function for every new test.
  44. *
  45. * @param {Object} batch A vows batch object
  46. * @return {Object} Parallelize version of the batch
  47. */
  48. exports.parallelize = function (batch) {
  49. /**
  50. * Top level contexts in a batch are already parallelized, so let's crawl
  51. * sub-contexts to parallelize them.
  52. */
  53. return _.reduce(batch, function (parallel_batch, ctx, ctx_name) {
  54. _.each(crawl(ctx), function (vow, index) {
  55. /**
  56. * Can't have duplicate context names at the top level, so prepend them
  57. * with a number.
  58. */
  59. parallel_batch['#' + (index + 1) + ' ' + ctx_name] = vow;
  60. });
  61. return parallel_batch;
  62. }, {});
  63. };
Add Comment
Please, Sign In to add comment