Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Convert to UMD modules

- removed browser global support, modules all the way down
- restored browser based buster tests

Browser based buster tests will fail until
busterjs/buster#348 is resolved and released.
With the proposed patch applied the browser tests run as expected.
  • Loading branch information...
commit 07817c45cd920c46089c6fe41b62e1b743ff1d44 1 parent 4fcb81f
@scothis scothis authored
Showing with 3,970 additions and 3,553 deletions.
  1. +3 −7 apply.js
  2. +8 −9 callbacks.js
  3. +6 −8 cancelable.js
  4. +6 −9 debug.js
  5. +7 −9 delay.js
  6. +8 −9 function.js
  7. +7 −9 keys.js
  8. +8 −9 node/function.js
  9. +2 −1  package.json
  10. +8 −8 parallel.js
  11. +8 −8 pipeline.js
  12. +10 −9 poll.js
  13. +8 −8 sequence.js
  14. +101 −87 test/all-test.js
  15. +101 −87 test/any-test.js
  16. +29 −14 test/apply-test.js
  17. +21 −21 test/buster.js
  18. +301 −286 test/callbacks-test.js
  19. +91 −76 test/cancelable-test.js
  20. +11 −0 test/curl-config.js
  21. +70 −56 test/debug-test.js
  22. +395 −380 test/defer-test.js
  23. +85 −69 test/delay-test.js
  24. +237 −222 test/function-test.js
  25. +62 −47 test/isPromise-test.js
  26. +68 −54 test/join-test.js
  27. +157 −142 test/keys-test.js
  28. +124 −109 test/map-test.js
  29. +228 −212 test/node/function-test.js
  30. +47 −32 test/parallel-test.js
  31. +57 −42 test/pipeline-test.js
  32. +133 −118 test/poll-test.js
  33. +579 −564 test/promise-test.js
  34. +195 −181 test/reduce-test.js
  35. +59 −46 test/reject-test.js
  36. +131 −116 test/resolve-test.js
  37. +21 −0 test/run.js
  38. +40 −25 test/sequence-test.js
  39. +102 −88 test/some-test.js
  40. +61 −45 test/timeout-test.js
  41. +144 −128 test/unfold-test.js
  42. +44 −28 test/unfold/list-test.js
  43. +151 −135 test/when-test.js
  44. +9 −8 timed.js
  45. +7 −9 timeout.js
  46. +8 −8 unfold.js
  47. +9 −8 unfold/list.js
  48. +3 −7 when.js
View
10 apply.js
@@ -58,13 +58,9 @@ define(function() {
};
});
-})(typeof define == 'function'
- ? define
- : function (factory) { typeof module != 'undefined'
- ? (module.exports = factory())
- : (this.when_apply = factory());
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
17 callbacks.js
@@ -11,9 +11,12 @@
*/
(function(define) {
-define(['./when'], function(when) {
+define(function(require) {
- var slice = [].slice;
+ var when, slice;
+
+ when = require('./when');
+ slice = [].slice;
return {
apply: apply,
@@ -257,11 +260,7 @@ define(['./when'], function(when) {
};
}
});
-})(typeof define == 'function'
- ? define
- : function (deps, factory) { typeof module != 'undefined'
- ? (module.exports = factory(require('./when')))
- : (this.when_callback = factory(this.when));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
14 cancelable.js
@@ -16,7 +16,9 @@
*/
(function(define) {
-define(['./when'], function(when) {
+define(function(require) {
+
+ var when = require('./when');
/**
* Makes deferred cancelable, adding a cancel() method.
@@ -56,13 +58,9 @@ define(['./when'], function(when) {
};
});
-})(typeof define == 'function'
- ? define
- : function (deps, factory) { typeof module != 'undefined'
- ? (module.exports = factory(require('./when')))
- : (this.when_cancelable = factory(this.when));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
15 debug.js
@@ -34,10 +34,11 @@
* @author brian@hovercraftstudios.com
*/
(function(define) {
-define(['./when'], function(when) {
+define(function(require) {
- var promiseId, pending, exceptionsToRethrow, own, warn, undef;
+ var when, promiseId, pending, exceptionsToRethrow, own, warn, undef;
+ when = require('./when');
promiseId = 0;
pending = {};
own = Object.prototype.hasOwnProperty;
@@ -343,11 +344,7 @@ define(['./when'], function(when) {
}
});
-})(typeof define == 'function'
- ? define
- : function (deps, factory) { typeof exports != 'undefined'
- ? (module.exports = factory(require('./when')))
- : (this.when = factory(this.when));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
16 delay.js
@@ -11,9 +11,11 @@
*/
(function(define) {
-define(['./when'], function(when) {
+define(function(require) {
- var undef;
+ var when, undef;
+
+ when = require('./when');
/**
* Creates a new promise that will resolve after a msec delay. If promise
@@ -55,13 +57,9 @@ define(['./when'], function(when) {
};
});
-})(typeof define == 'function'
- ? define
- : function (deps, factory) { typeof module != 'undefined'
- ? (module.exports = factory(require('./when')))
- : (this.when_delay = factory(this.when));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
17 function.js
@@ -11,9 +11,12 @@
*/
(function(define) {
-define(['./when'], function(when) {
+define(function(require) {
- var slice = [].slice;
+ var when, slice;
+
+ when = require('./when');
+ slice = [].slice;
return {
apply: apply,
@@ -191,13 +194,9 @@ define(['./when'], function(when) {
}
});
-})(typeof define == 'function'
- ? define
- : function (deps, factory) { typeof exports != 'undefined'
- ? (module.exports = factory(require('./when')))
- : (this.when_function = factory(this.when));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
16 keys.js
@@ -8,8 +8,10 @@
* @author John Hann
*/
(function(define) { 'use strict';
-define(['./when'], function(when) {
- var keys, eachKey, owns;
+define(function(require) {
+ var when, keys, eachKey, owns;
+
+ when = require('./when');
// Public API
@@ -84,11 +86,7 @@ define(['./when'], function(when) {
function identity(x) { return x; }
});
-})(typeof define == 'function'
- ? define
- : function (deps, factory) { typeof module != 'undefined'
- ? (module.exports = factory(require('./when')))
- : (this.when_keys = factory(this.when));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
17 node/function.js
@@ -11,9 +11,12 @@
*/
(function(define) {
-define(['../when'], function(when) {
+define(function(require) {
- var slice = [].slice;
+ var when, slice;
+
+ when = require('../when');
+ slice = [].slice;
return {
apply: apply,
@@ -168,13 +171,9 @@ define(['../when'], function(when) {
}
});
-})(typeof define == 'function'
- ? define
- : function (deps, factory) { typeof exports != 'undefined'
- ? (module.exports = factory(require('../when')))
- : (this.when_node_function = factory(this.when));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
3  package.json
@@ -29,7 +29,8 @@
"devDependencies": {
"buster": "~0.6",
"jshint": "~1.1",
- "promises-aplus-tests": "~1"
+ "promises-aplus-tests": "~1",
+ "curl": "https://github.com/cujojs/curl/tarball/0.7.3"
},
"main": "when",
"directories": {
View
16 parallel.js
@@ -10,7 +10,11 @@
*/
(function(define) {
-define(['./when'], function(when) {
+define(function(require) {
+
+ var when;
+
+ when = require('./when');
/**
* Run array of tasks in parallel
@@ -28,13 +32,9 @@ define(['./when'], function(when) {
};
});
-})(typeof define == 'function' && define.amd
- ? define
- : function (deps, factory) { typeof exports == 'object'
- ? (module.exports = factory(require('./when')))
- : (this.when_parallel = factory(this.when));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
16 pipeline.js
@@ -11,7 +11,11 @@
*/
(function(define) {
-define(['./when'], function(when) {
+define(function(require) {
+
+ var when;
+
+ when = require('./when');
/**
* Run array of tasks in a pipeline where the next
@@ -45,13 +49,9 @@ define(['./when'], function(when) {
};
});
-})(typeof define == 'function' && define.amd
- ? define
- : function (deps, factory) { typeof exports == 'object'
- ? (module.exports = factory(require('./when')))
- : (this.when_pipeline = factory(this.when));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
19 poll.js
@@ -10,9 +10,14 @@
(function (define) {
'use strict';
-define(['./when', './cancelable', './delay', './function'], function(when, cancelable, delay, fn) {
+define(function(require) {
- var undef;
+ var when, cancelable, delay, fn, undef;
+
+ when = require('./when');
+ cancelable = require('./cancelable');
+ delay = require('./delay');
+ fn = require('./function');
/**
* Periodically execute the work function on the msec delay. The result of
@@ -120,11 +125,7 @@ define(['./when', './cancelable', './delay', './function'], function(when, cance
}
});
-})(typeof define == 'function' && define.amd
- ? define
- : function (deps, factory) { typeof exports == 'object'
- ? (module.exports = factory(require('./when'), require('./cancelable'), require('./delay'), require('./function')))
- : (this.when_poll = factory(this.when, this.when_cancelable, this.when_delay, this.when_function));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
16 sequence.js
@@ -10,7 +10,11 @@
*/
(function(define) {
-define(['./when'], function(when) {
+define(function(require) {
+
+ var when;
+
+ when = require('./when');
/**
* Run array of tasks in sequence with no overlap
@@ -31,13 +35,9 @@ define(['./when'], function(when) {
};
});
-})(typeof define == 'function' && define.amd
- ? define
- : function (deps, factory) { typeof exports == 'object'
- ? (module.exports = factory(require('./when')))
- : (this.when_sequence = factory(this.when));
- }
- // Boilerplate for AMD, Node, and browser global
+})(
+ typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
+ // Boilerplate for AMD and Node
);
View
188 test/all-test.js
@@ -1,95 +1,109 @@
-(function(buster, when) {
+(function(buster, define) {
-var assert, fail, resolved, rejected;
+var assert, fail;
assert = buster.assert;
fail = buster.assertions.fail;
-resolved = when.resolve;
-rejected = when.reject;
-
-buster.testCase('when.all', {
-
- 'should resolve empty input': function(done) {
- return when.all([],
- function(result) {
- assert.equals(result, []);
- },
- fail
- ).ensure(done);
- },
-
- 'should resolve values array': function(done) {
- var input = [1, 2, 3];
- when.all(input,
- function(results) {
- assert.equals(results, input);
- },
- fail
- ).ensure(done);
- },
-
- 'should resolve promises array': function(done) {
- var input = [resolved(1), resolved(2), resolved(3)];
- when.all(input,
- function(results) {
- assert.equals(results, [1, 2, 3]);
- },
- fail
- ).ensure(done);
- },
-
- 'should resolve sparse array input': function(done) {
- var input = [, 1, , 1, 1 ];
- when.all(input,
- function(results) {
- assert.equals(results, input);
- },
- fail
- ).ensure(done);
- },
-
- 'should reject if any input promise rejects': function(done) {
- var input = [resolved(1), rejected(2), resolved(3)];
- when.all(input,
- fail,
- function(failed) {
- assert.equals(failed, 2);
- }
- ).ensure(done);
- },
-
- 'should throw if called with something other than a valid input plus callbacks': function() {
- assert.exception(function() {
- when.all(1, 2, 3);
- });
- },
-
- 'should accept a promise for an array': function(done) {
- var expected, input;
-
- expected = [1, 2, 3];
- input = resolved(expected);
-
- when.all(input,
- function(results) {
- assert.equals(results, expected);
- },
- fail
- ).ensure(done);
- },
-
- 'should resolve to empty array when input promise does not resolve to array': function(done) {
- when.all(resolved(1),
- function(result) {
- assert.equals(result, []);
- },
- fail
- ).ensure(done);
- }
+define('when.all-test', function (require) {
+
+ var when, resolved, rejected;
+
+ when = require('when');
+ resolved = when.resolve;
+ rejected = when.reject;
+
+ buster.testCase('when.all', {
+
+ 'should resolve empty input': function(done) {
+ return when.all([],
+ function(result) {
+ assert.equals(result, []);
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should resolve values array': function(done) {
+ var input = [1, 2, 3];
+ when.all(input,
+ function(results) {
+ assert.equals(results, input);
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should resolve promises array': function(done) {
+ var input = [resolved(1), resolved(2), resolved(3)];
+ when.all(input,
+ function(results) {
+ assert.equals(results, [1, 2, 3]);
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should resolve sparse array input': function(done) {
+ var input = [, 1, , 1, 1 ];
+ when.all(input,
+ function(results) {
+ assert.equals(results, input);
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should reject if any input promise rejects': function(done) {
+ var input = [resolved(1), rejected(2), resolved(3)];
+ when.all(input,
+ fail,
+ function(failed) {
+ assert.equals(failed, 2);
+ }
+ ).ensure(done);
+ },
+
+ 'should throw if called with something other than a valid input plus callbacks': function() {
+ assert.exception(function() {
+ when.all(1, 2, 3);
+ });
+ },
+
+ 'should accept a promise for an array': function(done) {
+ var expected, input;
+
+ expected = [1, 2, 3];
+ input = resolved(expected);
+
+ when.all(input,
+ function(results) {
+ assert.equals(results, expected);
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should resolve to empty array when input promise does not resolve to array': function(done) {
+ when.all(resolved(1),
+ function(result) {
+ assert.equals(result, []);
+ },
+ fail
+ ).ensure(done);
+ }
+ });
+
});
-})(
+}(
this.buster || require('buster'),
- this.when || require('..')
-);
+ typeof define === 'function' && define.amd ? define : function (id, factory) {
+ var packageName = id.split(/[\/\-\.]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
+ pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
+ factory(function (moduleId) {
+ return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
+ });
+ }
+ // Boilerplate for AMD and Node
+));
View
188 test/any-test.js
@@ -1,6 +1,6 @@
-(function(buster, when) {
+(function(buster, define) {
-var assert, refute, fail, resolved, rejected;
+var assert, refute, fail;
assert = buster.assert;
refute = buster.refute;
@@ -16,92 +16,106 @@ function contains(array, item) {
return false;
}
-resolved = when.resolve;
-rejected = when.reject;
-
-buster.testCase('when.any', {
-
- 'should resolve to undefined with empty input array': function(done) {
- when.any([],
- function(result) {
- refute.defined(result);
- },
- fail
- ).ensure(done);
- },
-
- 'should resolve with an input value': function(done) {
- var input = [1, 2, 3];
- when.any(input,
- function(result) {
- assert(contains(input, result));
- },
- fail
- ).ensure(done);
- },
-
- 'should resolve with a promised input value': function(done) {
- var input = [resolved(1), resolved(2), resolved(3)];
- when.any(input,
- function(result) {
- assert(contains([1, 2, 3], result));
- },
- fail
- ).ensure(done);
- },
-
- 'should reject with all rejected input values if all inputs are rejected': function(done) {
- var input = [rejected(1), rejected(2), rejected(3)];
- when.any(input,
- fail,
- function(result) {
- assert.equals(result, [1, 2, 3]);
- }
- ).ensure(done);
- },
-
- 'should throw if called with something other than a valid input plus callbacks': function() {
- assert.exception(function() {
- when.any(1, 2, 3);
- });
- },
-
- 'should accept a promise for an array': function(done) {
- var expected, input;
-
- expected = [1, 2, 3];
- input = resolved(expected);
-
- when.any(input,
- function(result) {
- refute.equals(expected.indexOf(result), -1);
- },
- fail
- ).ensure(done);
- },
-
- 'should allow zero handlers': function(done) {
- var input = [1, 2, 3];
- when.any(input).then(
- function(result) {
- assert(contains(input, result));
- },
- fail
- ).ensure(done);
- },
-
- 'should resolve to undefined when input promise does not resolve to array': function(done) {
- when.any(resolved(1),
- function(result) {
- refute.defined(result);
- },
- fail
- ).ensure(done);
- }
+define('when.any-test', function (require) {
+
+ var when, resolved, rejected;
+
+ when = require('when');
+ resolved = when.resolve;
+ rejected = when.reject;
+
+ buster.testCase('when.any', {
+
+ 'should resolve to undefined with empty input array': function(done) {
+ when.any([],
+ function(result) {
+ refute.defined(result);
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should resolve with an input value': function(done) {
+ var input = [1, 2, 3];
+ when.any(input,
+ function(result) {
+ assert(contains(input, result));
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should resolve with a promised input value': function(done) {
+ var input = [resolved(1), resolved(2), resolved(3)];
+ when.any(input,
+ function(result) {
+ assert(contains([1, 2, 3], result));
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should reject with all rejected input values if all inputs are rejected': function(done) {
+ var input = [rejected(1), rejected(2), rejected(3)];
+ when.any(input,
+ fail,
+ function(result) {
+ assert.equals(result, [1, 2, 3]);
+ }
+ ).ensure(done);
+ },
+
+ 'should throw if called with something other than a valid input plus callbacks': function() {
+ assert.exception(function() {
+ when.any(1, 2, 3);
+ });
+ },
+
+ 'should accept a promise for an array': function(done) {
+ var expected, input;
+
+ expected = [1, 2, 3];
+ input = resolved(expected);
+
+ when.any(input,
+ function(result) {
+ refute.equals(expected.indexOf(result), -1);
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should allow zero handlers': function(done) {
+ var input = [1, 2, 3];
+ when.any(input).then(
+ function(result) {
+ assert(contains(input, result));
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should resolve to undefined when input promise does not resolve to array': function(done) {
+ when.any(resolved(1),
+ function(result) {
+ refute.defined(result);
+ },
+ fail
+ ).ensure(done);
+ }
+
+ });
});
-})(
+}(
this.buster || require('buster'),
- this.when || require('..')
-);
+ typeof define === 'function' && define.amd ? define : function (id, factory) {
+ var packageName = id.split(/[\/\-\.]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
+ pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
+ factory(function (moduleId) {
+ return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
+ });
+ }
+ // Boilerplate for AMD and Node
+));
View
43 test/apply-test.js
@@ -1,4 +1,4 @@
-(function(buster, apply) {
+(function(buster, define) {
var assert = buster.assert;
@@ -14,19 +14,34 @@ function f() {
return sum;
}
-buster.testCase('when/apply', {
- 'should spread array onto arguments': function() {
- assert.equals(6, apply(f)([1,2,3]));
- },
+define('when/apply-test', function (require) {
+
+ var apply;
+
+ apply = require('when/apply');
+
+ buster.testCase('when/apply', {
+ 'should spread array onto arguments': function() {
+ assert.equals(6, apply(f)([1,2,3]));
+ },
+
+ 'should fail for non Array-like input': function() {
+ assert.exception(function() {
+ apply(f)(1,2,3);
+ });
+ }
+ });
- 'should fail for non Array-like input': function() {
- assert.exception(function() {
- apply(f)(1,2,3);
- });
- }
});
-})(
- this.buster || require('buster'),
- this.when_apply || require('../apply')
-);
+}(
+ this.buster || require('buster'),
+ typeof define === 'function' && define.amd ? define : function (id, factory) {
+ var packageName = id.split(/[\/\-\.]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
+ pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
+ factory(function (moduleId) {
+ return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
+ });
+ }
+ // Boilerplate for AMD and Node
+));
View
42 test/buster.js
@@ -1,36 +1,36 @@
(function() {
-var tests, config;
-
-tests = ['test/**/*-test.js'];
-config = {};
+var config = {};
config.node = {
environment: 'node',
rootPath: '../',
- tests: tests
+ tests: [
+ 'test/**/*-test.js'
+ ]
};
config.browser = {
environment: 'browser',
+ autoRun: false,
rootPath: '../',
- tests: tests,
+ resources: [
+ //'**', ** is busted in buster
+ '*.js',
+ 'node/**/*.js',
+ 'unfold/**/*.js',
+ 'node_modules/curl/**/*.js'
+ ],
+ libs: [
+ 'test/curl-config.js',
+ 'node_modules/curl/src/curl.js'
+ ],
sources: [
- 'when.js',
- 'apply.js',
- 'delay.js',
- 'timeout.js',
- 'cancelable.js',
- 'sequence.js',
- 'pipeline.js',
- 'parallel.js',
- 'callbacks.js',
- 'function.js',
- 'keys.js',
- 'poll.js',
- 'unfold.js',
- 'unfold/list.js',
- 'node/function.js'
+ // loaded as resources
+ ],
+ tests: [
+ 'test/**/*-test.js',
+ 'test/run.js'
]
};
View
587 test/callbacks-test.js
@@ -1,4 +1,4 @@
-(function(buster, when, callbacks) {
+(function(buster, define) {
var assert, fail, sentinel;
@@ -7,379 +7,394 @@ fail = buster.fail;
sentinel = {};
-function assertIsPromise(arg) {
- assert(when.isPromise(arg));
-}
+define('when/callbacks-test', function (require) {
-buster.testCase('when/callbacks', {
- 'apply': {
- 'should return a promise': function() {
- assertIsPromise(callbacks.apply(function() {}));
- },
-
- 'should resolve with the callback arguments': function(done) {
- var promise = callbacks.apply(function(cb) {
- cb(sentinel);
- });
-
- promise.then(function(val) {
- assert.same(val, sentinel);
- }, fail).ensure(done);
- },
-
- 'should reject with the errback arguments': function(done) {
- var promise = callbacks.apply(function(cb, eb){
- eb(sentinel);
- });
-
- promise.then(fail, function(reason) {
- assert.same(reason, sentinel);
- }).ensure(done);
- },
-
- 'should turn exceptions into rejections': function(done) {
- var error = new Error();
- var promise = callbacks.apply(function(){
- throw error;
- });
-
- promise.then(fail, function(reason) {
- assert.equals(reason, error);
- }).ensure(done);
- },
-
- 'should forward its second argument to the function': function(done) {
- var async = function(a, b, cb/*, eb*/) {
- cb(a + b);
- };
-
- var promise = callbacks.apply(async, [10, 15]);
-
- promise.then(function(result) {
- assert.equals(result, 25);
- }, fail).ensure(done);
- },
-
- 'should turn multiple callback values into an array': function(done) {
- var async = function(a, b, cb/*, eb*/) {
- cb(a * 10, b * 20);
- };
-
- var promise = callbacks.apply(async, [10, 20]);
- promise.then(function(results) {
- assert.equals(results, [100, 400]);
- }, fail).ensure(done);
- },
+ var callbacks, when;
- 'should accept promises on the extra arguments': function(done) {
- var async = function(a, b, cb/*, eb*/) {
- cb(a + b);
- };
+ callbacks = require('when/callbacks');
+ when = require('when');
- var promise = callbacks.apply(async, [when(10), 15]);
-
- promise.then(function(result) {
- assert.equals(result, 25);
- }, fail).ensure(done);
- }
- },
-
- 'call': {
- 'should return a promise': function() {
- assertIsPromise(callbacks.call(function() {}));
- },
-
- 'should resolve with the callback arguments': function(done) {
- var promise = callbacks.apply(function(cb) {
- cb(sentinel);
- });
+ function assertIsPromise(arg) {
+ assert(when.isPromise(arg));
+ }
- promise.then(function(val) {
- assert.same(val, sentinel);
- }, fail).ensure(done);
- },
+ buster.testCase('when/callbacks', {
+ 'apply': {
+ 'should return a promise': function() {
+ assertIsPromise(callbacks.apply(function() {}));
+ },
- 'should reject with the errback arguments': function(done) {
- var promise = callbacks.apply(function(cb, eb){
- eb(sentinel);
- });
+ 'should resolve with the callback arguments': function(done) {
+ var promise = callbacks.apply(function(cb) {
+ cb(sentinel);
+ });
- promise.then(fail, function(reason) {
- assert.same(reason, sentinel);
- }).ensure(done);
- },
+ promise.then(function(val) {
+ assert.same(val, sentinel);
+ }, fail).ensure(done);
+ },
- 'should turn exceptions into rejections': function(done) {
- var error = new Error();
- var promise = callbacks.call(function(){
- throw error;
- });
+ 'should reject with the errback arguments': function(done) {
+ var promise = callbacks.apply(function(cb, eb){
+ eb(sentinel);
+ });
- promise.then(fail, function(reason) {
- assert.equals(reason, error);
- }).ensure(done);
- },
+ promise.then(fail, function(reason) {
+ assert.same(reason, sentinel);
+ }).ensure(done);
+ },
- 'should forward its extra arguments to the function': function(done) {
- var async = function(a, b, cb/*, eb*/) {
- cb(a + b);
- };
+ 'should turn exceptions into rejections': function(done) {
+ var error = new Error();
+ var promise = callbacks.apply(function(){
+ throw error;
+ });
- var promise = callbacks.call(async, 10, 15);
+ promise.then(fail, function(reason) {
+ assert.equals(reason, error);
+ }).ensure(done);
+ },
- promise.then(function(result) {
- assert.equals(result, 25);
- }, fail).ensure(done);
- },
+ 'should forward its second argument to the function': function(done) {
+ var async = function(a, b, cb/*, eb*/) {
+ cb(a + b);
+ };
- 'should turn multiple callback values into an array': function(done) {
- var async = function(a, b, cb/*, eb*/) {
- cb(a * 10, b * 20);
- };
+ var promise = callbacks.apply(async, [10, 15]);
- var promise = callbacks.call(async, 10, 20);
- promise.then(function(results) {
- assert.equals(results, [100, 400]);
- }, fail).ensure(done);
- },
+ promise.then(function(result) {
+ assert.equals(result, 25);
+ }, fail).ensure(done);
+ },
- 'should accept promises on the extra arguments': function(done) {
- var async = function(a, b, cb/*, eb*/) {
- cb(a + b);
- };
+ 'should turn multiple callback values into an array': function(done) {
+ var async = function(a, b, cb/*, eb*/) {
+ cb(a * 10, b * 20);
+ };
- var promise = callbacks.call(async, when(10), 15);
+ var promise = callbacks.apply(async, [10, 20]);
+ promise.then(function(results) {
+ assert.equals(results, [100, 400]);
+ }, fail).ensure(done);
+ },
- promise.then(function(result) {
- assert.equals(result, 25);
- }, fail).ensure(done);
- }
- },
+ 'should accept promises on the extra arguments': function(done) {
+ var async = function(a, b, cb/*, eb*/) {
+ cb(a + b);
+ };
- 'bind': {
- 'should be an alias for lift': function() {
- assert.same(callbacks.bind, callbacks.lift);
- }
- },
+ var promise = callbacks.apply(async, [when(10), 15]);
- 'lift': {
- 'should return a function': function() {
- assert.isFunction(callbacks.lift(function() {}));
+ promise.then(function(result) {
+ assert.equals(result, 25);
+ }, fail).ensure(done);
+ }
},
- 'the returned function': {
+ 'call': {
'should return a promise': function() {
- var result = callbacks.lift(function() {});
- assertIsPromise(result());
- },
-
- 'should resolve the promise with the callback value': function(done) {
- var result = callbacks.lift(function(cb) {
- cb(10);
- });
-
- result().then(function(value) {
- assert.equals(value, 10);
- }, fail).ensure(done);
+ assertIsPromise(callbacks.call(function() {}));
},
- 'should forward arguments to the original function': function(done) {
- var result = callbacks.lift(function(a, b, cb) {
- cb(a + b);
+ 'should resolve with the callback arguments': function(done) {
+ var promise = callbacks.apply(function(cb) {
+ cb(sentinel);
});
- result(10, 15).then(function(value) {
- assert.equals(value, 25);
+ promise.then(function(val) {
+ assert.same(val, sentinel);
}, fail).ensure(done);
},
- 'should reject the promise with the errback value': function(done) {
- var error = new Error();
- var result = callbacks.lift(function(cb, eb) {
- eb(error);
+ 'should reject with the errback arguments': function(done) {
+ var promise = callbacks.apply(function(cb, eb){
+ eb(sentinel);
});
- result().then(fail, function(reason) {
- assert.same(reason, error);
+ promise.then(fail, function(reason) {
+ assert.same(reason, sentinel);
}).ensure(done);
},
'should turn exceptions into rejections': function(done) {
var error = new Error();
- var result = callbacks.lift(function(){
+ var promise = callbacks.call(function(){
throw error;
});
- result().then(fail, function(reason) {
+ promise.then(fail, function(reason) {
assert.equals(reason, error);
}).ensure(done);
},
+ 'should forward its extra arguments to the function': function(done) {
+ var async = function(a, b, cb/*, eb*/) {
+ cb(a + b);
+ };
+
+ var promise = callbacks.call(async, 10, 15);
+
+ promise.then(function(result) {
+ assert.equals(result, 25);
+ }, fail).ensure(done);
+ },
+
'should turn multiple callback values into an array': function(done) {
- var result = callbacks.lift(function(a, b, cb/*, eb*/) {
+ var async = function(a, b, cb/*, eb*/) {
cb(a * 10, b * 20);
- });
+ };
- result(10, 20).then(function(results) {
+ var promise = callbacks.call(async, 10, 20);
+ promise.then(function(results) {
assert.equals(results, [100, 400]);
}, fail).ensure(done);
},
- 'should accept promises as arguments': function(done) {
- var result = callbacks.lift(function(a, b, cb/*, eb*/) {
+ 'should accept promises on the extra arguments': function(done) {
+ var async = function(a, b, cb/*, eb*/) {
cb(a + b);
- });
+ };
- result(when(10), 15).then(function(result) {
+ var promise = callbacks.call(async, when(10), 15);
+
+ promise.then(function(result) {
assert.equals(result, 25);
}, fail).ensure(done);
}
},
- 'should accept leading arguments': function(done) {
- function fancySum(x, y, callback) {
- callback(x + y);
+ 'bind': {
+ 'should be an alias for lift': function() {
+ assert.same(callbacks.bind, callbacks.lift);
}
+ },
- var partiallyApplied = callbacks.lift(fancySum, 5);
+ 'lift': {
+ 'should return a function': function() {
+ assert.isFunction(callbacks.lift(function() {}));
+ },
- partiallyApplied(10).then(function(value) {
- assert.equals(value, 15);
- }, fail).ensure(done);
- },
+ 'the returned function': {
+ 'should return a promise': function() {
+ var result = callbacks.lift(function() {});
+ assertIsPromise(result());
+ },
+
+ 'should resolve the promise with the callback value': function(done) {
+ var result = callbacks.lift(function(cb) {
+ cb(10);
+ });
+
+ result().then(function(value) {
+ assert.equals(value, 10);
+ }, fail).ensure(done);
+ },
+
+ 'should forward arguments to the original function': function(done) {
+ var result = callbacks.lift(function(a, b, cb) {
+ cb(a + b);
+ });
+
+ result(10, 15).then(function(value) {
+ assert.equals(value, 25);
+ }, fail).ensure(done);
+ },
+
+ 'should reject the promise with the errback value': function(done) {
+ var error = new Error();
+ var result = callbacks.lift(function(cb, eb) {
+ eb(error);
+ });
+
+ result().then(fail, function(reason) {
+ assert.same(reason, error);
+ }).ensure(done);
+ },
+
+ 'should turn exceptions into rejections': function(done) {
+ var error = new Error();
+ var result = callbacks.lift(function(){
+ throw error;
+ });
+
+ result().then(fail, function(reason) {
+ assert.equals(reason, error);
+ }).ensure(done);
+ },
+
+ 'should turn multiple callback values into an array': function(done) {
+ var result = callbacks.lift(function(a, b, cb/*, eb*/) {
+ cb(a * 10, b * 20);
+ });
+
+ result(10, 20).then(function(results) {
+ assert.equals(results, [100, 400]);
+ }, fail).ensure(done);
+ },
+
+ 'should accept promises as arguments': function(done) {
+ var result = callbacks.lift(function(a, b, cb/*, eb*/) {
+ cb(a + b);
+ });
+
+ result(when(10), 15).then(function(result) {
+ assert.equals(result, 25);
+ }, fail).ensure(done);
+ }
+ },
- 'should accept promises as leading arguments': function(done) {
- function fancySum(x, y, callback) {
- callback(x + y);
- }
+ 'should accept leading arguments': function(done) {
+ function fancySum(x, y, callback) {
+ callback(x + y);
+ }
- var partiallyApplied = callbacks.lift(fancySum, when(5));
+ var partiallyApplied = callbacks.lift(fancySum, 5);
- partiallyApplied(10).then(function(value) {
- assert.equals(value, 15);
- }, fail).ensure(done);
- }
- },
+ partiallyApplied(10).then(function(value) {
+ assert.equals(value, 15);
+ }, fail).ensure(done);
+ },
- 'promisify': {
- 'should support callbacks in any position': function(done) {
- function weirdAsync(a, callback, b) {
- callback(a + b);
- }
+ 'should accept promises as leading arguments': function(done) {
+ function fancySum(x, y, callback) {
+ callback(x + y);
+ }
- var promisified = callbacks.promisify(weirdAsync, {
- callback: 1
- });
+ var partiallyApplied = callbacks.lift(fancySum, when(5));
- promisified(10, 5).then(function(result) {
- assert.equals(result, 15);
- }, fail).ensure(done);
+ partiallyApplied(10).then(function(value) {
+ assert.equals(value, 15);
+ }, fail).ensure(done);
+ }
},
- 'should support errbacks in any position': function(done) {
- function weirdAsync(errback, a, callback, b) {
- errback(a + b);
- }
+ 'promisify': {
+ 'should support callbacks in any position': function(done) {
+ function weirdAsync(a, callback, b) {
+ callback(a + b);
+ }
- var promisified = callbacks.promisify(weirdAsync, {
- callback: 2,
- errback: 0
- });
+ var promisified = callbacks.promisify(weirdAsync, {
+ callback: 1
+ });
- promisified(10, 5).then(fail, function(reason) {
- assert.equals(reason, 15);
- }).ensure(done);
- },
+ promisified(10, 5).then(function(result) {
+ assert.equals(result, 15);
+ }, fail).ensure(done);
+ },
- 'should turn multiple callback values into an array': function(done) {
- function invert(cb, eb, a, b) {
- cb(b, a);
- }
+ 'should support errbacks in any position': function(done) {
+ function weirdAsync(errback, a, callback, b) {
+ errback(a + b);
+ }
- var promisified = callbacks.promisify(invert, {
- callback: 0,
- errback: 1
- });
+ var promisified = callbacks.promisify(weirdAsync, {
+ callback: 2,
+ errback: 0
+ });
- promisified(10, 20).then(function(results) {
- assert.equals(results, [20, 10]);
- }, fail).ensure(done);
- },
+ promisified(10, 5).then(fail, function(reason) {
+ assert.equals(reason, 15);
+ }).ensure(done);
+ },
- 'should turn exceptions into rejections': function(done) {
- var error = new Error();
- var result = callbacks.promisify(function(){
- throw error;
- }, {});
+ 'should turn multiple callback values into an array': function(done) {
+ function invert(cb, eb, a, b) {
+ cb(b, a);
+ }
- result().then(fail, function(reason) {
- assert.equals(reason, error);
- }).ensure(done);
- },
+ var promisified = callbacks.promisify(invert, {
+ callback: 0,
+ errback: 1
+ });
- 'should accept promises as arguments': function(done) {
- var result = callbacks.promisify(function(a, b, cb/*, eb*/) {
- cb(a + b);
- }, {
- callback: -2,
- errback: -1
- });
-
- result(when(10), 15).then(function(result) {
- assert.equals(result, 25);
- }, fail).ensure(done);
- },
+ promisified(10, 20).then(function(results) {
+ assert.equals(results, [20, 10]);
+ }, fail).ensure(done);
+ },
- 'should understand -1 as "the last argument"': function(done) {
- function asyncSum(/*n1, n2, n3...callback*/) {
- var args = [].slice.call(arguments, 0);
+ 'should turn exceptions into rejections': function(done) {
+ var error = new Error();
+ var result = callbacks.promisify(function(){
+ throw error;
+ }, {});
- var callback = args.pop();
+ result().then(fail, function(reason) {
+ assert.equals(reason, error);
+ }).ensure(done);
+ },
- var result = args.reduce(function(prev, n) {
- return prev + n;
+ 'should accept promises as arguments': function(done) {
+ var result = callbacks.promisify(function(a, b, cb/*, eb*/) {
+ cb(a + b);
+ }, {
+ callback: -2,
+ errback: -1
});
- callback(result);
- }
- var promisified = callbacks.promisify(asyncSum, {
- callback: -1
- });
+ result(when(10), 15).then(function(result) {
+ assert.equals(result, 25);
+ }, fail).ensure(done);
+ },
- promisified(5, 10, 15).then(function(result) {
- assert.equals(result, 30);
- }, fail).ensure(done);
- },
+ 'should understand -1 as "the last argument"': function(done) {
+ function asyncSum(/*n1, n2, n3...callback*/) {
+ var args = [].slice.call(arguments, 0);
- 'should understand -2 as "the penultimate argument"': function(done) {
- function asyncConcat(/*str1, str2, str3...errback, callback*/) {
- var args = [].slice.call(arguments, 0);
+ var callback = args.pop();
- /*var callback =*/ args.pop();
- var errback = args.pop();
+ var result = args.reduce(function(prev, n) {
+ return prev + n;
+ });
+ callback(result);
+ }
- var result = args.reduce(function(prev, n) {
- return prev + ' ' + n;
+ var promisified = callbacks.promisify(asyncSum, {
+ callback: -1
});
- errback(result);
- }
+ promisified(5, 10, 15).then(function(result) {
+ assert.equals(result, 30);
+ }, fail).ensure(done);
+ },
+
+ 'should understand -2 as "the penultimate argument"': function(done) {
+ function asyncConcat(/*str1, str2, str3...errback, callback*/) {
+ var args = [].slice.call(arguments, 0);
+
+ /*var callback =*/ args.pop();
+ var errback = args.pop();
- var promisified = callbacks.promisify(asyncConcat, {
- errback: -2,
- callback: -1
- });
+ var result = args.reduce(function(prev, n) {
+ return prev + ' ' + n;
+ });
- var promise = promisified('That\'s', 'an', 'extreme', 'example');
- promise.then(fail, function(reason) {
- assert.equals(reason, 'That\'s an extreme example');
- }).ensure(done);
+ errback(result);
+ }
+
+ var promisified = callbacks.promisify(asyncConcat, {
+ errback: -2,
+ callback: -1
+ });
+
+ var promise = promisified('That\'s', 'an', 'extreme', 'example');
+ promise.then(fail, function(reason) {
+ assert.equals(reason, 'That\'s an extreme example');
+ }).ensure(done);
+ }
}
- }
+ });
+
});
-})(
- this.buster || require('buster'),
- this.when || require('..'),
- this.when_callback || require('../callbacks')
-);
+}(
+ this.buster || require('buster'),
+ typeof define === 'function' && define.amd ? define : function (id, factory) {
+ var packageName = id.split(/[\/\-\.]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
+ pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
+ factory(function (moduleId) {
+ return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
+ });
+ }
+ // Boilerplate for AMD and Node
+));
View
167 test/cancelable-test.js
@@ -1,4 +1,4 @@
-(function(buster, when, cancelable) {
+(function(buster, define) {
var assert, fail, sentinel, other;
@@ -8,82 +8,97 @@ fail = buster.assertions.fail;
sentinel = {};
other = {};
-buster.testCase('when/cancelable', {
- 'should decorate deferred with a cancel() method': function() {
- var c = cancelable(when.defer(), function() {});
- assert(typeof c.cancel == 'function');
- },
-
- 'should propagate a rejection when a cancelable deferred is canceled': function(done) {
- var c = cancelable(when.defer(), function() { return sentinel; });
- c.cancel();
-
- c.promise.then(
- fail,
- function(v) {
- assert.equals(v, sentinel);
- }
- ).ensure(done);
- },
-
- 'should return a promise for canceled value when canceled': function(done) {
- var c, promise;
-
- c = cancelable(when.defer(), function() { return sentinel; });
- promise = c.cancel();
-
- promise.then(
- fail,
- function(v) {
- assert.equals(v, sentinel);
- }
- ).ensure(done);
- },
-
- 'should not invoke canceler when rejected normally': function(done) {
- var c = cancelable(when.defer(), function() { return other; });
- c.reject(sentinel);
- c.cancel();
-
- c.promise.then(
- fail,
- function(v) {
- assert.equals(v, sentinel);
- }
- ).ensure(done);
- },
-
- 'should propagate the unaltered resolution value': function(done) {
- var c = cancelable(when.defer(), function() { return other; });
- c.resolve(sentinel);
- c.cancel();
-
- c.promise.then(
- function(val) {
- assert.same(val, sentinel);
- },
- function(e) {
- console.error(e);
- fail(e);
- }
- ).ensure(done);
- },
-
- 'should call progback for cancelable deferred': function(done) {
- var c = cancelable(when.defer());
-
- c.promise.then(null, null, function (status) {
- assert.same(status, sentinel);
- done();
- });
-
- c.notify(sentinel);
- }
+define('when/cancelable-test', function (require) {
+
+ var cancelable, when;
+
+ cancelable = require('when/cancelable');
+ when = require('when');
+
+ buster.testCase('when/cancelable', {
+ 'should decorate deferred with a cancel() method': function() {
+ var c = cancelable(when.defer(), function() {});
+ assert(typeof c.cancel == 'function');
+ },
+
+ 'should propagate a rejection when a cancelable deferred is canceled': function(done) {
+ var c = cancelable(when.defer(), function() { return sentinel; });
+ c.cancel();
+
+ c.promise.then(
+ fail,
+ function(v) {
+ assert.equals(v, sentinel);
+ }
+ ).ensure(done);
+ },
+
+ 'should return a promise for canceled value when canceled': function(done) {
+ var c, promise;
+
+ c = cancelable(when.defer(), function() { return sentinel; });
+ promise = c.cancel();
+
+ promise.then(
+ fail,
+ function(v) {
+ assert.equals(v, sentinel);
+ }
+ ).ensure(done);
+ },
+
+ 'should not invoke canceler when rejected normally': function(done) {
+ var c = cancelable(when.defer(), function() { return other; });
+ c.reject(sentinel);
+ c.cancel();
+
+ c.promise.then(
+ fail,
+ function(v) {
+ assert.equals(v, sentinel);
+ }
+ ).ensure(done);
+ },
+
+ 'should propagate the unaltered resolution value': function(done) {
+ var c = cancelable(when.defer(), function() { return other; });
+ c.resolve(sentinel);
+ c.cancel();
+
+ c.promise.then(
+ function(val) {
+ assert.same(val, sentinel);
+ },
+ function(e) {
+ console.error(e);
+ fail(e);
+ }
+ ).ensure(done);
+ },
+
+ 'should call progback for cancelable deferred': function(done) {
+ var c = cancelable(when.defer());
+
+ c.promise.then(null, null, function (status) {
+ assert.same(status, sentinel);
+ done();
+ });
+
+ c.notify(sentinel);
+ }
+
+ });
});
-})(
+}(
this.buster || require('buster'),
- this.when || require('..'),
- this.when_cancelable || require('../cancelable')
-);
+ typeof define === 'function' && define.amd ? define : function (id, factory) {
+ var packageName = id.split(/[\/\-\.]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
+ pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
+ factory(function (moduleId) {
+ return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
+ });
+ }
+ // Boilerplate for AMD and Node
+));
View
11 test/curl-config.js
@@ -0,0 +1,11 @@
+(function (global) {
+ 'use strict';
+
+ global.curl = {
+ packages: [
+ { name: 'when', location: './', main: 'when' },
+ { name: 'curl', location: 'node_modules/curl/src/curl', main: 'curl' }
+ ]
+ };
+
+}(this));
View
126 test/debug-test.js
@@ -1,4 +1,4 @@
-(function(buster, when) {
+(function(buster, define) {
var assert, refute, fail;
@@ -6,79 +6,93 @@ assert = buster.assert;
refute = buster.refute;
fail = buster.assertions.fail;
-buster.testCase('when/debug', {
+define('when/debug-test', function (require) {
- 'tearDown': function() {
- delete when.debug;
- },
+ var when;
- 'global rejection handler': {
- 'should be called if set': function(done) {
- var spy, d;
+ when = require('when/debug');
- spy = this.spy();
- when.debug = { reject: spy };
+ buster.testCase('when/debug', {
- d = when.defer();
+ 'tearDown': function() {
+ delete when.debug;
+ },
- d.promise.then(
- fail,
- function() {
- assert.calledOnce(spy);
- },
- fail
- ).ensure(done);
+ 'global rejection handler': {
+ 'should be called if set': function(done) {
+ var spy, d;
- d.reject();
- }
- },
+ spy = this.spy();
+ when.debug = { reject: spy };
- 'global resolution handler': {
- 'should be called if set': function(done) {
- var spy, d;
+ d = when.defer();
- spy = this.spy();
- when.debug = { resolve: spy };
+ d.promise.then(
+ fail,
+ function() {
+ assert.calledOnce(spy);
+ },
+ fail
+ ).ensure(done);
- d = when.defer();
+ d.reject();
+ }
+ },
- d.promise.then(
- function() {
- assert.calledOnce(spy);
- },
- fail,
- fail
- ).ensure(done);
+ 'global resolution handler': {
+ 'should be called if set': function(done) {
+ var spy, d;
- d.resolve();
- }
- },
+ spy = this.spy();
+ when.debug = { resolve: spy };
- 'global progress handler': {
- 'should be called if set': function(done) {
- var spy, d;
+ d = when.defer();
- spy = this.spy();
- when.debug = { progress: spy };
+ d.promise.then(
+ function() {
+ assert.calledOnce(spy);
+ },
+ fail,
+ fail
+ ).ensure(done);
- d = when.defer();
+ d.resolve();
+ }
+ },
- d.promise.then(
- function() {
- assert.calledOnce(spy);
- },
- fail
- ).ensure(done);
+ 'global progress handler': {
+ 'should be called if set': function(done) {
+ var spy, d;
- d.notify();
- d.resolve();
- }
- }
+ spy = this.spy();
+ when.debug = { progress: spy };
+ d = when.defer();
+ d.promise.then(
+ function() {
+ assert.calledOnce(spy);
+ },
+ fail
+ ).ensure(done);
+
+ d.notify();
+ d.resolve();
+ }
+ }
+
+ });
});
-})(
+
+}(
this.buster || require('buster'),
- this.when || require('../debug')
-);
+ typeof define === 'function' && define.amd ? define : function (id, factory) {
+ var packageName = id.split(/[\/\-\.]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
+ pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
+ factory(function (moduleId) {
+ return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
+ });
+ }
+ // Boilerplate for AMD and Node
+));
View
775 test/defer-test.js
@@ -1,4 +1,4 @@
-(function(buster, when) {
+(function(buster, define) {
var assert, refute, fail, sentinel, other;
@@ -25,425 +25,440 @@ function fakeRejected(reason) {
};
}
-buster.testCase('when.defer', {
-
- 'resolve': {
- 'should fulfill with an immediate value': function(done) {
- var d = when.defer();
-
- d.promise.then(
- function(val) {
- assert.same(val, sentinel);
- },
- fail
- ).ensure(done);
-
- d.resolve(sentinel);
- },
-
- 'should fulfill with fulfilled promised': function(done) {
- var d = when.defer();
-
- d.promise.then(
- function(val) {
- assert.same(val, sentinel);
- },
- fail
- ).ensure(done);
-
- d.resolve(fakeResolved(sentinel));
- },
-
- 'should reject with rejected promise': function(done) {
- var d = when.defer();
-
- d.promise.then(
- fail,
- function(val) {
- assert.same(val, sentinel);
- }
- ).ensure(done);
-
- d.resolve(fakeRejected(sentinel));
+define('when.defer-test', function (require) {
+
+ var when;
+
+ when = require('when');
+
+ buster.testCase('when.defer', {
+
+ 'resolve': {
+ 'should fulfill with an immediate value': function(done) {
+ var d = when.defer();
+
+ d.promise.then(
+ function(val) {
+ assert.same(val, sentinel);
+ },
+ fail
+ ).ensure(done);
+
+ d.resolve(sentinel);
+ },
+
+ 'should fulfill with fulfilled promised': function(done) {
+ var d = when.defer();
+
+ d.promise.then(
+ function(val) {
+ assert.same(val, sentinel);
+ },
+ fail
+ ).ensure(done);
+
+ d.resolve(fakeResolved(sentinel));
+ },
+
+ 'should reject with rejected promise': function(done) {
+ var d = when.defer();
+
+ d.promise.then(
+ fail,
+ function(val) {
+ assert.same(val, sentinel);
+ }
+ ).ensure(done);
+
+ d.resolve(fakeRejected(sentinel));
+ },
+
+ 'should return a promise for the resolution value': function(done) {
+ var d = when.defer();
+
+ d.resolve(sentinel).then(
+ function(returnedPromiseVal) {
+ assert.equals(returnedPromiseVal, sentinel);
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should return a promise for a promised resolution value': function(done) {
+ var d = when.defer();
+
+ d.resolve(when.resolve(sentinel)).then(
+ function(returnedPromiseVal) {
+ assert.equals(returnedPromiseVal, sentinel);
+ },
+ fail
+ ).ensure(done);
+ },
+
+ 'should return a promise for a promised rejection value': function(done) {
+ var d = when.defer();
+
+ // Both the returned promise, and the deferred's own promise should
+ // be rejected with the same value
+ d.resolve(when.reject(sentinel)).then(
+ fail,
+ function(returnedPromiseVal) {
+ assert.equals(returnedPromiseVal, sentinel);
+ }
+ ).ensure(done);
+ },
+
+ 'should invoke newly added callback when already resolved': function(done) {
+ var d = when.defer();
+
+ d.resolve(sentinel);
+
+ d.promise.then(
+ function(val) {
+ assert.same(val, sentinel);
+ done();
+ },
+ fail
+ ).ensure(done);
+ }
},
- 'should return a promise for the resolution value': function(done) {
- var d = when.defer();
-
- d.resolve(sentinel).then(
- function(returnedPromiseVal) {
- assert.equals(returnedPromiseVal, sentinel);
- },
- fail
- ).ensure(done);
+ 'reject': {
+ 'should reject with an immediate value': function(done) {
+ var d = when.defer();
+
+ d.promise.then(
+ fail,
+ function(val) {
+ assert.same(val, sentinel);
+ }
+ ).ensure(done);
+
+ d.reject(sentinel);
+ },
+
+ 'should reject with fulfilled promised': function(done) {
+ var d, expected;
+
+ d = when.defer();
+ expected = fakeResolved(sentinel);
+
+ d.promise.then(
+ fail,
+ function(val) {
+ assert.same(val, expected);
+ }
+ ).ensure(done);
+
+ d.reject(expected);
+ },
+
+ 'should reject with rejected promise': function(done) {
+ var d, expected;
+
+ d = when.defer();
+ expected = fakeRejected(sentinel);
+
+ d.promise.then(
+ fail,
+ function(val) {
+ assert.same(val, expected);
+ }
+ ).ensure(done);
+
+ d.reject(expected);
+ },
+
+
+ 'should return a promise for the rejection value': function(done) {
+ var d = when.defer();
+
+ // Both the returned promise, and the deferred's own promise should
+ // be rejected with the same value
+ d.reject(sentinel).then(
+ fail,
+ function(returnedPromiseVal) {
+ assert.equals(returnedPromiseVal, sentinel);
+ }
+ ).ensure(done);
+ },
+
+ 'should invoke newly added errback when already rejected': function(done) {
+ var d = when.defer();
+
+ d.reject(sentinel);
+
+ d.promise.then(
+ fail,
+ function (val) {
+ assert.equals(val, sentinel);
+ }
+ ).ensure(done);
+ }
},
- 'should return a promise for a promised resolution value': function(done) {
- var d = when.defer();
-
- d.resolve(when.resolve(sentinel)).then(
- function(returnedPromiseVal) {
- assert.equals(returnedPromiseVal, sentinel);
- },
- fail
- ).ensure(done);
+ 'notify': {
+
+ 'should notify of progress updates': function(done) {
+ var d = when.defer();
+
+ d.promise.then(
+ fail,
+ fail,
+ function(val) {
+ assert.same(val, sentinel);
+ done();
+ }
+ );
+
+ d.notify(sentinel);
+ },
+
+ 'should propagate progress to downstream promises': function(done) {
+ var d = when.defer();
+
+ d.promise
+ .then(fail, fail,
+ function(update) {
+ return update;
+ }
+ )
+ .then(fail, fail,
+ function(update) {
+ assert.same(update, sentinel);
+ done();
+ }
+ );
+
+ d.notify(sentinel);
+ },
+
+ 'should propagate transformed progress to downstream promises': function(done) {
+ var d = when.defer();
+
+ d.promise
+ .then(fail, fail,
+ function() {
+ return sentinel;
+ }
+ )
+ .then(fail, fail,
+ function(update) {
+ assert.same(update, sentinel);
+ done();
+ }
+ );
+
+ d.notify(other);
+ },
+
+ 'should propagate caught exception value as progress': function(done) {
+ var d = when.defer();
+
+ d.promise
+ .then(fail, fail,
+ function() {
+ throw sentinel;
+ }
+ )
+ .then(fail, fail,
+ function(update) {
+ assert.same(update, sentinel);
+ done();
+ }
+ );
+
+ d.notify(other);
+ },
+
+ 'should forward progress events when intermediary callback (tied to a resolved promise) returns a promise': function(done) {
+ var d, d2;
+
+ d = when.defer();
+ d2 = when.defer();
+
+ // resolve d BEFORE calling attaching progress handler
+ d.resolve();
+
+ d.promise.then(
+ function() {
+ return d2.promise;
+ }
+ ).then(fail, fail,
+ function(update) {
+ assert.same(update, sentinel);
+ done();
+ }
+ );
+
+ d2.notify(sentinel);
+ },
+
+ 'should forward progress events when intermediary callback (tied to an unresovled promise) returns a promise': function(done) {
+ var d, d2;
+
+ d = when.defer();
+ d2 = when.defer();
+
+ d.promise.then(
+ function() {
+ return d2.promise;
+ }
+ ).then(fail, fail,
+ function(update) {
+ assert.same(update, sentinel);
+ done();
+ }
+ );
+
+ // resolve d AFTER calling attaching progress handler
+ d.resolve();
+ d2.notify(sentinel);
+ },
+
+ 'should forward progress when resolved with another promise': function(done) {
+ var d, d2;
+
+ d = when.defer();
+ d2 = when.defer();