Skip to content

Commit 1b74e72

Browse files
committedAug 18, 2015
replace crlf to lf
1 parent ee21d8e commit 1b74e72

File tree

1 file changed

+291
-270
lines changed

1 file changed

+291
-270
lines changed
 

‎test/workflow-api-test.js

+291-270
Original file line numberDiff line numberDiff line change
@@ -1,271 +1,292 @@
1-
var assert = require('assert')
2-
var workflow = require('../')
3-
4-
describe("WorkSmith API", function() {
5-
6-
it("should run a workflow", function(done) {
7-
var flags = {};
8-
9-
var def = {
10-
task: function(def) {
11-
flags.define = true;
12-
return function build(context) {
13-
flags.build = true;
14-
return function execute(done) {
15-
flags.run = true;
16-
done();
17-
}
18-
}
19-
},
20-
param1: "value1"
21-
};
22-
23-
var wf = workflow(def)
24-
var wi = wf({});
25-
assert.ok(flags.define, "definition function must have run")
26-
assert.ok(flags.build, "build function must have run")
27-
wi(function(err, res) {
28-
assert.ok(flags.run, "execute function must have run")
29-
assert.ok(true,"always")
30-
done();
31-
})
32-
})
33-
34-
it("should provide worksmith as 'this' for definer", function(done) {
35-
var flags = {};
36-
37-
var def = {
38-
task: function(def) {
39-
flags.definerThis = this;
40-
return function build(context) {
41-
return function execute(done) {
42-
done();
43-
}
44-
}
45-
}
46-
};
47-
48-
var wf = workflow(def)
49-
var wi = wf({});
50-
wi(function(err, res) {
51-
assert.equal(flags.definerThis,workflow, "worksmith instance does not match")
52-
done();
53-
})
54-
})
55-
56-
it("should pass definition to builder", function(done) {
57-
var flags = {};
58-
59-
var def = {
60-
task: function(def) {
61-
flags.definition = def;
62-
return function build(context) {
63-
return function execute(done) {
64-
done();
65-
}
66-
}
67-
},
68-
param1: "value1"
69-
};
70-
71-
var wf = workflow(def)
72-
var wi = wf({});
73-
wi(function(err, res) {
74-
assert.equal(flags.definition,def, "definition must be passed correctly")
75-
done();
76-
})
77-
})
78-
79-
it("should pass context to executor", function(done) {
80-
var flags = {};
81-
82-
var def = {
83-
task: function(def) {
84-
return function build(context) {
85-
flags.context = context;
86-
return function execute(done) {
87-
done();
88-
}
89-
}
90-
},
91-
param1: "value1"
92-
};
93-
94-
var wf = workflow(def)
95-
var context = {};
96-
var wi = wf(context);
97-
wi(function(err, res) {
98-
assert.equal(flags.context,context, "context must be passed correctly")
99-
done();
100-
})
101-
})
102-
103-
it("should handle 'resultTo' pipeline", function(done) {
104-
var def = {
105-
task: function(def) {
106-
return function build(context) {
107-
return function execute(done) {
108-
done(null, 42);
109-
}
110-
}
111-
},
112-
resultTo:"result"
113-
};
114-
var wf = workflow(def)
115-
var context = {};
116-
var wi = wf(context);
117-
wi(function(err, res) {
118-
assert.equal(context.result,42, "result must be passed correctly")
119-
done();
120-
})
121-
122-
})
123-
124-
it("should provide 3 args to result callbacks", function(done) {
125-
var def = {
126-
task: function(def) {
127-
return function build(context) {
128-
return function execute(done) {
129-
done(null, 42);
130-
}
131-
}
132-
},
133-
resultTo:"result"
134-
};
135-
var wf = workflow(def)
136-
var context = {};
137-
var wi = wf(context);
138-
wi(function(err, res, ctx) {
139-
assert.equal(arguments.length, 3, "args count must match")
140-
assert.equal(ctx, context, "context must match")
141-
done();
142-
})
143-
144-
})
145-
146-
147-
148-
describe("parameter injector", function() {
149-
it("should inject definition fields correctly - static value", function(done) {
150-
var flags = {};
151-
152-
var def = {
153-
154-
task: function(def) {
155-
function build(context) {
156-
function execute(param1, done) {
157-
flags.param1 = param1;
158-
done();
159-
}
160-
execute.annotations = { inject:["param1"] }
161-
return execute
162-
}
163-
return build
164-
},
165-
param1: "value1"
166-
};
167-
168-
var wf = workflow(def)
169-
var context = { field1:"value1" };
170-
var wi = wf(context);
171-
wi(function(err, res) {
172-
assert.equal(flags.param1,"value1", "injected must be passed correctly")
173-
done();
174-
})
175-
})
176-
177-
it("should inject context fields correctly", function(done) {
178-
var flags = {};
179-
180-
var def = {
181-
182-
task: function(def) {
183-
function build(context) {
184-
function execute(param1, done) {
185-
flags.param1 = param1;
186-
done();
187-
}
188-
execute.annotations = { inject:["@field1"] }
189-
return execute
190-
}
191-
return build
192-
}
193-
};
194-
195-
var wf = workflow(def)
196-
var context = { field1:"value1" };
197-
var wi = wf(context);
198-
wi(function(err, res) {
199-
assert.equal(flags.param1,"value1", "injected must be passed correctly")
200-
done();
201-
})
202-
})
203-
})
204-
205-
it("should handle inject shortcut ", function(done) {
206-
var flags = {};
207-
208-
var def = {
209-
210-
task: function(def) {
211-
function build(context) {
212-
function execute(param1, done) {
213-
flags.param1 = param1;
214-
done();
215-
}
216-
execute.inject = ["@field1"]
217-
return execute
218-
}
219-
return build
220-
}
221-
};
222-
223-
var wf = workflow(def)
224-
var context = { field1:"value1" };
225-
var wi = wf(context);
226-
wi(function(err, res) {
227-
assert.equal(flags.param1,"value1", "injected must be passed correctly")
228-
done();
229-
})
230-
})
231-
it("should provide shortcut for sequence acitivity", function(done) {
232-
var steps = [];
233-
var counter = 0;
234-
steps.push({task: function() { return function() { return function(done) { counter++; done(); }}}});
235-
steps.push({task: function() { return function() { return function(done) { counter++; done(); }}}});
236-
workflow(steps)({}, function() {
237-
assert.equal(counter, 2)
238-
done();
239-
})
240-
})
241-
it("should handle injecting arrays ", function(done) {
242-
var flags = {};
243-
244-
var def = {
245-
246-
task: function(def) {
247-
function build(context) {
248-
function execute(f1, f2, done) {
249-
flags.f1 = f1;
250-
flags.f2 = f2;
251-
done();
252-
}
253-
execute.annotations = { inject:["f1","f2"] }
254-
return execute
255-
}
256-
return build
257-
},
258-
f1: "@field1",
259-
f2: "@field2"
260-
};
261-
262-
var wf = workflow(def)
263-
var context = { field1:"value1", field2:["a","b","c"] };
264-
var wi = wf(context);
265-
wi(function(err, res) {
266-
assert.equal(flags.f1,"value1", "injected must be passed correctly")
267-
assert.equal(flags.f2, context.field2, "injected must be passed correctly")
268-
done();
269-
})
270-
})
1+
var assert = require('assert')
2+
var workflow = require('../')
3+
4+
describe("WorkSmith API", function() {
5+
6+
it("should run a workflow", function(done) {
7+
var flags = {};
8+
9+
var def = {
10+
task: function(def) {
11+
flags.define = true;
12+
return function build(context) {
13+
flags.build = true;
14+
return function execute(done) {
15+
flags.run = true;
16+
done();
17+
}
18+
}
19+
},
20+
param1: "value1"
21+
};
22+
23+
var wf = workflow(def)
24+
var wi = wf({});
25+
assert.ok(flags.define, "definition function must have run")
26+
assert.ok(flags.build, "build function must have run")
27+
wi(function(err, res) {
28+
assert.ok(flags.run, "execute function must have run")
29+
assert.ok(true,"always")
30+
done();
31+
})
32+
})
33+
34+
it("should provide worksmith as 'this' for definer", function(done) {
35+
var flags = {};
36+
37+
var def = {
38+
task: function(def) {
39+
flags.definerThis = this;
40+
return function build(context) {
41+
return function execute(done) {
42+
done();
43+
}
44+
}
45+
}
46+
};
47+
48+
var wf = workflow(def)
49+
var wi = wf({});
50+
wi(function(err, res) {
51+
assert.equal(flags.definerThis,workflow, "worksmith instance does not match")
52+
done();
53+
})
54+
})
55+
56+
it("should pass definition to builder", function(done) {
57+
var flags = {};
58+
59+
var def = {
60+
task: function(def) {
61+
flags.definition = def;
62+
return function build(context) {
63+
return function execute(done) {
64+
done();
65+
}
66+
}
67+
},
68+
param1: "value1"
69+
};
70+
71+
var wf = workflow(def)
72+
var wi = wf({});
73+
wi(function(err, res) {
74+
assert.equal(flags.definition,def, "definition must be passed correctly")
75+
done();
76+
})
77+
})
78+
79+
it("should pass context to executor", function(done) {
80+
var flags = {};
81+
82+
var def = {
83+
task: function(def) {
84+
return function build(context) {
85+
flags.context = context;
86+
return function execute(done) {
87+
done();
88+
}
89+
}
90+
},
91+
param1: "value1"
92+
};
93+
94+
var wf = workflow(def)
95+
var context = {};
96+
var wi = wf(context);
97+
wi(function(err, res) {
98+
assert.equal(flags.context,context, "context must be passed correctly")
99+
done();
100+
})
101+
})
102+
103+
it("should handle 'resultTo' pipeline", function(done) {
104+
var def = {
105+
task: function(def) {
106+
return function build(context) {
107+
return function execute(done) {
108+
done(null, 42);
109+
}
110+
}
111+
},
112+
resultTo:"result"
113+
};
114+
var wf = workflow(def)
115+
var context = {};
116+
var wi = wf(context);
117+
wi(function(err, res) {
118+
assert.equal(context.result,42, "result must be passed correctly")
119+
done();
120+
})
121+
122+
})
123+
124+
it("should provide 3 args to result callbacks", function(done) {
125+
var def = {
126+
task: function(def) {
127+
return function build(context) {
128+
return function execute(done) {
129+
done(null, 42);
130+
}
131+
}
132+
},
133+
resultTo:"result"
134+
};
135+
var wf = workflow(def)
136+
var context = {};
137+
var wi = wf(context);
138+
wi(function(err, res, ctx) {
139+
assert.equal(arguments.length, 3, "args count must match")
140+
assert.equal(ctx, context, "context must match")
141+
done();
142+
})
143+
144+
})
145+
146+
147+
148+
describe("parameter injector", function() {
149+
it("should inject definition fields correctly - static value", function(done) {
150+
var flags = {};
151+
152+
var def = {
153+
154+
task: function(def) {
155+
function build(context) {
156+
function execute(param1, done) {
157+
flags.param1 = param1;
158+
done();
159+
}
160+
execute.annotations = { inject:["param1"] }
161+
return execute
162+
}
163+
return build
164+
},
165+
param1: "value1"
166+
};
167+
168+
var wf = workflow(def)
169+
var context = { field1:"value1" };
170+
var wi = wf(context);
171+
wi(function(err, res) {
172+
assert.equal(flags.param1,"value1", "injected must be passed correctly")
173+
done();
174+
})
175+
})
176+
177+
it("should inject context fields correctly", function(done) {
178+
var flags = {};
179+
180+
var def = {
181+
182+
task: function(def) {
183+
function build(context) {
184+
function execute(param1, done) {
185+
flags.param1 = param1;
186+
done();
187+
}
188+
execute.annotations = { inject:["@field1"] }
189+
return execute
190+
}
191+
return build
192+
}
193+
};
194+
195+
var wf = workflow(def)
196+
var context = { field1:"value1" };
197+
var wi = wf(context);
198+
wi(function(err, res) {
199+
assert.equal(flags.param1,"value1", "injected must be passed correctly")
200+
done();
201+
})
202+
})
203+
})
204+
205+
it("should handle inject shortcut ", function(done) {
206+
var flags = {};
207+
208+
var def = {
209+
210+
task: function(def) {
211+
function build(context) {
212+
function execute(param1, done) {
213+
flags.param1 = param1;
214+
done();
215+
}
216+
execute.inject = ["@field1"]
217+
return execute
218+
}
219+
return build
220+
}
221+
};
222+
223+
var wf = workflow(def)
224+
var context = { field1:"value1" };
225+
var wi = wf(context);
226+
wi(function(err, res) {
227+
assert.equal(flags.param1,"value1", "injected must be passed correctly")
228+
done();
229+
})
230+
})
231+
it("should provide shortcut for sequence acitivity", function(done) {
232+
var steps = [];
233+
var counter = 0;
234+
steps.push({task: function() { return function() { return function(done) { counter++; done(); }}}});
235+
steps.push({task: function() { return function() { return function(done) { counter++; done(); }}}});
236+
workflow(steps)({}, function() {
237+
assert.equal(counter, 2)
238+
done();
239+
})
240+
})
241+
it("should handle injecting arrays ", function(done) {
242+
var flags = {};
243+
244+
var def = {
245+
246+
task: function(def) {
247+
function build(context) {
248+
function execute(f1, f2, done) {
249+
flags.f1 = f1;
250+
flags.f2 = f2;
251+
done();
252+
}
253+
execute.annotations = { inject:["f1","f2"] }
254+
return execute
255+
}
256+
return build
257+
},
258+
f1: "@field1",
259+
f2: "@field2"
260+
};
261+
262+
var wf = workflow(def)
263+
var context = { field1:"value1", field2:["a","b","c"] };
264+
var wi = wf(context);
265+
wi(function(err, res) {
266+
assert.equal(flags.f1,"value1", "injected must be passed correctly")
267+
assert.equal(flags.f2, context.field2, "injected must be passed correctly")
268+
done();
269+
})
270+
})
271+
272+
xit("should not call wf.complete twice on an throw", function(done) {
273+
var def = [
274+
{
275+
task: function(step) {
276+
return function(context) {
277+
return function execute(done) {
278+
done();
279+
}
280+
}
281+
}
282+
}
283+
]
284+
var flag = [];
285+
var wf = workflow(def)
286+
wf({}, function(err, res) {
287+
flag.push({})
288+
throw new Error("error")
289+
});
290+
//flag.length == 1
291+
})
271292
})

0 commit comments

Comments
 (0)
Please sign in to comment.