async.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var falafel = require('../');
  2. var test = require('tape');
  3. test('array', function (t) {
  4. t.plan(5);
  5. var src = '(function () {'
  6. + 'var xs = [ 1, 2, [ 3, 4 ] ];'
  7. + 'var ys = [ 5, 6 ];'
  8. + 'g([ xs, ys ]);'
  9. + '})()';
  10. var pending = 0;
  11. var output = falafel(src, function (node) {
  12. if (node.type === 'ArrayExpression') {
  13. pending ++;
  14. setTimeout(function () {
  15. node.update('fn(' + node.source() + ')');
  16. if (--pending === 0) check();
  17. }, 50 * pending * 2);
  18. }
  19. });
  20. var arrays = [
  21. [ 3, 4 ],
  22. [ 1, 2, [ 3, 4 ] ],
  23. [ 5, 6 ],
  24. [ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ],
  25. ];
  26. function check () {
  27. Function([ 'fn', 'g' ], output)(
  28. function (xs) {
  29. t.same(arrays.shift(), xs);
  30. return xs;
  31. },
  32. function (xs) {
  33. t.same(xs, [ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]);
  34. }
  35. );
  36. }
  37. });