Skip to content

Commit

Permalink
Merge pull request #1 from hdgarrood/assert-throws
Browse files Browse the repository at this point in the history
Add assertThrows
  • Loading branch information
garyb committed Jul 29, 2015
2 parents b5cb9bb + c0f5d4a commit 3ede58f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
28 changes: 28 additions & 0 deletions docs/Test/Assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,32 @@ assert' :: forall e. String -> Boolean -> Eff (assert :: ASSERT | e) Unit
Throws a runtime exception with the specified message when the boolean
value is false.

#### `assertThrows`

``` purescript
assertThrows :: forall e a. (Unit -> a) -> Eff (assert :: ASSERT | e) Unit
```

Throws a runtime exception with message "Assertion failed: An error should
have been thrown", unless the argument throws an exception when evaluated.

This function is specifically for testing unsafe pure code; for example,
to make sure that an exception is thrown if a precondition is not
satisfied. Functions which use `Eff (err :: EXCEPTION | eff) a` can be
tested with `catchException` instead.

#### `assertThrows'`

``` purescript
assertThrows' :: forall e a. String -> (Unit -> a) -> Eff (assert :: ASSERT | e) Unit
```

Throws a runtime exception with the specified message, unless the argument
throws an exception when evaluated.

This function is specifically for testing unsafe pure code; for example,
to make sure that an exception is thrown if a precondition is not
satisfied. Functions which use `Eff (err :: EXCEPTION | eff) a` can be
tested with `catchException` instead.


14 changes: 14 additions & 0 deletions src/Test/Assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ exports["assert'"] = function (message) {
};
};
};

exports.checkThrows = function (fn) {
return function () {
try {
fn();
return false;
} catch (e) {
if (e instanceof Error) return true;
var err = new Error("Threw something other than an Error");
err.something = e;
throw err;
}
};
};
32 changes: 31 additions & 1 deletion src/Test/Assert.purs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
module Test.Assert where
module Test.Assert
( assert'
, assert
, assertThrows
, assertThrows'
, ASSERT()
) where

import Control.Monad.Eff (Eff())
import Prelude
Expand All @@ -14,3 +20,27 @@ assert = assert' "Assertion failed"
-- | Throws a runtime exception with the specified message when the boolean
-- | value is false.
foreign import assert' :: forall e. String -> Boolean -> Eff (assert :: ASSERT | e) Unit

-- | Throws a runtime exception with message "Assertion failed: An error should
-- | have been thrown", unless the argument throws an exception when evaluated.
-- |
-- | This function is specifically for testing unsafe pure code; for example,
-- | to make sure that an exception is thrown if a precondition is not
-- | satisfied. Functions which use `Eff (err :: EXCEPTION | eff) a` can be
-- | tested with `catchException` instead.
assertThrows :: forall e a. (Unit -> a) -> Eff (assert :: ASSERT | e) Unit
assertThrows = assertThrows' "Assertion failed: An error should have been thrown"

-- | Throws a runtime exception with the specified message, unless the argument
-- | throws an exception when evaluated.
-- |
-- | This function is specifically for testing unsafe pure code; for example,
-- | to make sure that an exception is thrown if a precondition is not
-- | satisfied. Functions which use `Eff (err :: EXCEPTION | eff) a` can be
-- | tested with `catchException` instead.
assertThrows' :: forall e a. String -> (Unit -> a) -> Eff (assert :: ASSERT | e) Unit
assertThrows' msg fn =
checkThrows fn >>= assert' msg


foreign import checkThrows :: forall e a. (Unit -> a) -> Eff (assert :: ASSERT | e) Boolean

0 comments on commit 3ede58f

Please sign in to comment.