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, deferred, failure', {asserts: 1}, function () {
28 var s = openerp.testing.Stack();
29 return s.execute(function () {
30 return $.Deferred(function (d) {
33 }).then(reject, function (f) {
34 strictEqual(f, "failed", "should propagate failure");
39 test('successful setup', {asserts: 2}, function () {
40 var setup_done = false;
41 var s = openerp.testing.Stack();
42 return s.push(function () {
43 return $.Deferred(function (d) {
44 setTimeout(function () {
49 }).execute(function () {
51 }).then(function (val) {
52 ok(setup_done, "should have executed setup");
53 strictEqual(val, 42, "should return executed function value (not setup)");
56 test('successful teardown', {asserts: 2}, function () {
58 var s = openerp.testing.Stack();
59 return s.push(null, function () {
60 return $.Deferred(function (d) {
61 setTimeout(function () {
66 }).execute(function () {
68 }).then(function (val) {
69 ok(teardown, "should have executed teardown");
70 strictEqual(val, 42, "should return executed function value (not setup)");
73 test('successful setup and teardown', {asserts: 3}, function () {
74 var setup = false, teardown = false;
75 var s = openerp.testing.Stack();
76 return s.push(function () {
77 return $.Deferred(function (d) {
78 setTimeout(function () {
84 return $.Deferred(function (d) {
85 setTimeout(function () {
90 }).execute(function () {
92 }).then(function (val) {
93 ok(setup, "should have executed setup");
94 ok(teardown, "should have executed teardown");
95 strictEqual(val, 42, "should return executed function value (not setup)");
99 test('multiple setups', {asserts: 2}, function () {
101 var s = openerp.testing.Stack();
102 return s.push(function () {
104 }).push(function () {
106 }).push(function () {
108 }).push(function () {
110 }).execute(function () {
112 }).then(function (val) {
113 strictEqual(setups, 4, "should have executed all setups of stack");
114 strictEqual(val, 42);
117 test('multiple teardowns', {asserts: 2}, function () {
119 var s = openerp.testing.Stack();
120 return s.push(null, function () {
122 }).push(null, function () {
124 }).push(null, function () {
126 }).push(null, function () {
128 }).execute(function () {
130 }).then(function (val) {
131 strictEqual(teardowns, 4, "should have executed all teardowns of stack");
132 strictEqual(val, 42);
135 test('holes in setups', {asserts: 2}, function () {
137 var s = openerp.testing.Stack();
138 return s.push(function () {
140 }).push().push().push(function () {
142 }).push(function () {
144 }).push().push(function () {
146 }).execute(function () {
148 }).then(function (val) {
149 deepEqual(setups, [0, 3, 4, 6],
150 "should have executed setups in correct order");
151 strictEqual(val, 42);
154 test('holes in teardowns', {asserts: 2}, function () {
156 var s = openerp.testing.Stack();
157 return s.push(null, function () {
159 }).push().push().push(null, function () {
161 }).push(null, function () {
163 }).push().push(null, function () {
165 }).execute(function () {
167 }).then(function (val) {
168 deepEqual(teardowns, [6, 4, 3, 0],
169 "should have executed teardowns in correct order");
170 strictEqual(val, 42);
175 test('failed setup', {asserts: 5}, function () {
176 var setup, teardown, teardown2, code;
177 return openerp.testing.Stack().push(function () {
181 }).push(function () {
182 return $.Deferred().reject("Fail!");
185 }).execute(function () {
188 }).then(reject, function (m) {
189 ok(setup, "should have executed first setup function");
190 ok(teardown, "should have executed first teardown function");
191 ok(!teardown2, "should not have executed second teardown function");
192 strictEqual(m, "Fail!", "should return setup failure message");
193 ok(!code, "should not have executed callback");
197 test('failed teardown', {asserts: 2}, function () {
199 return openerp.testing.Stack().push(null, function () {
201 return $.Deferred().reject('Fail 1');
202 }).push(null, function () {
204 }).push(null, function () {
206 return $.Deferred().reject('Fail 3');
207 }).execute(function () {
209 }).then(reject, function (m) {
210 strictEqual(teardowns, 3,
211 "should have tried executing all teardowns");
212 strictEqual(m, "Fail 3", "should return first failure message");
216 test('failed call + teardown', {asserts: 2}, function () {
218 return openerp.testing.Stack().push(null, function () {
220 }).push(null, function () {
222 return $.Deferred().reject('Fail 2');
223 }).execute(function () {
224 return $.Deferred().reject("code");
225 }).then(reject, function (m) {
226 strictEqual(teardowns, 2,
227 "should have tried executing all teardowns");
228 strictEqual(m, "code", "should return first failure message");
233 test('arguments passing', {asserts: 9}, function () {
234 var asserter = function (a, b, c) {
236 strictEqual(b, "foo");
237 deepEqual(c, {bar: "baz", qux: 42});
240 return openerp.testing.Stack()
241 .push(asserter, asserter)
242 .execute(asserter, 1, "foo", {bar: 'baz', qux: 42});