support async tests

pull/1/head
ansuz 8 years ago
parent 922e7803e9
commit 6a1c799a6e

@ -15,26 +15,40 @@ define([
var failMessages = []; var failMessages = [];
var ASSERTS = []; var ASSERTS = [];
var runASSERTS = function () { var runASSERTS = function (cb) {
var count = ASSERTS.length;
var successes = 0;
var done = function (err, index) {
count--;
if (err) { failMessages.push(err); }
else { successes++; }
if (count === 0) { cb(); }
};
ASSERTS.forEach(function (f, index) { ASSERTS.forEach(function (f, index) {
f(index); f(function (err) {
done(err, index);
}, index);
}); });
}; };
var assert = function (test, msg) { var assert = function (test, msg) {
ASSERTS.push(function (i) { ASSERTS.push(function (cb, i) {
var returned = test(); test(function (result) {
if (returned === true) { if (result === true) {
assertions++; assertions++;
} else { cb();
failed = true; } else {
failedOn = assertions; failed = true;
failMessages.push({ failedOn = assertions;
test: i, cb({
message: msg, test: i,
output: returned, message: msg,
}); output: result,
} });
}
});
}); });
}; };
@ -58,7 +72,7 @@ define([
}; };
var HJSON_equal = function (shjson) { var HJSON_equal = function (shjson) {
assert(function () { assert(function (cb) {
// parse your stringified Hyperjson // parse your stringified Hyperjson
var hjson; var hjson;
@ -82,10 +96,10 @@ define([
var diff = TextPatcher.format(shjson, op); var diff = TextPatcher.format(shjson, op);
if (success) { if (success) {
return true; return cb(true);
} else { } else {
return '<br><br>insert: ' + diff.insert + '<br><br>' + return cb('<br><br>insert: ' + diff.insert + '<br><br>' +
'remove: ' + diff.remove + '<br><br>'; 'remove: ' + diff.remove + '<br><br>');
} }
}, "expected hyperjson equality"); }, "expected hyperjson equality");
}; };
@ -94,7 +108,7 @@ define([
var roundTrip = function (sel) { var roundTrip = function (sel) {
var target = $(sel)[0]; var target = $(sel)[0];
assert(function () { assert(function (cb) {
var hjson = Hyperjson.fromDOM(target); var hjson = Hyperjson.fromDOM(target);
var cloned = Hyperjson.toDOM(hjson); var cloned = Hyperjson.toDOM(hjson);
var success = cloned.outerHTML === target.outerHTML; var success = cloned.outerHTML === target.outerHTML;
@ -111,7 +125,7 @@ define([
TextPatcher.log(target.outerHTML, op); TextPatcher.log(target.outerHTML, op);
} }
return success; return cb(success);
}, "Round trip serialization introduced artifacts."); }, "Round trip serialization introduced artifacts.");
}; };
@ -125,9 +139,9 @@ define([
var strungJSON = function (orig) { var strungJSON = function (orig) {
var result; var result;
assert(function () { assert(function (cb) {
result = JSON.stringify(JSON.parse(orig)); result = JSON.stringify(JSON.parse(orig));
return result === orig; return cb(result === orig);
}, "expected result (" + result + ") to equal original (" + orig + ")"); }, "expected result (" + result + ") to equal original (" + orig + ")");
}; };
@ -138,46 +152,56 @@ define([
}); });
// check that old hashes parse correctly // check that old hashes parse correctly
assert(function () { assert(function (cb) {
var secret = Cryptpad.parseHash('67b8385b07352be53e40746d2be6ccd7XAYSuJYYqa9NfmInyHci7LNy'); var secret = Cryptpad.parseHash('67b8385b07352be53e40746d2be6ccd7XAYSuJYYqa9NfmInyHci7LNy');
return secret.channel === "67b8385b07352be53e40746d2be6ccd7" && return cb(secret.channel === "67b8385b07352be53e40746d2be6ccd7" &&
secret.key === "XAYSuJYYqa9NfmInyHci7LNy" && secret.key === "XAYSuJYYqa9NfmInyHci7LNy" &&
secret.version === 0; secret.version === 0);
}, "Old hash failed to parse"); }, "Old hash failed to parse");
// make sure version 1 hashes parse correctly // make sure version 1 hashes parse correctly
assert(function () { assert(function (cb) {
var secret = Cryptpad.parseHash('/1/edit/3Ujt4F2Sjnjbis6CoYWpoQ/usn4+9CqVja8Q7RZOGTfRgqI'); var secret = Cryptpad.parseHash('/1/edit/3Ujt4F2Sjnjbis6CoYWpoQ/usn4+9CqVja8Q7RZOGTfRgqI');
return secret.version === 1 && return cb(secret.version === 1 &&
secret.mode === "edit" && secret.mode === "edit" &&
secret.channel === "3Ujt4F2Sjnjbis6CoYWpoQ" && secret.channel === "3Ujt4F2Sjnjbis6CoYWpoQ" &&
secret.key === "usn4+9CqVja8Q7RZOGTfRgqI" && secret.key === "usn4+9CqVja8Q7RZOGTfRgqI" &&
!secret.present; !secret.present);
}, "version 1 hash failed to parse"); }, "version 1 hash failed to parse");
// test support for present mode in hashes // test support for present mode in hashes
assert(function () { assert(function (cb) {
var secret = Cryptpad.parseHash('/1/edit/CmN5+YJkrHFS3NSBg-P7Sg/DNZ2wcG683GscU4fyOyqA87G/present'); var secret = Cryptpad.parseHash('/1/edit/CmN5+YJkrHFS3NSBg-P7Sg/DNZ2wcG683GscU4fyOyqA87G/present');
return secret.version === 1 return cb(secret.version === 1
&& secret.mode === "edit" && secret.mode === "edit"
&& secret.channel === "CmN5+YJkrHFS3NSBg-P7Sg" && secret.channel === "CmN5+YJkrHFS3NSBg-P7Sg"
&& secret.key === "DNZ2wcG683GscU4fyOyqA87G" && secret.key === "DNZ2wcG683GscU4fyOyqA87G"
&& secret.present; && secret.present);
}, "version 1 hash failed to parse"); }, "version 1 hash failed to parse");
// test support for present mode in hashes
assert(function (cb) {
var secret = Cryptpad.parseHash('/1/edit//CmN5+YJkrHFS3NSBg-P7Sg/DNZ2wcG683GscU4fyOyqA87G//present');
return cb(secret.version === 1
&& secret.mode === "edit"
&& secret.channel === "CmN5+YJkrHFS3NSBg-P7Sg"
&& secret.key === "DNZ2wcG683GscU4fyOyqA87G"
&& secret.present);
}, "Couldn't handle multiple successive slashes");
// test support for trailing slash // test support for trailing slash
assert(function () { assert(function (cb) {
var secret = Cryptpad.parseHash('/1/edit/3Ujt4F2Sjnjbis6CoYWpoQ/usn4+9CqVja8Q7RZOGTfRgqI/'); var secret = Cryptpad.parseHash('/1/edit/3Ujt4F2Sjnjbis6CoYWpoQ/usn4+9CqVja8Q7RZOGTfRgqI/');
return secret.version === 1 && return cb(secret.version === 1 &&
secret.mode === "edit" && secret.mode === "edit" &&
secret.channel === "3Ujt4F2Sjnjbis6CoYWpoQ" && secret.channel === "3Ujt4F2Sjnjbis6CoYWpoQ" &&
secret.key === "usn4+9CqVja8Q7RZOGTfRgqI" && secret.key === "usn4+9CqVja8Q7RZOGTfRgqI" &&
!secret.present; !secret.present);
}, "test support for trailing slashes in version 1 hash failed to parse"); }, "test support for trailing slashes in version 1 hash failed to parse");
assert(function () { assert(function (cb) {
// TODO // TODO
return true; return cb(true);
}, "version 2 hash failed to parse correctly"); }, "version 2 hash failed to parse correctly");
var swap = function (str, dict) { var swap = function (str, dict) {
@ -194,7 +218,7 @@ define([
return str || ''; return str || '';
}; };
var formatFailures = function () { var formatFailures = function () {
var template = multiline(function () { /* var template = multiline(function () { /*
<p class="error"> <p class="error">
Failed on test number {{test}} with error message: Failed on test number {{test}} with error message:
@ -215,16 +239,15 @@ The test returned:
}).join("\n"); }).join("\n");
}; };
runASSERTS(); runASSERTS(function () {
$("body").html(function (i, val) {
$("body").html(function (i, val) { var dict = {
var dict = { previous: val,
previous: val, totalAssertions: ASSERTS.length,
totalAssertions: ASSERTS.length, passedAssertions: assertions,
passedAssertions: assertions, plural: (assertions === 1? '' : 's'),
plural: (assertions === 1? '' : 's'), failMessages: formatFailures()
failMessages: formatFailures() };
};
var SUCCESS = swap(multiline(function(){/* var SUCCESS = swap(multiline(function(){/*
<div class="report">{{passedAssertions}} / {{totalAssertions}} test{{plural}} passed. <div class="report">{{passedAssertions}} / {{totalAssertions}} test{{plural}} passed.
@ -237,12 +260,13 @@ The test returned:
{{previous}} {{previous}}
*/}), dict); */}), dict);
var report = SUCCESS; var report = SUCCESS;
return report; return report;
}); });
var $report = $('.report'); var $report = $('.report');
$report.addClass(failed?'failure':'success'); $report.addClass(failed?'failure':'success');
});
}); });

Loading…
Cancel
Save