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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
// *********************** Test Framework of Sorts ************************* //
function loadScript(url) {
// disallow loading remote URLs
if((url.substr(0, 7) == "http://")
|| (url.substr(0, 2) == "//")
|| (url.substr(0, 5) == "data:")
|| (url.substr(0, 11) == "javsacript:")) {
throw "Not loading remote test scripts";
}
if (typeof document != "undefined") document.write('<script src="'+url+'"></script>');
};
function patchTest(fun) {
var source = fun.toString();
var output = "";
var i = 0;
var testMarker = "T("
while (i < source.length) {
var testStart = source.indexOf(testMarker, i);
if (testStart == -1) {
output = output + source.substring(i, source.length);
break;
}
var testEnd = source.indexOf(");", testStart);
var testCode = source.substring(testStart + testMarker.length, testEnd);
output += source.substring(i, testStart) + "T(" + testCode + "," + JSON.stringify(testCode);
i = testEnd;
}
try {
return eval("(" + output + ")");
} catch (e) {
return null;
}
}
function runAllTests() {
var rows = $("#tests tbody.content tr");
$("td", rows).text("");
$("td.status", rows).removeClass("error").removeClass("failure").removeClass("success").text("not run");
var offset = 0;
function runNext() {
if (offset < rows.length) {
var row = rows.get(offset);
runTest($("th button", row).get(0), function() {
offset += 1;
setTimeout(runNext, 100);
}, false, true);
} else {
saveTestReport();
}
}
runNext();
}
var numFailures = 0;
var currentRow = null;
function runTest(button, callback, debug, noSave) {
// offer to save admins
if (currentRow != null) {
alert("Can not run multiple tests simultaneously.");
return;
}
var row = currentRow = $(button).parents("tr").get(0);
$("td.status", row).removeClass("error").removeClass("failure").removeClass("success");
$("td", row).text("");
$("#toolbar li.current").text("Running: "+row.id);
var testFun = couchTests[row.id];
function run() {
numFailures = 0;
var start = new Date().getTime();
try {
if (debug == undefined || !debug) {
testFun = patchTest(testFun) || testFun;
}
testFun(debug);
var status = numFailures > 0 ? "failure" : "success";
} catch (e) {
var status = "error";
if ($("td.details ol", row).length == 0) {
$("<ol></ol>").appendTo($("td.details", row));
}
$("<li><b>Exception raised:</b> <code class='error'></code></li>")
.find("code").text(JSON.stringify(e)).end()
.appendTo($("td.details ol", row));
if (debug) {
currentRow = null;
throw e;
}
}
if ($("td.details ol", row).length) {
$("<a href='#'>Run with debugger</a>").click(function() {
runTest(this, undefined, true);
}).prependTo($("td.details ol", row));
}
var duration = new Date().getTime() - start;
$("td.status", row).removeClass("running").addClass(status).text(status);
$("td.duration", row).text(duration + "ms");
$("#toolbar li.current").text("Finished: "+row.id);
updateTestsFooter();
currentRow = null;
if (callback) callback();
if (!noSave) saveTestReport();
}
$("td.status", row).addClass("running").text("running…");
setTimeout(run, 100);
}
function showSource(cell) {
var name = $(cell).text();
var win = window.open("", name, "width=700,height=500,resizable=yes,scrollbars=yes");
win.document.location = "script/test/" + name + ".js";
}
var readyToRun;
function setupAdminParty(fun) {
if (readyToRun) {
fun();
} else {
function removeAdmins(confs, doneFun) {
// iterate through the config and remove current user last
// current user is at front of list
var remove = confs.pop();
if (remove) {
$.couch.config({
success : function() {
removeAdmins(confs, doneFun);
}
}, "admins", remove[0], null);
} else {
doneFun();
}
};
$.couch.session({
success : function(resp) {
var userCtx = resp.userCtx;
if (userCtx.name && userCtx.roles.indexOf("_admin") != -1) {
// admin but not admin party. dialog offering to make admin party
$.showDialog("dialog/_admin_party.html", {
submit: function(data, callback) {
$.couch.config({
success : function(conf) {
var meAdmin, adminConfs = [];
for (var name in conf) {
if (name == userCtx.name) {
meAdmin = [name, conf[name]];
} else {
adminConfs.push([name, conf[name]]);
}
}
adminConfs.unshift(meAdmin);
removeAdmins(adminConfs, function() {
callback();
$.futon.session.sidebar();
readyToRun = true;
setTimeout(fun, 500);
});
}
}, "admins");
}
});
} else if (userCtx.roles.indexOf("_admin") != -1) {
// admin party!
readyToRun = true;
fun();
} else {
// not an admin
alert("Error: You need to be an admin to run the tests.");
};
}
});
}
};
function updateTestsListing() {
for (var name in couchTests) {
var testFunction = couchTests[name];
var row = $("<tr><th></th><td></td><td></td><td></td></tr>")
.find("th").text(name).attr("title", "Show source").click(function() {
showSource(this);
}).end()
.find("td:nth(0)").addClass("status").text("not run").end()
.find("td:nth(1)").addClass("duration").end()
.find("td:nth(2)").addClass("details").end();
$("<button type='button' class='run' title='Run test'></button>").click(function() {
this.blur();
var self = this;
// check for admin party
setupAdminParty(function() {
runTest(self);
});
return false;
}).prependTo(row.find("th"));
row.attr("id", name).appendTo("#tests tbody.content");
}
$("#tests tr").removeClass("odd").filter(":odd").addClass("odd");
updateTestsFooter();
}
function updateTestsFooter() {
var tests = $("#tests tbody.content tr td.status");
var testsRun = tests.filter(".success, .error, .failure");
var testsFailed = testsRun.not(".success");
var totalDuration = 0;
$("#tests tbody.content tr td.duration:contains('ms')").each(function() {
var text = $(this).text();
totalDuration += parseInt(text.substr(0, text.length - 2), 10);
});
$("#tests tbody.footer td").html("<span>"+testsRun.length + " of " + tests.length +
" test(s) run, " + testsFailed.length + " failures (" +
totalDuration + " ms)</span> ");
}
// make report and save to local db
// display how many reports need replicating to the mothership
// have button to replicate them
function saveTestReport(report) {
var report = makeTestReport();
if (report) {
var db = $.couch.db("test_suite_reports");
var saveReport = function(db_info) {
report.db = db_info;
$.couch.info({success : function(node_info) {
report.node = node_info;
db.saveDoc(report);
}})
};
var createDb = function() {
db.create({success: function() {
db.info({success:saveReport});
}});
}
db.info({error: createDb, success:saveReport});
}
};
function makeTestReport() {
var report = {};
report.summary = $("#tests tbody.footer td").text();
report.platform = testPlatform();
var date = new Date();
report.timestamp = date.getTime();
report.timezone = date.getTimezoneOffset();
report.tests = [];
$("#tests tbody.content tr").each(function() {
var status = $("td.status", this).text();
if (status != "not run") {
var test = {};
test.name = this.id;
test.status = status;
test.duration = parseInt($("td.duration", this).text());
test.details = [];
$("td.details li", this).each(function() {
test.details.push($(this).text());
});
if (test.details.length == 0) {
delete test.details;
}
report.tests.push(test);
}
});
if (report.tests.length > 0) return report;
};
function testPlatform() {
var b = $.browser;
var bs = ["mozilla", "msie", "opera", "safari"];
for (var i=0; i < bs.length; i++) {
if (b[bs[i]]) {
return {"browser" : bs[i], "version" : b.version};
}
};
return {"browser" : "undetected"};
}
function reportTests() {
// replicate the database to couchdb.couchdb.org
}
// Use T to perform a test that returns false on failure and if the test fails,
// display the line that failed.
// Example:
// T(MyValue==1);
function T(arg1, arg2, testName) {
if (!arg1) {
if (currentRow) {
if ($("td.details ol", currentRow).length == 0) {
$("<ol></ol>").appendTo($("td.details", currentRow));
}
var message = (arg2 != null ? arg2 : arg1).toString();
$("<li><b>Assertion " + (testName ? "'" + testName + "'" : "") + " failed:</b> <code class='failure'></code></li>")
.find("code").text(message).end()
.appendTo($("td.details ol", currentRow));
}
numFailures += 1
}
}
function TEquals(expected, actual, testName) {
T(equals(expected, actual), "expected '" + repr(expected) +
"', got '" + repr(actual) + "'", testName);
}
function equals(a,b) {
if (a === b) return true;
try {
return repr(a) === repr(b);
} catch (e) {
return false;
}
}
function repr(val) {
if (val === undefined) {
return null;
} else if (val === null) {
return "null";
} else {
return JSON.stringify(val);
}
}
function makeDocs(start, end, templateDoc) {
var templateDocSrc = templateDoc ? JSON.stringify(templateDoc) : "{}"
if (end === undefined) {
end = start;
start = 0;
}
var docs = []
for (var i = start; i < end; i++) {
var newDoc = eval("(" + templateDocSrc + ")");
newDoc._id = (i).toString();
newDoc.integer = i;
newDoc.string = (i).toString();
docs.push(newDoc)
}
return docs;
}
function run_on_modified_server(settings, fun) {
try {
// set the settings
for(var i=0; i < settings.length; i++) {
var s = settings[i];
var xhr = CouchDB.request("PUT", "/_config/" + s.section + "/" + s.key, {
body: JSON.stringify(s.value),
headers: {"X-Couch-Persist": "false"}
});
CouchDB.maybeThrowError(xhr);
s.oldValue = xhr.responseText;
}
// run the thing
fun();
} finally {
// unset the settings
for(var j=0; j < i; j++) {
var s = settings[j];
if(s.oldValue == "\"\"\n") { // unset value
CouchDB.request("DELETE", "/_config/" + s.section + "/" + s.key, {
headers: {"X-Couch-Persist": "false"}
});
} else {
CouchDB.request("PUT", "/_config/" + s.section + "/" + s.key, {
body: s.oldValue,
headers: {"X-Couch-Persist": "false"}
});
}
}
}
}
function stringFun(fun) {
var string = fun.toSource ? fun.toSource() : "(" + fun.toString() + ")";
return string;
}
function waitForSuccess(fun, tag) {
var start = new Date();
while(true) {
if (new Date() - start > 5000) {
throw("timeout: "+tag);
} else {
try {
fun();
break;
} catch (e) {}
// sync http req allow async req to happen
CouchDB.request("GET", "/test_suite_db/?tag="+encodeURIComponent(tag));
}
}
}
function waitForRestart() {
var waiting = true;
while (waiting) {
try {
CouchDB.request("GET", "/");
CouchDB.request("GET", "/");
waiting = false;
} catch(e) {
// the request will fail until restart completes
}
}
};
function restartServer() {
var xhr;
try {
CouchDB.request("POST", "/_restart");
} catch(e) {
// this request may sometimes fail
}
waitForRestart();
}
|