[Pkg-javascript-commits] [node-jade] 44/72: Support async tests with callback
Jelmer Vernooij
jelmer at moszumanska.debian.org
Sun Jul 3 18:03:28 UTC 2016
This is an automated email from the git hooks/post-receive script.
jelmer pushed a commit to annotated tag upstream/1.0.0
in repository node-jade.
commit a84f932113069c17f82a4163bad62108a913ccea
Author: Timothy Gu <timothygu99 at gmail.com>
Date: Wed Jul 8 17:08:23 2015 +0800
Support async tests with callback
---
test/test.js | 50 ++++++++++++++++++++++++++++++++------------------
1 file changed, 32 insertions(+), 18 deletions(-)
diff --git a/test/test.js b/test/test.js
index b97e1ff..5662a79 100644
--- a/test/test.js
+++ b/test/test.js
@@ -5,39 +5,53 @@ var tr = require('../');
module.exports = test;
function test(name, fn) {
- testit(name, function () {
+ function testBody (done) {
var originals = {}, result;
try {
- result = fn(function (name, overrider) {
- originals[name] = tr[name];
- tr[name] = overrider;
- });
+ if (done) {
+ result = fn(function (name, overrider) {
+ originals[name] = tr[name];
+ tr[name] = overrider;
+ }, function (err) {
+ restore();
+ return done(err);
+ });
+ } else {
+ result = fn(function (name, overrider) {
+ originals[name] = tr[name];
+ tr[name] = overrider;
+ });
+ }
} catch (ex) {
- Object.keys(originals).forEach(function (key) {
- tr[key] = originals[key];
- });
+ restore();
throw ex;
}
- Object.keys(originals).forEach(function (key) {
- tr[key] = originals[key];
- });
+ restore();
if (Object.keys(originals).length === 0) {
return result;
} else if (result) {
return result.then(function () {
- Object.keys(originals).forEach(function (key) {
- tr[key] = originals[key];
- });
+ restore();
}, function (err) {
- Object.keys(originals).forEach(function (key) {
- tr[key] = originals[key];
- });
+ restore();
throw err;
});
} else {
+ restore();
+ }
+ function restore () {
Object.keys(originals).forEach(function (key) {
tr[key] = originals[key];
});
}
- });
+ }
+ if (fn.length < 2) {
+ testit(name, function () {
+ return testBody();
+ });
+ } else {
+ testit(name, function (done) {
+ return testBody(done);
+ });
+ }
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-jade.git
More information about the Pkg-javascript-commits
mailing list