torch-0.1: Simple unit test library (or framework)Source codeContentsIndex
Test.Torch
Description

Test.Torch is a simple library (or framework) for unit test.

As Library:

I assume You want to implement a function that can't find way to test using QuickCheck because for example return type is wrapped by IO or some monads, but for some arguments, you know collect return values. It is time to use Test.Torch. let its function like this:

 f :: Int -> IO Bool
 f n = some_complecated_process

And you know that f 1 is same as return True, and f 0 is same as return False.

You can write unit test monadic way (this monad is acutually Writer monad, and Tests are treated as Monoid).

 test_for_f = do
   b <- liftIO $ f 1 -- you can do IO Action in test using liftIO
   ok b "f 1 is same as return True"
   b' <- liftIO $ f 0
   notOk b' "f 2 is same as return False"

Then run it.

 test = run test_for_f

Test report is displayed to terminal. If second test failed,

 Running 2 tests.
 .f
 1 test(s) failed.
 f 2 is same as return False: failed.

This output means that f 2 returned True.

If all tests are passed,

 Running 2 tests.
 ..
 Ok, All tests passed.

Output this.

As Framework:

If you think this is not a good format, You can improve output format. define some Hook, call it with your test by makeReportWithHook. Hook also has monadic interface.

And you can also create your own test constructor, with defining Test, Failure, and some Builder.

Synopsis
ok :: Bool -> String -> Builder ()
notOk :: Bool -> String -> Builder ()
is :: (Eq a, Show a) => a -> a -> String -> Builder ()
isn't :: (Eq a, Show a) => a -> a -> String -> Builder ()
isBottom :: NFData a => a -> String -> Builder ()
isn'tBottom :: NFData a => a -> String -> Builder ()
run :: Builder a -> IO ()
Documentation
ok :: Bool -> String -> Builder ()Source
notOk :: Bool -> String -> Builder ()Source

ok and notOk are test for assertion, take a Bool value, and then check whether the value is equal to True or False.

 ok    True  "'ok' succeeds if given value is True"
 notOk False "'notOk' succeeds if given value is False"

Second String argument is the test's name, this is used for telling you what test failed if test was failed usually (and every predefined tests in this module, requires test's name).

is :: (Eq a, Show a) => a -> a -> String -> Builder ()Source
isn't :: (Eq a, Show a) => a -> a -> String -> Builder ()Source

is and isn't are test for equality. First argument is treated as gotten value, and second is expected value.

 is    1 1 "test that checks 1 == 1"
 isn't 1 2 "test that checks 1 /= 2"
 is (fact 10) 3628800 "check if factorial function works..."
isBottom :: NFData a => a -> String -> Builder ()Source
isn'tBottom :: NFData a => a -> String -> Builder ()Source

isBottom and isn'tBottom evaluates given value, and check if it is Bottom (undefined, error, or some exeptions).

 isBottom undefined "for example, this test succeeds"
run :: Builder a -> IO ()Source

run takes a builder (do block contains some tests such as is or notOk), and build test from builder, run it, and report to stdout.

You can define your own run, see makeReportWithHook.

Produced by Haddock version 2.4.2