Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / @protobufjs / aspromise / tests / index.js
1 var tape = require("tape");\r
2 \r
3 var asPromise = require("..");\r
4 \r
5 tape.test("aspromise", function(test) {\r
6 \r
7     test.test(this.name + " - resolve", function(test) {\r
8 \r
9         function fn(arg1, arg2, callback) {\r
10             test.equal(this, ctx, "function should be called with this = ctx");\r
11             test.equal(arg1, 1, "function should be called with arg1 = 1");\r
12             test.equal(arg2, 2, "function should be called with arg2 = 2");\r
13             callback(null, arg2);\r
14         }\r
15 \r
16         var ctx = {};\r
17 \r
18         var promise = asPromise(fn, ctx, 1, 2);\r
19         promise.then(function(arg2) {\r
20             test.equal(arg2, 2, "promise should be resolved with arg2 = 2");\r
21             test.end();\r
22         }).catch(function(err) {\r
23             test.fail("promise should not be rejected (" + err + ")");\r
24         });\r
25     });\r
26 \r
27     test.test(this.name + " - reject", function(test) {\r
28 \r
29         function fn(arg1, arg2, callback) {\r
30             test.equal(this, ctx, "function should be called with this = ctx");\r
31             test.equal(arg1, 1, "function should be called with arg1 = 1");\r
32             test.equal(arg2, 2, "function should be called with arg2 = 2");\r
33             callback(arg1);\r
34         }\r
35 \r
36         var ctx = {};\r
37 \r
38         var promise = asPromise(fn, ctx, 1, 2);\r
39         promise.then(function() {\r
40             test.fail("promise should not be resolved");\r
41         }).catch(function(err) {\r
42             test.equal(err, 1, "promise should be rejected with err = 1");\r
43             test.end();\r
44         });\r
45     });\r
46 \r
47     test.test(this.name + " - resolve twice", function(test) {\r
48 \r
49         function fn(arg1, arg2, callback) {\r
50             test.equal(this, ctx, "function should be called with this = ctx");\r
51             test.equal(arg1, 1, "function should be called with arg1 = 1");\r
52             test.equal(arg2, 2, "function should be called with arg2 = 2");\r
53             callback(null, arg2);\r
54             callback(null, arg1);\r
55         }\r
56 \r
57         var ctx = {};\r
58         var count = 0;\r
59 \r
60         var promise = asPromise(fn, ctx, 1, 2);\r
61         promise.then(function(arg2) {\r
62             test.equal(arg2, 2, "promise should be resolved with arg2 = 2");\r
63             if (++count > 1)\r
64                 test.fail("promise should not be resolved twice");\r
65             test.end();\r
66         }).catch(function(err) {\r
67             test.fail("promise should not be rejected (" + err + ")");\r
68         });\r
69     });\r
70 \r
71     test.test(this.name + " - reject twice", function(test) {\r
72 \r
73         function fn(arg1, arg2, callback) {\r
74             test.equal(this, ctx, "function should be called with this = ctx");\r
75             test.equal(arg1, 1, "function should be called with arg1 = 1");\r
76             test.equal(arg2, 2, "function should be called with arg2 = 2");\r
77             callback(arg1);\r
78             callback(arg2);\r
79         }\r
80 \r
81         var ctx = {};\r
82         var count = 0;\r
83 \r
84         var promise = asPromise(fn, ctx, 1, 2);\r
85         promise.then(function() {\r
86             test.fail("promise should not be resolved");\r
87         }).catch(function(err) {\r
88             test.equal(err, 1, "promise should be rejected with err = 1");\r
89             if (++count > 1)\r
90                 test.fail("promise should not be rejected twice");\r
91             test.end();\r
92         });\r
93     });\r
94 \r
95     test.test(this.name + " - reject error", function(test) {\r
96 \r
97         function fn(callback) {\r
98             test.ok(arguments.length === 1 && typeof callback === "function", "function should be called with just a callback");\r
99             throw 3;\r
100         }\r
101 \r
102         var promise = asPromise(fn, null);\r
103         promise.then(function() {\r
104             test.fail("promise should not be resolved");\r
105         }).catch(function(err) {\r
106             test.equal(err, 3, "promise should be rejected with err = 3");\r
107             test.end();\r
108         });\r
109     });\r
110 \r
111     test.test(this.name + " - reject and error", function(test) {\r
112 \r
113         function fn(callback) {\r
114             callback(3);\r
115             throw 4;\r
116         }\r
117 \r
118         var count = 0;\r
119 \r
120         var promise = asPromise(fn, null);\r
121         promise.then(function() {\r
122             test.fail("promise should not be resolved");\r
123         }).catch(function(err) {\r
124             test.equal(err, 3, "promise should be rejected with err = 3");\r
125             if (++count > 1)\r
126                 test.fail("promise should not be rejected twice");\r
127             test.end();\r
128         });\r
129     });\r
130 });\r