support async tests

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

@ -15,27 +15,41 @@ 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++;
cb();
} else { } else {
failed = true; failed = true;
failedOn = assertions; failedOn = assertions;
failMessages.push({ cb({
test: i, test: i,
message: msg, message: msg,
output: returned, output: result,
}); });
} }
}); });
});
}; };
var HJSON_list = [ var HJSON_list = [
@ -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) {
@ -215,8 +239,7 @@ 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,
@ -244,5 +267,6 @@ The test returned:
var $report = $('.report'); var $report = $('.report');
$report.addClass(failed?'failure':'success'); $report.addClass(failed?'failure':'success');
});
}); });

Loading…
Cancel
Save