-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
304 lines (303 loc) · 9.92 KB
/
index.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var GasTap = /** @class */ (function () {
function GasTap(options) {
if (options === void 0) { options = null; }
/**
*
* GasT - Google Apps Script Testing-framework
*
* GasT is a TAP-compliant testing framework for Google Apps Script.
* It provides a simple way to verify that the GAS programs you write
* behave as expected.
*
* Github - https://github.com/zixia/gast
* Test Anything Protocol - http://testanything.org/
*
* Issues: https://github.com/zixia/gast/issues
* Author: Zhuohuan LI <[email protected]>
* Date: 2015-11-05
*
* Example:
```javascript
if ((typeof GasTap)==='undefined') { // GasT Initialization. (only if not initialized yet.)
eval(UrlFetchApp.fetch('https://raw.githubusercontent.com/zixia/gast/master/src/gas-tap-lib.js').getContentText())
} // Class GasTap is ready for use now!
var test = new GasTap()
```
*/
this.VERSION = '0.2.0';
this.totalSucc = 0;
this.totalFail = 0;
this.totalSkip = 0;
if (options && options.loggerFunc) {
this.loggerFunc = options.loggerFunc;
}
if (typeof (this.loggerFunc) != 'function')
throw Error('options.logger must be a function to accept output parameter');
this.print('TAP version GasTap v' + this.VERSION + '(BUGGY)');
}
// default output to gas logger.log
GasTap.prototype.loggerFunc = function (msg) {
Logger.log(msg);
};
;
Object.defineProperty(GasTap.prototype, "totalFailed", {
/***************************************************************
*
* Instance methods export
*
****************************************************************/
get: function () { return this.totalFail; },
enumerable: true,
configurable: true
});
Object.defineProperty(GasTap.prototype, "totalSucceed", {
get: function () { return this.totalSucc; },
enumerable: true,
configurable: true
});
Object.defineProperty(GasTap.prototype, "totalSkipped", {
get: function () { return this.totalSkip; },
enumerable: true,
configurable: true
});
GasTap.prototype.test = function (description, run) {
var t = new test(description, this);
try {
run(t);
}
catch (e /* if e instanceof String */) {
// Logger.log('caught exception: ' + e)
var SKIP_RE = new RegExp(t.EXCEPTION_SKIP);
var PASS_RE = new RegExp(t.EXCEPTION_PASS);
var FAIL_RE = new RegExp(t.EXCEPTION_FAIL);
switch (true) {
case SKIP_RE.test(e):
case PASS_RE.test(e):
case FAIL_RE.test(e):
break;
default:
if (e instanceof Error)
Logger.log('Stack:\n' + e.stack);
throw e;
}
}
finally {
this.totalSucc += t.succCounter;
this.totalFail += t.failCounter;
this.totalSkip += t.skipCounter;
}
};
GasTap.prototype.print = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var message = Utilities.formatString.apply(null, args);
this.loggerFunc(message);
};
/**
* Prints a total line to log output. For an example "3 tests, 0 failures"
*
* @returns tapResults
*/
GasTap.prototype.finish = function () {
var totalNum = this.totalSucc + this.totalFail + this.totalSkip;
var results = {
nTotal: totalNum,
nFailed: this.totalFail,
nSkipped: this.totalSkip,
nSucceeded: this.totalSucc
};
if (totalNum != (test.counter)) {
throw Error('test counting error!');
}
var msg = Utilities.formatString('%s..%s', Math.floor(totalNum) > 0 ? 1 : 0, Math.floor(totalNum));
this.print(msg);
msg = Utilities.formatString('%s tests, %s failures', Math.floor(totalNum), Math.floor(this.totalFail));
if (this.totalSkip > 0) {
msg += ', ' + Math.floor(this.totalSkip) + ' skipped';
}
this.print(msg);
return results;
};
return GasTap;
}());
var test = /** @class */ (function () {
/***************************************************************
*
* T 's functions
*
****************************************************************/
function test(desc, tap) {
this.succCounter = 0;
this.failCounter = 0;
this.skipCounter = 0;
this.description = 'unknown description';
this.EXCEPTION_SKIP = 'GasTapSkip';
this.EXCEPTION_PASS = 'GasTapPass';
this.EXCEPTION_FAIL = 'GasTapFail';
this.print = null;
if (desc)
this.description = desc;
this.print = tap.print.bind(tap);
}
test.prototype.tapOutput = function (ok, msg) {
this.print((ok ? 'ok' : 'not ok')
+ ' ' + ++test.counter
+ ' - ' + msg
+ ' - ' + this.description);
};
test.prototype.ok = function (value, msg) {
if (value) {
this.succCounter++;
this.tapOutput(true, msg);
}
else {
this.failCounter++;
this.tapOutput(false, msg);
}
};
test.prototype.notOk = function (value, msg) {
if (!value) {
this.succCounter++;
this.tapOutput(true, msg);
}
else {
this.failCounter++;
this.tapOutput(false, msg);
}
};
test.prototype.equal = function (v1, v2, msg) {
if (v1 == v2) {
this.succCounter++;
this.tapOutput(true, msg);
}
else {
this.failCounter++;
this.tapOutput(false, v1 + " not equal " + v2 + " - " + msg);
}
};
test.prototype.notEqual = function (v1, v2, msg) {
if (v1 != v2) {
this.succCounter++;
this.tapOutput(true, msg);
}
else {
this.failCounter++;
this.tapOutput(false, v1 + " equal " + v2 + " - " + msg);
}
};
test.prototype.deepEqual = function (v1, v2, msg) {
var isDeepEqual = recursionDeepEqual(v1, v2);
function recursionDeepEqual(rv1, rv2) {
if (!(rv1 instanceof Object) || !(rv2 instanceof Object))
return rv1 == rv2;
if (Object.keys(rv1).length != Object.keys(rv2).length)
return false;
for (var k in rv1) {
if (!(k in rv2)
|| ((typeof rv1[k]) != (typeof rv2[k])))
return false;
if (!recursionDeepEqual(rv1[k], rv2[k]))
return false;
}
return true;
}
if (isDeepEqual) {
this.succCounter++;
this.tapOutput(true, msg);
}
else {
this.failCounter++;
this.tapOutput(false, v1 + " not deepEqual " + v2 + " - " + msg);
}
};
test.prototype.notDeepEqual = function (v1, v2, msg) {
var isNotDeepEqual = recursionNotDeepEqual(v1, v2);
function recursionNotDeepEqual(rv1, rv2) {
if (!(rv1 instanceof Object) || !(rv2 instanceof Object))
return rv1 != rv2;
if (Object.keys(rv1).length != Object.keys(rv2).length)
return true;
for (var k in rv1) {
if (!(k in rv2)
|| ((typeof rv1[k]) != (typeof rv2[k])))
return true;
if (recursionNotDeepEqual(rv1[k], rv2[k]))
return true;
}
return false;
}
if (isNotDeepEqual) {
this.succCounter++;
this.tapOutput(true, msg);
}
else {
this.failCounter++;
this.tapOutput(false, v1 + " notDeepEqual " + v2 + " - " + msg);
}
};
test.prototype.nan = function (v1, msg) {
if (v1 !== v1) {
this.succCounter++;
this.tapOutput(true, msg);
}
else {
this.failCounter++;
this.tapOutput(false, v1 + " not is NaN - " + msg);
}
};
test.prototype.notNan = function (v1, msg) {
if (!(v1 !== v1)) {
this.succCounter++;
this.tapOutput(true, msg);
}
else {
this.failCounter++;
this.tapOutput(false, v1 + " is NaN - " + msg);
}
};
test.prototype.throws = function (fn, msg) {
try {
fn();
this.failCounter++;
this.tapOutput(false, 'exception wanted - ' + msg);
}
catch (e) {
this.succCounter++;
this.tapOutput(true, msg);
}
};
test.prototype.notThrow = function (fn, msg) {
try {
fn();
this.succCounter++;
this.tapOutput(true, msg);
}
catch (e) {
this.failCounter++;
this.tapOutput(false, 'unexpected exception:' + e.message + ' - ' + msg);
}
};
test.prototype.skip = function (msg) {
this.skipCounter++;
this.tapOutput(true, msg + ' # SKIP');
throw this.EXCEPTION_SKIP;
};
test.prototype.pass = function (msg) {
this.succCounter++;
this.tapOutput(true, msg + ' # PASS');
throw this.EXCEPTION_PASS;
};
test.prototype.fail = function (msg) {
this.failCounter++;
this.tapOutput(false, msg + ' # FAIL');
throw this.EXCEPTION_FAIL;
};
test.prototype.reset = function () {
this.succCounter = this.failCounter = this.skipCounter = 0;
this.description = 'unknown';
};
test.counter = 0;
return test;
}());