--- /dev/null
+var tape = require("tape");\r
+\r
+var asPromise = require("..");\r
+\r
+tape.test("aspromise", function(test) {\r
+\r
+ test.test(this.name + " - resolve", function(test) {\r
+\r
+ function fn(arg1, arg2, callback) {\r
+ test.equal(this, ctx, "function should be called with this = ctx");\r
+ test.equal(arg1, 1, "function should be called with arg1 = 1");\r
+ test.equal(arg2, 2, "function should be called with arg2 = 2");\r
+ callback(null, arg2);\r
+ }\r
+\r
+ var ctx = {};\r
+\r
+ var promise = asPromise(fn, ctx, 1, 2);\r
+ promise.then(function(arg2) {\r
+ test.equal(arg2, 2, "promise should be resolved with arg2 = 2");\r
+ test.end();\r
+ }).catch(function(err) {\r
+ test.fail("promise should not be rejected (" + err + ")");\r
+ });\r
+ });\r
+\r
+ test.test(this.name + " - reject", function(test) {\r
+\r
+ function fn(arg1, arg2, callback) {\r
+ test.equal(this, ctx, "function should be called with this = ctx");\r
+ test.equal(arg1, 1, "function should be called with arg1 = 1");\r
+ test.equal(arg2, 2, "function should be called with arg2 = 2");\r
+ callback(arg1);\r
+ }\r
+\r
+ var ctx = {};\r
+\r
+ var promise = asPromise(fn, ctx, 1, 2);\r
+ promise.then(function() {\r
+ test.fail("promise should not be resolved");\r
+ }).catch(function(err) {\r
+ test.equal(err, 1, "promise should be rejected with err = 1");\r
+ test.end();\r
+ });\r
+ });\r
+\r
+ test.test(this.name + " - resolve twice", function(test) {\r
+\r
+ function fn(arg1, arg2, callback) {\r
+ test.equal(this, ctx, "function should be called with this = ctx");\r
+ test.equal(arg1, 1, "function should be called with arg1 = 1");\r
+ test.equal(arg2, 2, "function should be called with arg2 = 2");\r
+ callback(null, arg2);\r
+ callback(null, arg1);\r
+ }\r
+\r
+ var ctx = {};\r
+ var count = 0;\r
+\r
+ var promise = asPromise(fn, ctx, 1, 2);\r
+ promise.then(function(arg2) {\r
+ test.equal(arg2, 2, "promise should be resolved with arg2 = 2");\r
+ if (++count > 1)\r
+ test.fail("promise should not be resolved twice");\r
+ test.end();\r
+ }).catch(function(err) {\r
+ test.fail("promise should not be rejected (" + err + ")");\r
+ });\r
+ });\r
+\r
+ test.test(this.name + " - reject twice", function(test) {\r
+\r
+ function fn(arg1, arg2, callback) {\r
+ test.equal(this, ctx, "function should be called with this = ctx");\r
+ test.equal(arg1, 1, "function should be called with arg1 = 1");\r
+ test.equal(arg2, 2, "function should be called with arg2 = 2");\r
+ callback(arg1);\r
+ callback(arg2);\r
+ }\r
+\r
+ var ctx = {};\r
+ var count = 0;\r
+\r
+ var promise = asPromise(fn, ctx, 1, 2);\r
+ promise.then(function() {\r
+ test.fail("promise should not be resolved");\r
+ }).catch(function(err) {\r
+ test.equal(err, 1, "promise should be rejected with err = 1");\r
+ if (++count > 1)\r
+ test.fail("promise should not be rejected twice");\r
+ test.end();\r
+ });\r
+ });\r
+\r
+ test.test(this.name + " - reject error", function(test) {\r
+\r
+ function fn(callback) {\r
+ test.ok(arguments.length === 1 && typeof callback === "function", "function should be called with just a callback");\r
+ throw 3;\r
+ }\r
+\r
+ var promise = asPromise(fn, null);\r
+ promise.then(function() {\r
+ test.fail("promise should not be resolved");\r
+ }).catch(function(err) {\r
+ test.equal(err, 3, "promise should be rejected with err = 3");\r
+ test.end();\r
+ });\r
+ });\r
+\r
+ test.test(this.name + " - reject and error", function(test) {\r
+\r
+ function fn(callback) {\r
+ callback(3);\r
+ throw 4;\r
+ }\r
+\r
+ var count = 0;\r
+\r
+ var promise = asPromise(fn, null);\r
+ promise.then(function() {\r
+ test.fail("promise should not be resolved");\r
+ }).catch(function(err) {\r
+ test.equal(err, 3, "promise should be rejected with err = 3");\r
+ if (++count > 1)\r
+ test.fail("promise should not be rejected twice");\r
+ test.end();\r
+ });\r
+ });\r
+});\r