1 openerp.testing.section('testing.stack', function (test) {
2 // I heard you like tests, so I put tests in your testing infrastructure,
3 // so you can test what you test
4 var reject = function () {
5 // utility function, rejects a success
6 var args = _.toArray(arguments);
7 return $.Deferred(function (d) {
8 d.reject.apply(d, ["unexpected success"].concat(args));
11 test('direct, value, success', {asserts: 1}, function () {
12 var s = openerp.testing.Stack();
13 return s.execute(function () {
15 }).then(function (val) {
16 strictEqual(val, 42, "should return the handler value");
19 test('direct, deferred, success', {asserts: 1}, function () {
20 var s = openerp.testing.Stack();
21 return s.execute(function () {
23 }).then(function (val) {
24 strictEqual(val, 42, "should return the handler value")
27 test('direct, value, error', {asserts: 1}, function () {
28 var s = openerp.testing.Stack();
29 return s.execute(function () {
30 throw new Error("foo");
31 }).then(reject, function (f) {
32 strictEqual(f.message, "foo", "should reject with exception");
36 test('direct, deferred, failure', {asserts: 1}, function () {
37 var s = openerp.testing.Stack();
38 return s.execute(function () {
39 return $.Deferred(function (d) {
42 }).then(reject, function (f) {
43 strictEqual(f, "failed", "should propagate failure");
48 test('successful setup', {asserts: 2}, function () {
49 var setup_done = false;
50 var s = openerp.testing.Stack();
51 return s.push(function () {
52 return $.Deferred(function (d) {
53 setTimeout(function () {
58 }).execute(function () {
60 }).then(function (val) {
61 ok(setup_done, "should have executed setup");
62 strictEqual(val, 42, "should return executed function value (not setup)");
65 test('successful teardown', {asserts: 2}, function () {
67 var s = openerp.testing.Stack();
68 return s.push(null, function () {
69 return $.Deferred(function (d) {
70 setTimeout(function () {
75 }).execute(function () {
77 }).then(function (val) {
78 ok(teardown, "should have executed teardown");
79 strictEqual(val, 42, "should return executed function value (not setup)");
82 test('successful setup and teardown', {asserts: 3}, function () {
83 var setup = false, teardown = false;
84 var s = openerp.testing.Stack();
85 return s.push(function () {
86 return $.Deferred(function (d) {
87 setTimeout(function () {
93 return $.Deferred(function (d) {
94 setTimeout(function () {
99 }).execute(function () {
101 }).then(function (val) {
102 ok(setup, "should have executed setup");
103 ok(teardown, "should have executed teardown");
104 strictEqual(val, 42, "should return executed function value (not setup)");
108 test('multiple setups', {asserts: 2}, function () {
110 var s = openerp.testing.Stack();
111 return s.push(function () {
113 }).push(function () {
115 }).push(function () {
117 }).push(function () {
119 }).execute(function () {
121 }).then(function (val) {
122 strictEqual(setups, 4, "should have executed all setups of stack");
123 strictEqual(val, 42);
126 test('multiple teardowns', {asserts: 2}, function () {
128 var s = openerp.testing.Stack();
129 return s.push(null, function () {
131 }).push(null, function () {
133 }).push(null, function () {
135 }).push(null, function () {
137 }).execute(function () {
139 }).then(function (val) {
140 strictEqual(teardowns, 4, "should have executed all teardowns of stack");
141 strictEqual(val, 42);
144 test('holes in setups', {asserts: 2}, function () {
146 var s = openerp.testing.Stack();
147 return s.push(function () {
149 }).push().push().push(function () {
151 }).push(function () {
153 }).push().push(function () {
155 }).execute(function () {
157 }).then(function (val) {
158 deepEqual(setups, [0, 3, 4, 6],
159 "should have executed setups in correct order");
160 strictEqual(val, 42);
163 test('holes in teardowns', {asserts: 2}, function () {
165 var s = openerp.testing.Stack();
166 return s.push(null, function () {
168 }).push().push().push(null, function () {
170 }).push(null, function () {
172 }).push().push(null, function () {
174 }).execute(function () {
176 }).then(function (val) {
177 deepEqual(teardowns, [6, 4, 3, 0],
178 "should have executed teardowns in correct order");
179 strictEqual(val, 42);
184 test('failed setup', {asserts: 5}, function () {
185 var setup, teardown, teardown2, code;
186 return openerp.testing.Stack().push(function () {
190 }).push(function () {
191 return $.Deferred().reject("Fail!");
194 }).execute(function () {
197 }).then(reject, function (m) {
198 ok(setup, "should have executed first setup function");
199 ok(teardown, "should have executed first teardown function");
200 ok(!teardown2, "should not have executed second teardown function");
201 strictEqual(m, "Fail!", "should return setup failure message");
202 ok(!code, "should not have executed callback");
206 test('failed teardown', {asserts: 2}, function () {
208 return openerp.testing.Stack().push(null, function () {
210 return $.Deferred().reject('Fail 1');
211 }).push(null, function () {
213 }).push(null, function () {
215 return $.Deferred().reject('Fail 3');
216 }).execute(function () {
218 }).then(reject, function (m) {
219 strictEqual(teardowns, 3,
220 "should have tried executing all teardowns");
221 strictEqual(m, "Fail 3", "should return first failure message");
225 test('failed call + teardown', {asserts: 2}, function () {
227 return openerp.testing.Stack().push(null, function () {
229 }).push(null, function () {
231 return $.Deferred().reject('Fail 2');
232 }).execute(function () {
233 return $.Deferred().reject("code");
234 }).then(reject, function (m) {
235 strictEqual(teardowns, 2,
236 "should have tried executing all teardowns");
237 strictEqual(m, "code", "should return first failure message");
242 test('arguments passing', {asserts: 9}, function () {
243 var asserter = function (a, b, c) {
245 strictEqual(b, "foo");
246 deepEqual(c, {bar: "baz", qux: 42});
249 return openerp.testing.Stack()
250 .push(asserter, asserter)
251 .execute(asserter, 1, "foo", {bar: 'baz', qux: 42});