DocTest

From HaskellWiki
Revision as of 08:08, 16 October 2010 by SimonHengel (talk | contribs) (fix wording issue)
Jump to navigation Jump to search

What is DocTest

DocTest is a small program, that checks examples in Haddock comments. It is modeled after doctest for Python.

Installation

DocTest is available from Hackage. Install it, by typing:

$ cabal install doctest

Usage

Below is a small Haskell module. The module contains source code comments. Those comments are examples from an interactive Haskell session and demonstrate how the module is used.

module Fib where

-- | Compute Fibonacci numbers
--
-- Examples:
--
-- >>> fib 10
-- 55
--
-- >>> fib 5
-- 5
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)

(A line starting with >>> denotes an expression. All comment lines following an expression denote the result of that expression. Result is defined by what an REPL (e.g. ghci) prints to stdout and stderr when evaluating that expression.)

With DocTest you may check whether the implementation satisfies the given examples, by typing:

$ doctest Fib.hs

You may produce Haddock documentation for that module with:

$ haddock -h Fib.hs -o doc/

Hacking

DocTest is still experimental. You can find a reference to the public source repository at Hackage.

Patches are gladly welcome!