-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
221 lines (189 loc) · 6.59 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict';
var generateDescribe = require("./lib/generate");
var SETUP_METHODS = generateDescribe.SETUP_METHODS;
var IT_METHODS = generateDescribe.IT_METHODS;
var THAT_METHODS = generateDescribe.THAT_METHODS;
var SET_METHODS = generateDescribe.SET_METHODS;
var REPLACE_METHOD = generateDescribe.REPLACE_METHOD;
var _ = require("underscore");
var utils = require("./lib/utils");
var Module = function(name,ctx,f) {
// Jasmine fallback
if (!global.hasOwnProperty("describe")) {
throw Error("There is no 'describe' method in your global. Probably mocha isn't running.");
}
if (name instanceof Function) {
ctx = name;
name = "";
}
if (ctx instanceof Function) {
f = ctx;
ctx = {};
}
if (ctx && !(ctx instanceof Object)) {
throw Error("Second argument should be object if set");
}
if (f && !(f instanceof Function)) {
throw Error("Third argument should be function if set");
}
name = name || "";
var TestSuit = function Suit(msg,_ctx){
if (utils.isSuitInstance(this)){
if (msg instanceof Object) {
_ctx = msg;
msg = "";
}
msg = msg || "";
var parents = [];
var CurrentSuit = this[utils.SUIT_PROPERTY];
var ctx = utils.getSuitContextObject(CurrentSuit,{});
while(utils.isSuit(CurrentSuit)) {
parents.unshift(CurrentSuit);
CurrentSuit = CurrentSuit.parent;
}
var modifiers = {
insertAbove: [],
insertBelow: [],
replaceWith: []
};
parents.forEach(function(Suit){
utils.extend(ctx,Suit.contextData);
modifiers.insertAbove = (Suit.__insertAbove || []).concat(modifiers.insertAbove);
modifiers.insertBelow = modifiers.insertBelow.concat(Suit.__insertBelow || []);
modifiers.replaceWith = (Suit.__replaceWith || []).concat(modifiers.replaceWith);
});
utils.extend(ctx,_ctx);
generateDescribe(parents,ctx,utils.dotEndString(msg),modifiers);
} else {
utils.generateObject(TestSuit,arguments);
}
};
utils.extendSuit(TestSuit,{
parent: null,
fcall: f,
describe: name,
contextData: ctx
});
SETUP_METHODS.forEach(function(callName){
TestSuit[callName] = function(a,b) {
if (utils.isSuit(a)) {
utils.pushNewCall(this,callName, { suit: a, args: b });
} else {
if (typeof a !== "string") {
b = a;
a = undefined;
}
utils.pushNewCall(this,callName, { msg: a, fcall: b });
}
return this;
};
});
IT_METHODS.concat(THAT_METHODS).forEach(function(callName){
TestSuit[callName] = function(a,b) {
if (utils.isSuit(a)) {
utils.pushNewCall(this,callName, { suit: a, args: b });
} else {
a = String(a);
utils.pushNewCall(this,callName, { msg: a, fcall: b });
}
return this;
};
});
SET_METHODS.forEach(function(callName) {
TestSuit[callName] = function(suit,newSuit, args){
if (!utils.isSuit(suit)) {
throw new Error(callName+" first argument should be suit");
}
if (!utils.isSuit(newSuit)) {
throw new Error(callName +" second argument should be suit");
}
utils.pushNewCall(this,callName, { targetSuit: suit, newSuit: newSuit, args: args });
return this;
};
});
TestSuit.replaceWith = function(suit,newSuit,args){
if (!utils.isSuit(suit)) {
throw new Error("replaceWith first argument should be suit");
}
if (!utils.isSuit(newSuit)) {
throw new Error("replaceWith second argument should be suit");
}
utils.pushNewCall(this,"replaceWith", { targetSuit: suit, newSuit: newSuit, args: args });
return this;
};
TestSuit.with = function(suit,args){
if (!utils.isSuit(suit)) {
throw Error("Argument should be Suit class object");
}
var self = this;
SETUP_METHODS.concat(IT_METHODS).concat(THAT_METHODS).forEach(function(method){
self[method](suit,args);
});
return this;
};
TestSuit.extend = function(msg,ctx,f) {
if (ctx instanceof Function) {
f = ctx;
ctx = {};
}
if (ctx && !(ctx instanceof Object)) {
throw Error("Second argument should be object if set");
}
if (f && !(f instanceof Function)) {
throw Error("Third argument should be function if set");
}
var Parent = this;
var NewSuit = function Suit() {
if (utils.isSuitInstance(this)) {
Parent.apply(this,arguments);
}
else {
utils.generateObject(NewSuit,arguments);
}
};
utils.extend(NewSuit,Parent);
utils.extendSuit(NewSuit,{
parent: Parent,
fcall: f,
describe: msg || "",
contextData: ctx
});
return NewSuit;
};
TestSuit.xtend = function(msg,ctx,f) {
var NewSuit = this.extend.apply(this,arguments);
Object.defineProperty(NewSuit,"skip", {value: true});
return NewSuit;
};
TestSuit.bindTo = function(obj) {
utils.bindTo(this,obj);
};
TestSuit.unbind = function() {
utils.unbind(this);
};
TestSuit.copy = function(description,__ctx) {
var NewSuit;
var OldSuit = this;
if (OldSuit.parent) {
NewSuit = OldSuit.parent.extend(description,__ctx);
} else {
NewSuit = Module(description,__ctx);
}
Object.keys(OldSuit).forEach(function(key) {
(utils.getCallList(OldSuit,key) || []).forEach(function(f) {
var newDescriptor = Object.assign({},f);
if (newDescriptor.newSuit) {
newDescriptor.newSuit = newDescriptor.newSuit.copy();
}
if (newDescriptor.suit) {
newDescriptor.suit = newDescriptor.suit.copy();
}
utils.pushNewCall(NewSuit,key,newDescriptor);
});
});
NewSuit.bindTo(utils.boundTo(OldSuit));
return NewSuit;
};
return TestSuit;
};
module.exports = Module;