Debian Codegraph @

deferred/test/index.js

raw
1 "use strict";
2
3 var isFunction = require("es5-ext/function/is-function")
4 , convert = require("es5-ext/string/#/hyphen-to-camel")
5 , path = require("path")
6 , readdir = require("fs").readdir
7 , indexTest = require("tad/lib/utils/index-test");
8
9 var dir = path.dirname(__dirname);
10
11 module.exports = {
12 "": indexTest(
13 indexTest.readDir(dir)(function (o) {
14 delete o.assimilate;
15 delete o.benchmark;
16 delete o.deferred;
17 delete o.examples;
18 delete o.ext;
19 delete o.promise;
20 delete o.profiler;
21 return o;
22 }),
23 [
24 "Deferred", "callAsync", "delay", "extend", "gate", "profile", "profileEnd",
25 "promisify", "promisifySync", "reject", "resolve", "every", "find", "map", "reduce",
26 "some", "timeout"
27 ]
28 ),
29 "isPromise": function (t, a) {
30 a(t.isPromise(t(null)), true);
31 a(t.isPromise({}), false);
32 },
33 "InvokeAsync": function (t, a, d) {
34 var x = {};
35 t.invokeAsync({}, function (cb) {
36 setTimeout(function () { cb(null, x); }, 0);
37 return {};
38 })(function (r) { a(r, x); }).done(d, d);
39 },
40 "CallAsync": function (t, a, d) {
41 var x = {};
42 t.callAsync(function (cb) {
43 setTimeout(function () { cb(null, x); }, 0);
44 return {};
45 })(function (r) { a(r, x); }).done(d, d);
46 },
47 "Delay": function (t, a, d) {
48 var x = {};
49 t.delay(function (r) { return r; }, 5)(x)(function (r) { a(r, x); }).done(d, d);
50 },
51 "Gate": function (t, a) {
52 var fn, dx, dy, ready;
53 fn = t.gate(function (p) { return p; }, 1);
54 dx = t();
55 fn(dx.promise);
56 dy = t();
57 fn(dy.promise).done(function () { a(ready, true); });
58 dy.resolve({});
59 ready = true;
60 dx.resolve({});
61 ready = false;
62 },
63 "Profile": function (t, a) {
64 a(typeof t.profile, "function", "Profile");
65 a(typeof t.profileEnd, "function", "ProfileEnd");
66 },
67 "Promisify": function (t, a, d) {
68 var x = {};
69 t.promisify(function (cb) {
70 setTimeout(function () { cb(null, x); }, 0);
71 return {};
72 })()(function (r) { a(r, x); }).done(d, d);
73 },
74 "PromisifySync": function (t, a, d) {
75 t.promisifySync(function () {})()(function (r) { a(r, undefined); }).done(d, d);
76 },
77 "Map": function (t, a, d) {
78 t.map([t(1), t(2), 3], function (res) { return t(res * res); })(function (r) {
79 a.deep(r, [1, 4, 9]);
80 }, a.never).done(d, d);
81 },
82 "Reduce": function (t, a, d) {
83 t.reduce([t(1), t(2), 3], function (arg1, arg2) { return t(arg1 * arg2); }, 1)(function (
84 r
85 ) { a(r, 6); }, a.never).done(d, d);
86 },
87 "Some": function (t, a, d) {
88 var count = 0;
89 t.some([t(1), t(2), 3], function (res, index) {
90 ++count;
91 return index;
92 })(function (r) {
93 a(r, true);
94 a(count, 2, "Count");
95 }, a.never).done(d, d);
96 },
97 "Deferred function is main object": function (t, a) {
98 var d = t();
99 d.resolve({});
100 a.ok(isFunction(d.resolve) && isFunction(d.promise.then));
101 },
102 "Ports are loaded": function (t, a, d) {
103 var p = t().resolve();
104 readdir(dir + "/ext/promise", function (err, files) {
105 if (err) {
106 d(err);
107 return;
108 }
109 files
110 .map(function (file) {
111 if (file.slice(-3) === ".js" && file[0] !== "_") {
112 return convert.call(file.slice(0, -3));
113 }
114 return null;
115 })
116 .filter(Boolean)
117 .forEach(function (file) { a(isFunction(p[file]), true, file); });
118 d();
119 });
120 }
121 };