Permalink
Browse files

handling exceptions on callbacks

  • Loading branch information...
1 parent f7a5083 commit 1dab646d1170ec2070ac330306452552256f66ff @refractalize committed Mar 7, 2011
Showing with 27 additions and 12 deletions.
  1. +1 −0 .gitignore
  2. +20 −6 cupoftea.js
  3. +5 −5 cupoftea_test.js
  4. +1 −1 specs.watchr
View
@@ -0,0 +1 @@
+.*.swp
View
@@ -119,9 +119,10 @@ var Callbacks = function (runStack) {
this.shouldNotCall = function () {
hasCallbacks = true;
+ var error = new Error("shouldn't be called");
return function () {
if (!currentSpecResultsCalled) {
- runStack.results('this shouldnt be called');
+ runStack.results(error);
currentSpecResultsCalled = true;
}
};
@@ -135,7 +136,8 @@ var Callbacks = function (runStack) {
var result = f.apply(null, arguments);
} catch (e) {
runStack.results(e);
- //throw e;
+ expectedExceptions.push(e);
+ throw e;
}
numberOfCallbacks--;
@@ -154,13 +156,17 @@ var Callbacks = function (runStack) {
return hasCallbacks;
};
- this.failIfNotCalled = function () {
+ this.assertCallbacks = function () {
if (numberOfCallbacks > 0) {
runStack.results('not called');
+ } else if (hasCallbacks) {
+ runStack.results();
}
};
};
+var expectedExceptions = [];
+
var RunStack = function () {
var callbacks = new Callbacks(this);
var deepestSpec;
@@ -200,8 +206,8 @@ var RunStack = function () {
return callbacks.shouldNotCall();
};
- this.failIfCallbacksNotCalled = function () {
- callbacks.failIfNotCalled();
+ this.assertAllCallbacks = function () {
+ callbacks.assertCallbacks();
};
this.results = function (exception) {
@@ -252,6 +258,14 @@ shouldCall = function (f) {
process.addListener('exit', function () {
//console.log('exiting');
_(runStacks).each(function (runStack) {
- runStack.failIfCallbacksNotCalled();
+ runStack.assertAllCallbacks();
});
});
+
+process.on('uncaughtException', function(err) {
+ if (_(expectedExceptions).contains(err)) {
+ console.log(err);
+ } else {
+ throw err;
+ }
+});
View
@@ -27,7 +27,7 @@ spec('cupoftea', function () {
assert.equal(x, 22);
});
- spec('inner b fails', function () {
+ spec('inner b (failing)', function () {
d('inner inner b');
x++;
assert.equal(x, 22);
@@ -40,14 +40,14 @@ spec('cupoftea', function () {
x = 20;
});
- spec('should not call', function () {
+ spec('should not call (failing)', function () {
d('should not call');
setTimeout(shouldNotCall(), 1000);
assert.equal(x, 10);
});
spec("should not call and isn't", function () {
- d('should not call');
+ d('should not call ---------------');
shouldNotCall();
assert.equal(x, 10);
});
@@ -60,7 +60,7 @@ spec('cupoftea', function () {
assert.equal(x, 10);
});
- spec('should call but exception thrown', function () {
+ spec('should call but exception thrown (failing)', function () {
d('should call');
setTimeout(shouldCall(function () {
d('stuff');
@@ -69,7 +69,7 @@ spec('cupoftea', function () {
assert.equal(x, 10);
});
- spec("should call but isn't", function () {
+ spec("should call but isn't (failing)", function () {
d('should call');
shouldCall(function () {
d('stuff');
View
@@ -49,4 +49,4 @@ watch '.*' do |code|
test($mappers.find_spec_for(code[0]))
end
-test('test.js')
+test('cupoftea_test.js')

0 comments on commit 1dab646

Please sign in to comment.