Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

starts refactoring

  • Loading branch information...
commit f643c8e5c9c57170379b8116bdb90e97a540be04 1 parent 4eea092
@dfilatov authored
View
2  Makefile
@@ -1,5 +1,5 @@
test:
- node test/runner.js
+ node test/utils/runner.js
benchmark:
node benchmarks/comparison.js
View
2  package.json
@@ -23,6 +23,6 @@
"main" : "lib/vow",
"engines" : { "node" : ">= 0.4.0" },
"scripts" : {
- "test" : "./node_modules/istanbul/lib/cli.js test test/runner.js"
+ "test" : "./node_modules/istanbul/lib/cli.js test test/utils/runner.js"
}
}
View
10 test/promise.fail.js
@@ -0,0 +1,10 @@
+module.exports = {
+ 'onRejected callback should be called on reject' : function(test) {
+ var promise = Vow.promise();
+ promise.reject('error');
+ promise.fail(function(error) {
+ test.strictEqual(error, 'error');
+ test.done();
+ });
+ }
+};
View
144 test/promise.fulfill.js
@@ -0,0 +1,144 @@
+module.exports = {
+ 'onFulfilled callbacks should be called on fulfill only' : function(test) {
+ var promise = Vow.promise(),
+ called1 = false,
+ called2 = false,
+ called3 = false,
+ called4 = false;
+
+ promise.then(function() {
+ called1 = true;
+ });
+
+ promise.then(null, function() {
+ called2 = true;
+ });
+
+ promise.then(function() {
+ called3 = true;
+ });
+
+ promise.fulfill();
+
+ promise.then(function() {
+ called4 = true;
+ });
+
+ promise.then(function() {
+ test.ok(called1);
+ test.ok(!called2);
+ test.ok(called3);
+ test.done();
+ });
+ },
+
+ 'onFulfilled callbacks should be called once' : function(test) {
+ var promise = Vow.promise(),
+ calledCnt = 0;
+
+ promise.then(function() {
+ calledCnt++;
+ });
+
+ promise.fulfill();
+ promise.fulfill();
+
+ promise.then(function() {
+ test.strictEqual(calledCnt, 1);
+ test.done();
+ });
+ },
+
+ 'onFulfilled callbacks shouldn\'t be called if fulfill have been called after reject' : function(test) {
+ var promise = Vow.promise(),
+ called = false;
+
+ promise.then(function() {
+ called = true;
+ });
+
+ promise.reject();
+ promise.fulfill();
+
+ promise.then(null, function() {
+ test.ok(!called);
+ test.done();
+ });
+ },
+
+ 'onFulfilled callbacks should be executed in the order of their originating calls to then' : function(test) {
+ var promise = Vow.promise(),
+ resOrder = [];
+
+ promise.then(function() {
+ resOrder.push(1);
+ });
+
+ promise.then(function() {
+ resOrder.push(2);
+ });
+
+ promise.fulfill();
+
+ promise.then(function() {
+ resOrder.push(3);
+ });
+
+ promise.then(function() {
+ resOrder.push(4);
+ });
+
+ setTimeout(function() {
+ promise.then(function() {
+ resOrder.push(5);
+ setTimeout(function() {
+ test.deepEqual(resOrder, [1, 2, 3, 4, 5, 6]);
+ test.done();
+ }, 10);
+ });
+
+ promise.then(function() {
+ resOrder.push(6);
+ });
+ }, 10);
+ },
+
+ 'onFulfilled callback shouldn\'t be called in the same turn of the event loop as the call to then' : function(test) {
+ var promise = Vow.promise(),
+ resOrder = [];
+
+ promise.then(function() {
+ resOrder.push(3);
+ });
+
+ process.nextTick(function() {
+ process.nextTick(function() {
+ resOrder.push(4);
+ process.nextTick(function() {
+ resOrder.push(7);
+ });
+ promise.then(function() {
+ resOrder.push(8);
+ });
+ });
+
+ promise.then(function() {
+ resOrder.push(5);
+ });
+
+ promise.then(function() {
+ resOrder.push(6);
+ });
+
+ resOrder.push(2);
+ });
+
+ resOrder.push(1);
+ promise.fulfill();
+
+ setTimeout(function() {
+ test.deepEqual(resOrder, [1, 2, 3, 4, 5, 6, 7, 8]);
+ test.done();
+ }, 20);
+ }
+};
View
144 test/promise.reject.js
@@ -0,0 +1,144 @@
+module.exports = {
+ 'onRejected callbacks should be called on reject only' : function(test) {
+ var promise = Vow.promise(),
+ called1 = false,
+ called2 = false,
+ called3 = false,
+ called4 = false;
+
+ promise.then(null, function() {
+ called1 = true;
+ });
+
+ promise.then(function() {
+ called2 = true;
+ });
+
+ promise.then(null, function() {
+ called3 = true;
+ });
+
+ promise.reject();
+
+ promise.then(null, function() {
+ called4 = true;
+ });
+
+ promise.then(null, function() {
+ test.ok(called1);
+ test.ok(!called2);
+ test.ok(called3);
+ test.done();
+ });
+ },
+
+ 'onRejected callbacks should be called once' : function(test) {
+ var promise = Vow.promise(),
+ calledCnt = 0;
+
+ promise.then(null, function() {
+ calledCnt++;
+ });
+
+ promise.reject();
+ promise.reject();
+
+ promise.then(null, function() {
+ test.strictEqual(calledCnt, 1);
+ test.done();
+ });
+ },
+
+ 'onRejected callbacks shouldn\'t be called if reject have been called after fulfill' : function(test) {
+ var promise = Vow.promise(),
+ called = false;
+
+ promise.then(null, function() {
+ called = true;
+ });
+
+ promise.fulfill();
+ promise.reject();
+
+ promise.then(function() {
+ test.ok(!called);
+ test.done();
+ });
+ },
+
+ 'onRejected callbacks should be executed in the order of their originating calls to then' : function(test) {
+ var promise = Vow.promise(),
+ resOrder = [];
+
+ promise.then(null, function() {
+ resOrder.push(1);
+ });
+
+ promise.then(null, function() {
+ resOrder.push(2);
+ });
+
+ promise.reject();
+
+ promise.then(null, function() {
+ resOrder.push(3);
+ });
+
+ promise.then(null, function() {
+ resOrder.push(4);
+ });
+
+ setTimeout(function() {
+ promise.then(null, function() {
+ resOrder.push(5);
+ setTimeout(function() {
+ test.deepEqual(resOrder, [1, 2, 3, 4, 5, 6]);
+ test.done();
+ }, 10);
+ });
+
+ promise.then(null, function() {
+ resOrder.push(6);
+ });
+ }, 10);
+ },
+
+ 'onRejected callback shouldn\'t be called in the same turn of the event loop as the call to then' : function(test) {
+ var promise = Vow.promise(),
+ resOrder = [];
+
+ promise.then(null, function() {
+ resOrder.push(3);
+ });
+
+ process.nextTick(function() {
+ process.nextTick(function() {
+ resOrder.push(4);
+ process.nextTick(function() {
+ resOrder.push(7);
+ });
+ promise.then(null, function() {
+ resOrder.push(8);
+ });
+ });
+
+ promise.then(null, function() {
+ resOrder.push(5);
+ });
+
+ promise.then(null, function() {
+ resOrder.push(6);
+ });
+
+ resOrder.push(2);
+ });
+
+ resOrder.push(1);
+ promise.reject();
+
+ setTimeout(function() {
+ test.deepEqual(resOrder, [1, 2, 3, 4, 5, 6, 7, 8]);
+ test.done();
+ }, 20);
+ }
+};
View
105 test/promise.then.js
@@ -0,0 +1,105 @@
+module.exports = {
+ 'resulting promise should be fulfilled with same value' : function(test) {
+ var promise = Vow.promise();
+
+ promise.then().then(function(val) {
+ test.strictEqual(val, 'val');
+ test.done();
+ });
+
+ promise.fulfill('val');
+ },
+
+ 'resulting promise should be rejected with same reason' : function(test) {
+ var promise = Vow.promise(),
+ error = new Error('errot');
+
+ promise.then().then(null, function(_error) {
+ test.strictEqual(_error, error);
+ test.done();
+ });
+
+ promise.reject(error);
+ },
+
+ 'resulting promise should be fulfilled with returned value of onFulfilled callback' : function(test) {
+ var promise = Vow.promise(),
+ resPromise = promise.then(function() {
+ return 'val';
+ });
+
+ promise.fulfill();
+
+ resPromise.then(function(val) {
+ test.strictEqual(val, 'val');
+ test.done();
+ });
+ },
+
+ 'resulting promise should be fulfilled with same value as returned promise of onFulfilled callback' : function(test) {
+ var promise = Vow.promise(),
+ retPromise = Vow.promise(),
+ resPromise = promise.then(function() {
+ return retPromise;
+ });
+
+ promise.then(function() {
+ retPromise.fulfill('ok');
+ });
+
+ resPromise.then(function(val) {
+ test.strictEqual(val, 'ok');
+ test.done();
+ });
+
+ promise.fulfill();
+ },
+
+ 'resulting promise should be rejected with same value as returned promise of onRejected callback' : function(test) {
+ var promise = Vow.promise(),
+ retPromise = Vow.promise(),
+ resPromise = promise.then(function() {
+ return retPromise;
+ }),
+ error = new Error('error');
+
+ promise.then(function() {
+ retPromise.reject(error);
+ });
+
+ resPromise.then(null, function(_error) {
+ test.strictEqual(_error, error);
+ test.done();
+ });
+
+ promise.fulfill();
+ },
+
+ 'resulting promise should be rejected if onFulfilled callback throw exception' : function(test) {
+ var promise = Vow.promise(),
+ resPromise = promise.then(function() {
+ throw { message : 'error' };
+ });
+
+ resPromise.then(null, function(arg) {
+ test.deepEqual(arg, { message : 'error' });
+ test.done();
+ });
+
+ promise.fulfill();
+ },
+
+ 'resulting promise should be rejected if onRejected callback throw exception' : function(test) {
+ var promise = Vow.promise(),
+ resPromise = promise.then(null, function() {
+ throw { message : 'error' };
+ });
+
+ resPromise.then(null, function(arg) {
+ test.deepEqual(arg, { message : 'error' });
+ test.done();
+ });
+
+ promise.reject();
+ }
+};
View
26 test/promise.valueof.js
@@ -0,0 +1,26 @@
+module.exports = {
+ 'should return undefined if promise is unresolved' : function(test) {
+ var promise = Vow.promise();
+
+ test.strictEqual(promise.valueOf(), undefined);
+ test.done();
+ },
+
+ 'should return value of fulfillment if promise if fulfilled' : function(test) {
+ var promise = Vow.promise();
+ promise.fulfill('ok');
+
+ test.strictEqual(promise.valueOf(), 'ok');
+ test.done();
+ },
+
+ 'should return reason of rejection if promise if rejected' : function(test) {
+ var promise = Vow.promise(),
+ error = Error();
+
+ promise.reject(error);
+
+ test.strictEqual(promise.valueOf(), error);
+ test.done();
+ }
+};
View
7 test/runner.js
@@ -1,7 +0,0 @@
-require('nodeunit').reporters.default.run(['test/vow.js'], null, function(err) {
- err?
- process.exit(1) :
- require('promises-aplus-tests')(require('./aplus-adapter'), function(err) {
- err && process.exit(1);
- });
-});
View
2  test/aplus-adapter.js → test/utils/aplus-adapter.js
@@ -1,4 +1,4 @@
-var Vow = require('..');
+var Vow = require('../..');
module.exports = {
fulfilled : function(val) {
View
22 test/utils/runner.js
@@ -0,0 +1,22 @@
+global.Vow = require('../..');
+
+var fs = require('fs'),
+ path = require('path'),
+ dir = path.join(__dirname, '..');
+
+require('nodeunit').reporters.default.run(
+ fs.readdirSync(dir)
+ .filter(function(file){
+ return fs.statSync(path.join(dir, file)).isFile();
+ })
+ .map(function(file) {
+ return path.join('test', file);
+ }),
+ null,
+ function(err) {
+ err?
+ process.exit(1) :
+ require('promises-aplus-tests')(require('./aplus-adapter'), function(err) {
+ err && process.exit(1);
+ });
+ });
View
443 test/vow.js
@@ -1,449 +1,6 @@
var Vow = require('..');
module.exports = {
- 'promise' : {
- 'promise should be immediately fulfilled with argument value' : function(test) {
- var promise = Vow.promise('val');
- promise.then(function(val) {
- test.strictEqual(val, 'val');
- test.done();
- });
- }
- },
- 'promise.fulfill' : {
- 'onFulfilled callbacks should be called on fulfill only' : function(test) {
- var promise = Vow.promise(),
- called1 = false,
- called2 = false,
- called3 = false,
- called4 = false;
-
- promise.then(function() {
- called1 = true;
- });
-
- promise.then(null, function() {
- called2 = true;
- });
-
- promise.then(function() {
- called3 = true;
- });
-
- promise.fulfill();
-
- promise.then(function() {
- called4 = true;
- });
-
- promise.then(function() {
- test.ok(called1);
- test.ok(!called2);
- test.ok(called3);
- test.done();
- });
- },
-
- 'onFulfilled callbacks should be called once' : function(test) {
- var promise = Vow.promise(),
- calledCnt = 0;
-
- promise.then(function() {
- calledCnt++;
- });
-
- promise.fulfill();
- promise.fulfill();
-
- promise.then(function() {
- test.strictEqual(calledCnt, 1);
- test.done();
- });
- },
-
- 'onFulfilled callbacks shouldn\'t be called if fulfill have been called after reject' : function(test) {
- var promise = Vow.promise(),
- called = false;
-
- promise.then(function() {
- called = true;
- });
-
- promise.reject();
- promise.fulfill();
-
- promise.then(null, function() {
- test.ok(!called);
- test.done();
- });
- },
-
- 'onFulfilled callbacks should be executed in the order of their originating calls to then' : function(test) {
- var promise = Vow.promise(),
- resOrder = [];
-
- promise.then(function() {
- resOrder.push(1);
- });
-
- promise.then(function() {
- resOrder.push(2);
- });
-
- promise.fulfill();
-
- promise.then(function() {
- resOrder.push(3);
- });
-
- promise.then(function() {
- resOrder.push(4);
- });
-
- setTimeout(function() {
- promise.then(function() {
- resOrder.push(5);
- setTimeout(function() {
- test.deepEqual(resOrder, [1, 2, 3, 4, 5, 6]);
- test.done();
- }, 10);
- });
-
- promise.then(function() {
- resOrder.push(6);
- });
- }, 10);
- },
-
- 'onFulfilled callback shouldn\'t be called in the same turn of the event loop as the call to then' : function(test) {
- var promise = Vow.promise(),
- resOrder = [];
-
- promise.then(function() {
- resOrder.push(3);
- });
-
- process.nextTick(function() {
- process.nextTick(function() {
- resOrder.push(4);
- process.nextTick(function() {
- resOrder.push(7);
- });
- promise.then(function() {
- resOrder.push(8);
- });
- });
-
- promise.then(function() {
- resOrder.push(5);
- });
-
- promise.then(function() {
- resOrder.push(6);
- });
-
- resOrder.push(2);
- });
-
- resOrder.push(1);
- promise.fulfill();
-
- setTimeout(function() {
- test.deepEqual(resOrder, [1, 2, 3, 4, 5, 6, 7, 8]);
- test.done();
- }, 20);
- }
- },
-
- 'promise.reject' : {
- 'onRejected callbacks should be called on reject only' : function(test) {
- var promise = Vow.promise(),
- called1 = false,
- called2 = false,
- called3 = false,
- called4 = false;
-
- promise.then(null, function() {
- called1 = true;
- });
-
- promise.then(function() {
- called2 = true;
- });
-
- promise.then(null, function() {
- called3 = true;
- });
-
- promise.reject();
-
- promise.then(null, function() {
- called4 = true;
- });
-
- promise.then(null, function() {
- test.ok(called1);
- test.ok(!called2);
- test.ok(called3);
- test.done();
- });
- },
-
- 'onRejected callbacks should be called once' : function(test) {
- var promise = Vow.promise(),
- calledCnt = 0;
-
- promise.then(null, function() {
- calledCnt++;
- });
-
- promise.reject();
- promise.reject();
-
- promise.then(null, function() {
- test.strictEqual(calledCnt, 1);
- test.done();
- });
- },
-
- 'onRejected callbacks shouldn\'t be called if reject have been called after fulfill' : function(test) {
- var promise = Vow.promise(),
- called = false;
-
- promise.then(null, function() {
- called = true;
- });
-
- promise.fulfill();
- promise.reject();
-
- promise.then(function() {
- test.ok(!called);
- test.done();
- });
- },
-
- 'onRejected callbacks should be executed in the order of their originating calls to then' : function(test) {
- var promise = Vow.promise(),
- resOrder = [];
-
- promise.then(null, function() {
- resOrder.push(1);
- });
-
- promise.then(null, function() {
- resOrder.push(2);
- });
-
- promise.reject();
-
- promise.then(null, function() {
- resOrder.push(3);
- });
-
- promise.then(null, function() {
- resOrder.push(4);
- });
-
- setTimeout(function() {
- promise.then(null, function() {
- resOrder.push(5);
- setTimeout(function() {
- test.deepEqual(resOrder, [1, 2, 3, 4, 5, 6]);
- test.done();
- }, 10);
- });
-
- promise.then(null, function() {
- resOrder.push(6);
- });
- }, 10);
- },
-
- 'onRejected callback shouldn\'t be called in the same turn of the event loop as the call to then' : function(test) {
- var promise = Vow.promise(),
- resOrder = [];
-
- promise.then(null, function() {
- resOrder.push(3);
- });
-
- process.nextTick(function() {
- process.nextTick(function() {
- resOrder.push(4);
- process.nextTick(function() {
- resOrder.push(7);
- });
- promise.then(null, function() {
- resOrder.push(8);
- });
- });
-
- promise.then(null, function() {
- resOrder.push(5);
- });
-
- promise.then(null, function() {
- resOrder.push(6);
- });
-
- resOrder.push(2);
- });
-
- resOrder.push(1);
- promise.reject();
-
- setTimeout(function() {
- test.deepEqual(resOrder, [1, 2, 3, 4, 5, 6, 7, 8]);
- test.done();
- }, 20);
- }
- },
-
- 'promise.valueOf' : {
- 'should return undefined if promise is unresolved' : function(test) {
- var promise = Vow.promise();
-
- test.strictEqual(promise.valueOf(), undefined);
- test.done();
- },
-
- 'should return value of fulfillment if promise if fulfilled' : function(test) {
- var promise = Vow.promise();
- promise.fulfill('ok');
-
- test.strictEqual(promise.valueOf(), 'ok');
- test.done();
- },
-
- 'should return reason of rejection if promise if rejected' : function(test) {
- var promise = Vow.promise(),
- error = Error();
-
- promise.reject(error);
-
- test.strictEqual(promise.valueOf(), error);
- test.done();
- }
- },
-
- 'promise.then' : {
- 'resulting promise should be fulfilled with same value' : function(test) {
- var promise = Vow.promise();
-
- promise.then().then(function(val) {
- test.strictEqual(val, 'val');
- test.done();
- });
-
- promise.fulfill('val');
- },
-
- 'resulting promise should be rejected with same reason' : function(test) {
- var promise = Vow.promise(),
- error = new Error('errot');
-
- promise.then().then(null, function(_error) {
- test.strictEqual(_error, error);
- test.done();
- });
-
- promise.reject(error);
- },
-
- 'resulting promise should be fulfilled with returned value of onFulfilled callback' : function(test) {
- var promise = Vow.promise(),
- resPromise = promise.then(function() {
- return 'val';
- });
-
- promise.fulfill();
-
- resPromise.then(function(val) {
- test.strictEqual(val, 'val');
- test.done();
- });
- },
-
- 'resulting promise should be fulfilled with same value as returned promise of onFulfilled callback' : function(test) {
- var promise = Vow.promise(),
- retPromise = Vow.promise(),
- resPromise = promise.then(function() {
- return retPromise;
- });
-
- promise.then(function() {
- retPromise.fulfill('ok');
- });
-
- resPromise.then(function(val) {
- test.strictEqual(val, 'ok');
- test.done();
- });
-
- promise.fulfill();
- },
-
- 'resulting promise should be rejected with same value as returned promise of onRejected callback' : function(test) {
- var promise = Vow.promise(),
- retPromise = Vow.promise(),
- resPromise = promise.then(function() {
- return retPromise;
- }),
- error = new Error('error');
-
- promise.then(function() {
- retPromise.reject(error);
- });
-
- resPromise.then(null, function(_error) {
- test.strictEqual(_error, error);
- test.done();
- });
-
- promise.fulfill();
- },
-
- 'resulting promise should be rejected if onFulfilled callback throw exception' : function(test) {
- var promise = Vow.promise(),
- resPromise = promise.then(function() {
- throw { message : 'error' };
- });
-
- resPromise.then(null, function(arg) {
- test.deepEqual(arg, { message : 'error' });
- test.done();
- });
-
- promise.fulfill();
- },
-
- 'resulting promise should be rejected if onRejected callback throw exception' : function(test) {
- var promise = Vow.promise(),
- resPromise = promise.then(null, function() {
- throw { message : 'error' };
- });
-
- resPromise.then(null, function(arg) {
- test.deepEqual(arg, { message : 'error' });
- test.done();
- });
-
- promise.reject();
- }
- },
-
- 'promise.fail' : {
- 'onRejected callback should be called on reject' : function(test) {
- var promise = Vow.promise();
- promise.reject('error');
- promise.fail(function(error) {
- test.strictEqual(error, 'error');
- test.done();
- });
- }
- },
-
'promise.always' : {
'onResolved callback should be called on fulfill' : function(test) {
var promise = Vow.promise();
View
9 test/vow.promise.js
@@ -0,0 +1,9 @@
+module.exports = {
+ 'promise should be immediately fulfilled with argument value' : function(test) {
+ var promise = Vow.promise('val');
+ promise.then(function(val) {
+ test.strictEqual(val, 'val');
+ test.done();
+ });
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.