-
Notifications
You must be signed in to change notification settings - Fork 79
/
behave.js
201 lines (172 loc) · 4.6 KB
/
behave.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
var suites = [],
output = [],
specCount = 0,
failures = 0,
successes = 0;
//add to log output
function log(text) {
output.push('[behave] '+text);
}
function Suite(descText) {
this.desc = descText
this.specs = [];
}
Suite.prototype.evaluate = function(cb) {
log('Describing '+this.desc+':');
var executing = false,
that = this;
var timer = setInterval(function() {
if (that.specs.length > 0 && !executing) {
executing = true;
var s = that.specs.shift();
s.evaluate(function() {
executing = false;
});
}
else if (that.specs.length === 0 && !executing) {
clearInterval(timer);
cb();
}
},50);
};
Suite.prototype.addSpec = function(spec) {
this.specs.push(spec);
};
function Spec(descText, async, timeout) {
this.desc = descText;
this.async = async;
this.timeout = timeout;
this.expectations = [];
}
Spec.prototype.addExpectation = function(ex) {
this.expectations.push(ex);
};
Spec.prototype.evaluate = function(cb) {
log('it '+this.desc);
specCount++;
if (this.async) {
var time = 0,
that = this;
var timer = setInterval(function() {
if (that.expectations.length > 0 && that.done) {
var ex = that.expectations.shift();
ex.evaluate();
}
else if ((that.expectations.length === 0 && that.done) || (time > that.timeout||10000)) {
clearInterval(timer);
cb();
}
time=time+50;
},50);
}
else {
for (var i = 0, l = this.expectations.length; i<l; i++) {
var ex = this.expectations[i];
ex.evaluate();
}
cb();
}
};
function Expectation(v) {
this.someValue = v;
};
//Matchers
Expectation.prototype.toBe = function(other) {
this.comparisonText = 'to be';
this.otherValue = other;
this.satisfied = this.someValue === this.otherValue;
};
Expectation.prototype.notToBe = function(other) {
this.comparisonText = 'not to be';
this.otherValue = other;
this.satisfied = this.someValue !== this.otherValue;
};
Expectation.prototype.toMatch = function(regex) {
this.comparisonText = 'to match';
this.otherValue = regex;
this.satisfied = regex.test(this.someValue);
};
Expectation.prototype.notToMatch = function(regex) {
this.comparisonText = 'not to match';
this.otherValue = regex;
this.satisfied = !regex.test(this.someValue);
};
Expectation.prototype.evaluate = function() {
if (this.satisfied) {
successes++;
log('I expected '+this.someValue+' '+ this.comparisonText +' '+this.otherValue);
}
else {
failures++;
log('I incorrectly got '+this.someValue+', when I expected '+this.otherValue);
}
};
//Configure the global object of a test suite with the necessary functions
exports.andSetup = function(global) {
//Create the BDD interface on the global object
global.describe = function(suiteDesc, suiteClosure) {
//create a new suite object for the scope of the current "describe"
var SUITE = new Suite(suiteDesc);
suites.push(SUITE);
//Now, create a new global "it" which has SUITE in scope
global.it = function(specDesc, specClosure) {
var SPEC = new Spec(specDesc);
SUITE.addSpec(SPEC);
//Now, create a new global "expect" which has SPEC in scope
global.expect = function(someValue) {
var ex = new Expectation(someValue);
SPEC.addExpectation(ex);
return ex;
};
//now run spec
specClosure();
SPEC.done = true;
};
//Async specs
global.it.eventually = function(specDesc, specClosure, timeout) {
var SPEC = new Spec(specDesc, true, timeout);
SUITE.addSpec(SPEC);
//Now, create a new global "expect" which has SPEC in scope
global.expect = function(someValue) {
var ex = new Expectation(someValue);
SPEC.addExpectation(ex);
return ex;
};
//now run spec
specClosure(function() {
SPEC.done = true;
});
};
//now run suite
suiteClosure();
};
};
//Report on the suites that have been added
exports.run = function() {
specCount = 0;
failures = 0;
successes = 0;
output = [];
log('');
log('Oh, behave! Testing in progress...');
var executing = false;
var timer = setInterval(function() {
if (suites.length > 0 && !executing) {
executing = true;
var s = suites.shift();
s.evaluate(function() {
executing = false;
});
}
else if (suites.length === 0 && !executing) {
log('');
log('*******************************************');
log('* \\o/ T E S T R U N C O M P L E T E \\o/ *');
log('*******************************************');
log('You ran '+specCount+' specs with '+failures+' failures and '+successes+' successes.');
//Flush output
Ti.API.info(output.join('\n'));
clearInterval(timer);
}
},50);
};