1 |
|
2 | var AssertionError, Does, Loader, colors, config, deferred, does, facto, ipso, loadModules, loadModulesSync, parallel, should, util, _ref, _ref1;
|
3 |
|
4 | _ref = require('also'), util = _ref.util, deferred = _ref.deferred, parallel = _ref.parallel;
|
5 |
|
6 | AssertionError = require('assert').AssertionError;
|
7 |
|
8 | facto = require('facto');
|
9 |
|
10 | Loader = require('./loader');
|
11 |
|
12 | colors = require('colors');
|
13 |
|
14 | Does = require('does');
|
15 |
|
16 | does = Does({
|
17 | does: {
|
18 | mode: 'spec'
|
19 | }
|
20 | });
|
21 |
|
22 | should = require('should');
|
23 |
|
24 | config = {
|
25 | dir: process.cwd(),
|
26 | modules: {}
|
27 | };
|
28 |
|
29 | _ref1 = Loader.create(config), loadModules = _ref1.loadModules, loadModulesSync = _ref1.loadModulesSync;
|
30 |
|
31 | module.exports = ipso = function(actualTestFunction) {
|
32 | var testFunctionForMocha;
|
33 | return testFunctionForMocha = function(done) {
|
34 | var Module, arg1, argsToInjectIntoTest, fnArgsArray, testResolver, _i, _len, _ref2,
|
35 | _this = this;
|
36 | fnArgsArray = util.argsOf(actualTestFunction);
|
37 | argsToInjectIntoTest = [];
|
38 | if (done == null) {
|
39 | if (fnArgsArray[0] === 'done' || fnArgsArray[0] === 'facto') {
|
40 | console.log('ipso cannot inject done into describe() or context()'.red);
|
41 | return;
|
42 | }
|
43 | does.activate({
|
44 | context: this,
|
45 | mode: 'spec',
|
46 | spec: null,
|
47 | resolver: null
|
48 | });
|
49 | _ref2 = loadModulesSync(fnArgsArray, does);
|
50 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
51 | Module = _ref2[_i];
|
52 | argsToInjectIntoTest.push(Module);
|
53 | }
|
54 | actualTestFunction.apply(this, argsToInjectIntoTest);
|
55 | return;
|
56 | }
|
57 | does.activate({
|
58 | context: this,
|
59 | mode: 'spec',
|
60 | spec: this.test,
|
61 | resolver: done
|
62 | });
|
63 | testResolver = function(metadata) {
|
64 | return does.assert(done).then(function(result) {
|
65 | if (fnArgsArray[0] === 'facto') {
|
66 | facto(metadata);
|
67 | }
|
68 | return done();
|
69 | }, function(error) {
|
70 | if (fnArgsArray[0] === 'facto') {
|
71 | return facto(metadata);
|
72 | }
|
73 | }, function(notify) {});
|
74 | };
|
75 | if (fnArgsArray[0] === 'done' || fnArgsArray[0] === 'facto') {
|
76 | argsToInjectIntoTest.push(testResolver);
|
77 | arg1 = fnArgsArray.shift();
|
78 | }
|
79 | return loadModules(fnArgsArray, does).then(function(Modules) {
|
80 | var error, promise, _j, _len1;
|
81 | for (_j = 0, _len1 = Modules.length; _j < _len1; _j++) {
|
82 | Module = Modules[_j];
|
83 | argsToInjectIntoTest.push(Module);
|
84 | }
|
85 | try {
|
86 | promise = actualTestFunction.apply(_this, argsToInjectIntoTest);
|
87 | } catch (_error) {
|
88 | error = _error;
|
89 | does.reset().then(function() {
|
90 | return done(error);
|
91 | });
|
92 | return;
|
93 | }
|
94 | if (arg1 !== 'done' && arg1 !== 'facto') {
|
95 | try {
|
96 | if ((promise.then != null) && _this.test.type === 'test') {
|
97 | return does.reset().then(function() {
|
98 | return done(new Error('Synchronous test returned promise. Inject test resolver (done or facto).'));
|
99 | });
|
100 | }
|
101 | } catch (_error) {}
|
102 | testResolver();
|
103 | return;
|
104 | }
|
105 | try {
|
106 | if (promise.then != null) {
|
107 | return promise.then((function() {}), function(error) {
|
108 | return does.reset().then(function() {
|
109 | return done(error);
|
110 | });
|
111 | });
|
112 | }
|
113 | } catch (_error) {}
|
114 | }, function(error) {
|
115 | return does.reset().then(function() {
|
116 | return done(error);
|
117 | });
|
118 | });
|
119 | };
|
120 | };
|
121 |
|
122 | ipso.ipso = ipso;
|
123 |
|
124 | ipso.mock = function(name) {
|
125 | var object;
|
126 | object = {
|
127 | title: name,
|
128 | is: function(mock) {
|
129 | if (typeof mock === 'object') {
|
130 | return object.should.equal(mock);
|
131 | }
|
132 | return name.should.equal(mock);
|
133 | },
|
134 | "with": function(list) {
|
135 | var key;
|
136 | for (key in list) {
|
137 | object[key] = list[key];
|
138 | }
|
139 | return object;
|
140 | }
|
141 | };
|
142 | return does.spectateSync({
|
143 | name: name,
|
144 | tagged: true
|
145 | }, object);
|
146 | };
|
147 |
|
148 | ipso.Mock = function(name) {
|
149 | var klass, mockObject;
|
150 | mockObject = ipso.mock(name);
|
151 | return klass = (function() {
|
152 | _Class["with"] = function() {
|
153 | mockObject["with"].apply(this, arguments);
|
154 | return klass;
|
155 | };
|
156 |
|
157 | function _Class() {
|
158 | var stub, stubs;
|
159 | stubs = does.getSync(name).object;
|
160 | if (typeof stubs.$constructor === 'function') {
|
161 | stubs.$constructor.apply(this, arguments);
|
162 | }
|
163 | for (stub in stubs) {
|
164 | if (stub === '$constructor') {
|
165 | continue;
|
166 | }
|
167 | this[stub] = stubs[stub];
|
168 | }
|
169 | }
|
170 |
|
171 | return _Class;
|
172 |
|
173 | })();
|
174 | };
|
175 |
|
176 | ipso.tag = deferred(function(action, list) {
|
177 | var tag;
|
178 | return parallel((function() {
|
179 | var _results;
|
180 | _results = [];
|
181 | for (tag in list) {
|
182 | _results.push((function(tag) {
|
183 | return function() {
|
184 | return does.spectateSync({
|
185 | name: tag,
|
186 | tagged: true
|
187 | }, list[tag]);
|
188 | };
|
189 | })(tag));
|
190 | }
|
191 | return _results;
|
192 | })()).then(action.resolve, action.reject, action.notify);
|
193 | });
|
194 |
|
195 | ipso.define = require('./define');
|
196 |
|
197 | ipso.does = does;
|
198 |
|
199 | module.exports.once = function(fn) {
|
200 | return (function(done) {
|
201 | return function() {
|
202 | if (done) {
|
203 | return;
|
204 | }
|
205 | done = true;
|
206 | return fn.apply(this, arguments);
|
207 | };
|
208 | })(false);
|
209 | };
|