Add assert.ifError
authorMikeal Rogers <mikeal.rogers@gmail.com>
Fri, 21 May 2010 19:05:55 +0000 (12:05 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Fri, 21 May 2010 19:06:12 +0000 (12:06 -0700)
doc/api.markdown
lib/assert.js
test/simple/test-assert.js

index 6afc466..c2467a1 100644 (file)
@@ -2546,6 +2546,9 @@ Expects `block` to throw an error.
 
 Expects `block` not to throw an error.
 
+### assert.ifError(value)
+
+Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, `error` in callbacks.
 
 ## Path
 
index da9198a..2d1fc08 100644 (file)
@@ -284,3 +284,4 @@ assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
   _throws.apply(this, [false].concat(pSlice.call(arguments)));
 };
 
+assert.ifError = function (err) { if (err) {throw err;}};
index 6102368..10aeb5c 100644 (file)
@@ -154,3 +154,6 @@ try {
 }
 assert.equal(true,threw,'a.doesNotThrow is not catching type matching errors');
 
+assert.throws(function () {assert.ifError(new Error('test error'))});
+assert.doesNotThrow(function(){assert.ifError(null)});
+assert.doesNotThrow(function(){assert.ifError()});