From 6a1c799a6ea79d7b26eb55ea48800d40f9cc0c69 Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 10 May 2017 11:54:21 +0200 Subject: [PATCH] support async tests --- www/assert/main.js | 130 +++++++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 53 deletions(-) diff --git a/www/assert/main.js b/www/assert/main.js index 392392db8..449834a61 100644 --- a/www/assert/main.js +++ b/www/assert/main.js @@ -15,26 +15,40 @@ define([ var failMessages = []; 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) { - f(index); + f(function (err) { + done(err, index); + }, index); }); }; var assert = function (test, msg) { - ASSERTS.push(function (i) { - var returned = test(); - if (returned === true) { - assertions++; - } else { - failed = true; - failedOn = assertions; - failMessages.push({ - test: i, - message: msg, - output: returned, - }); - } + ASSERTS.push(function (cb, i) { + test(function (result) { + if (result === true) { + assertions++; + cb(); + } else { + failed = true; + failedOn = assertions; + cb({ + test: i, + message: msg, + output: result, + }); + } + }); }); }; @@ -58,7 +72,7 @@ define([ }; var HJSON_equal = function (shjson) { - assert(function () { + assert(function (cb) { // parse your stringified Hyperjson var hjson; @@ -82,10 +96,10 @@ define([ var diff = TextPatcher.format(shjson, op); if (success) { - return true; + return cb(true); } else { - return '

insert: ' + diff.insert + '

' + - 'remove: ' + diff.remove + '

'; + return cb('

insert: ' + diff.insert + '

' + + 'remove: ' + diff.remove + '

'); } }, "expected hyperjson equality"); }; @@ -94,7 +108,7 @@ define([ var roundTrip = function (sel) { var target = $(sel)[0]; - assert(function () { + assert(function (cb) { var hjson = Hyperjson.fromDOM(target); var cloned = Hyperjson.toDOM(hjson); var success = cloned.outerHTML === target.outerHTML; @@ -111,7 +125,7 @@ define([ TextPatcher.log(target.outerHTML, op); } - return success; + return cb(success); }, "Round trip serialization introduced artifacts."); }; @@ -125,9 +139,9 @@ define([ var strungJSON = function (orig) { var result; - assert(function () { + assert(function (cb) { result = JSON.stringify(JSON.parse(orig)); - return result === orig; + return cb(result === orig); }, "expected result (" + result + ") to equal original (" + orig + ")"); }; @@ -138,46 +152,56 @@ define([ }); // check that old hashes parse correctly - assert(function () { + assert(function (cb) { var secret = Cryptpad.parseHash('67b8385b07352be53e40746d2be6ccd7XAYSuJYYqa9NfmInyHci7LNy'); - return secret.channel === "67b8385b07352be53e40746d2be6ccd7" && + return cb(secret.channel === "67b8385b07352be53e40746d2be6ccd7" && secret.key === "XAYSuJYYqa9NfmInyHci7LNy" && - secret.version === 0; + secret.version === 0); }, "Old hash failed to parse"); // make sure version 1 hashes parse correctly - assert(function () { + assert(function (cb) { var secret = Cryptpad.parseHash('/1/edit/3Ujt4F2Sjnjbis6CoYWpoQ/usn4+9CqVja8Q7RZOGTfRgqI'); - return secret.version === 1 && + return cb(secret.version === 1 && secret.mode === "edit" && secret.channel === "3Ujt4F2Sjnjbis6CoYWpoQ" && secret.key === "usn4+9CqVja8Q7RZOGTfRgqI" && - !secret.present; + !secret.present); }, "version 1 hash failed to parse"); // test support for present mode in hashes - assert(function () { + assert(function (cb) { var secret = Cryptpad.parseHash('/1/edit/CmN5+YJkrHFS3NSBg-P7Sg/DNZ2wcG683GscU4fyOyqA87G/present'); - return secret.version === 1 + return cb(secret.version === 1 && secret.mode === "edit" && secret.channel === "CmN5+YJkrHFS3NSBg-P7Sg" && secret.key === "DNZ2wcG683GscU4fyOyqA87G" - && secret.present; + && secret.present); }, "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 - assert(function () { + assert(function (cb) { var secret = Cryptpad.parseHash('/1/edit/3Ujt4F2Sjnjbis6CoYWpoQ/usn4+9CqVja8Q7RZOGTfRgqI/'); - return secret.version === 1 && + return cb(secret.version === 1 && secret.mode === "edit" && secret.channel === "3Ujt4F2Sjnjbis6CoYWpoQ" && secret.key === "usn4+9CqVja8Q7RZOGTfRgqI" && - !secret.present; + !secret.present); }, "test support for trailing slashes in version 1 hash failed to parse"); - assert(function () { + assert(function (cb) { // TODO - return true; + return cb(true); }, "version 2 hash failed to parse correctly"); var swap = function (str, dict) { @@ -194,7 +218,7 @@ define([ return str || ''; }; - var formatFailures = function () { + var formatFailures = function () { var template = multiline(function () { /*

Failed on test number {{test}} with error message: @@ -215,16 +239,15 @@ The test returned: }).join("\n"); }; - runASSERTS(); - - $("body").html(function (i, val) { - var dict = { - previous: val, - totalAssertions: ASSERTS.length, - passedAssertions: assertions, - plural: (assertions === 1? '' : 's'), - failMessages: formatFailures() - }; + runASSERTS(function () { + $("body").html(function (i, val) { + var dict = { + previous: val, + totalAssertions: ASSERTS.length, + passedAssertions: assertions, + plural: (assertions === 1? '' : 's'), + failMessages: formatFailures() + }; var SUCCESS = swap(multiline(function(){/*

{{passedAssertions}} / {{totalAssertions}} test{{plural}} passed. @@ -237,12 +260,13 @@ The test returned: {{previous}} */}), dict); - var report = SUCCESS; + var report = SUCCESS; - return report; - }); + return report; + }); - var $report = $('.report'); - $report.addClass(failed?'failure':'success'); + var $report = $('.report'); + $report.addClass(failed?'failure':'success'); + }); });