• Stars
    star
    375
  • Rank 114,096 (Top 3 %)
  • Language
    Haskell
  • License
    MIT License
  • Created about 14 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An implementation of Python's doctest for Haskell

Doctest: Test interactive Haskell examples

doctest is a tool that checks examples and properties in Haddock comments. It is similar in spirit to the popular Python module with the same name.

Getting started

Installation

doctest is available from Hackage. Install it with:

cabal update && cabal install doctest

Make sure that Cabal's bindir is on your PATH.

On Linux:

export PATH="$HOME/.cabal/bin:$PATH"

On Mac OS X:

export PATH="$HOME/Library/Haskell/bin:$PATH"

On Windows:

set PATH="%AppData%\cabal\bin\;%PATH%"

A basic example

Below is a small Haskell module. The module contains a Haddock comment with some examples of interaction. The examples demonstrate how the module is supposed to be used.

-- src/Fib.hs
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 comment line starting with >>> denotes an expression. All comment lines following an expression denote the result of that expression. Result is defined by what a REPL (e.g. ghci) prints to stdout and stderr when evaluating that expression.)

With doctest you can check whether the implementation satisfies the given examples:

doctest src/Fib.hs

Running doctest for a Cabal package

The easiest way to run doctest for a Cabal package is via cabal repl --with-ghc=doctest.

This doesn't make a big difference for a simple package, but in more involved situations cabal will make sure that all dependencies are available and it will pass any required GHC options to doctest.

A simple .cabal file for Fib looks like this:

-- fib.cabal
cabal-version: 1.12

name:           fib
version:        0.0.0
build-type:     Simple

library
  build-depends: base == 4.*
  hs-source-dirs: src
  exposed-modules: Fib
  default-language: Haskell2010

With a .cabal file in place, it is possible to run doctest via cabal repl:

$ cabal repl --with-ghc=doctest
...
Examples: 2  Tried: 2  Errors: 0  Failures: 0

Notes:

  • If you use properties you need to pass --build-depends=QuickCheck and --build-depends=template-haskell to cabal repl.

  • doctest always uses the version of GHC it was compiled with. Reinstalling doctest with cabal install doctest --overwrite-policy=always before each invocation ensures that it uses the same version of GHC as is on the PATH.

  • Technically, cabal build is not necessary. cabal repl --with-ghc=doctest will build any dependencies as needed. However, it's more robust to run cabal build first (specifically it is not a good idea to build ghc-paths with --with-ghc=doctest).

So a more robust way to call doctest is as follows:

cabal install doctest --overwrite-policy=always && cabal build && cabal repl --build-depends=QuickCheck --build-depends=template-haskell --with-ghc=doctest

(This is what you want to use on CI.)

Passing doctest options to cabal repl

You can pass doctest options like --fast, --preserve-it and --verbose to cabal repl via --repl-options.

Example:

$ cabal repl --with-ghc=doctest --repl-options=--verbose
### Started execution at src/Fib.hs:7.
### example:
fib 10
### Successful!

### Started execution at src/Fib.hs:10.
### example:
fib 5
### Successful!

# Final summary:
Examples: 2  Tried: 2  Errors: 0  Failures: 0

Writing examples and properties

Example groups

Examples from a single Haddock comment are grouped together and share the same scope. E.g. the following works:

-- |
-- >>> let x = 23
-- >>> x + 42
-- 65

If an example fails, subsequent examples from the same group are skipped. E.g. for

-- |
-- >>> let x = 23
-- >>> let n = x + y
-- >>> print n

print n is skipped, because let n = x + y fails (as y is not in scope).

A note on performance

By default, doctest calls :reload between each group to clear GHCi's scope of any local definitions. This ensures that previous examples cannot influence later ones. However, it can lead to performance penalties if you are using doctest in a project with many modules. One possible remedy is to pass the --fast flag to doctest, which disables calling :reload between groups. If doctests are running too slowly, you might consider using --fast. (With the caveat that the order in which groups appear now matters!)

However, note that due to a bug on GHC 8.2.1 or later, the performance of --fast suffers significantly when combined with the --preserve-it flag (which keeps the value of GHCi's it value between examples).

Setup code

You can put setup code in a named chunk with the name $setup. The setup code is run before each example group. If the setup code produces any errors/failures, all tests from that module are skipped.

Here is an example:

module Foo where

import Bar.Baz

-- $setup
-- >>> let x = 23 :: Int

-- |
-- >>> foo + x
-- 65
foo :: Int
foo = 42

Note that you should not place setup code inbetween the module header (module ... where) and import declarations. GHC will not be able to parse it (issue #167). It is best to place setup code right after import declarations, but due to its declarative nature you can place it anywhere inbetween top level declarations as well.

Multi-line input

GHCi supports commands which span multiple lines, and the same syntax works for doctest:

-- |
-- >>> :{
--  let
--    x = 1
--    y = 2
--  in x + y + multiline
-- :}
-- 6
multiline = 3

Note that >>> can be left off for the lines following the first: this is so that haddock does not strip leading whitespace. The expected output has whitespace stripped relative to the :}.

Some peculiarities on the ghci side mean that whitespace at the very start is lost. This breaks the example broken, since the x and y aren't aligned from ghci's perspective. A workaround is to avoid leading space, or add a newline such that the indentation does not matter:

{- | >>> :{
let x = 1
    y = 2
  in x + y + works
:}
6
-}
works = 3

{- | >>> :{
 let x = 1
     y = 2
  in x + y + broken
:}
3
-}
broken = 3

Multi-line output

If there are no blank lines in the output, multiple lines are handled automatically.

-- | >>> putStr "Hello\nWorld!"
-- Hello
-- World!

If however the output contains blank lines, they must be noted explicitly with <BLANKLINE>. For example,

import Data.List ( intercalate )

-- | Double-space a paragraph.
--
--   Examples:
--
--   >>> let s1 = "\"Every one of whom?\""
--   >>> let s2 = "\"Every one of whom do you think?\""
--   >>> let s3 = "\"I haven't any idea.\""
--   >>> let paragraph = unlines [s1,s2,s3]
--   >>> putStrLn $ doubleSpace paragraph
--   "Every one of whom?"
--   <BLANKLINE>
--   "Every one of whom do you think?"
--   <BLANKLINE>
--   "I haven't any idea."
--
doubleSpace :: String -> String
doubleSpace = (intercalate "\n\n") . lines

Matching arbitrary output

Any lines containing only three dots (...) will match one or more lines with arbitrary content. For instance,

-- |
-- >>> putStrLn "foo\nbar\nbaz"
-- foo
-- ...
-- baz

If a line contains three dots and additional content, the three dots will match anything within that line:

-- |
-- >>> putStrLn "foo bar baz"
-- foo ... baz

QuickCheck properties

Haddock has markup support for properties. Doctest can verify properties with QuickCheck. A simple property looks like this:

-- |
-- prop> \xs -> sort xs == (sort . sort) (xs :: [Int])

The lambda abstraction is optional and can be omitted:

-- |
-- prop> sort xs == (sort . sort) (xs :: [Int])

A complete example that uses setup code is below:

module Fib where

-- $setup
-- >>> import Control.Applicative
-- >>> import Test.QuickCheck
-- >>> newtype Small = Small Int deriving Show
-- >>> instance Arbitrary Small where arbitrary = Small . (`mod` 10) <$> arbitrary

-- | Compute Fibonacci numbers
--
-- The following property holds:
--
-- prop> \(Small n) -> fib n == fib (n + 2) - fib (n + 1)
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)

If you see an error like the following, ensure that QuickCheck is visible to doctest (e.g. by passing --build-depends=QuickCheck to cabal repl).

<interactive>:39:3:
    Not in scope: ‘polyQuickCheck’
    In the splice: $(polyQuickCheck (mkName "doctest_prop"))

<interactive>:39:3:
    GHC stage restriction:
      ‘polyQuickCheck’ is used in a top-level splice or annotation,
      and must be imported, not defined locally
    In the expression: polyQuickCheck (mkName "doctest_prop")
    In the splice: $(polyQuickCheck (mkName "doctest_prop"))

Hiding examples from Haddock

You can put examples into named chunks, and not refer to them in the export list. That way they will not be part of the generated Haddock documentation, but Doctest will still find them.

-- $
-- >>> 1 + 1
-- 2

Using GHC extensions

There's two sets of GHC extensions involved when running Doctest:

  1. The set of GHC extensions that are active when compiling the module code (excluding the doctest examples). The easiest way to specify these extensions is through LANGUAGE pragmas in your source files.

  2. The set of GHC extensions that are active when executing the Doctest examples. (These are not influenced by the LANGUAGE pragmas in the file.) The recommended way to enable extensions for Doctest examples is to switch them on like this:

-- |
-- >>> :set -XTupleSections
-- >>> fst' $ (1,) 2
-- 1
fst' :: (a, b) -> a
fst' = fst

Alternatively you can pass any GHC options to Doctest, e.g.:

doctest -XCPP Foo.hs

These options will affect both the loading of the module and the execution of the Doctest examples.

If you want to omit the information which language extensions are enabled from the Doctest examples you can use the method described in Hiding examples from Haddock, e.g.:

-- $
-- >>> :set -XTupleSections

Limitations

  • Doctests only works on platforms that have support for GHC's --interactive mode (ghci).

  • Due to a GHC bug, running :set -XTemplateHaskell within ghci may unload any modules that were specified on the command-line.

    To address this doctest >= 0.19.0 does two things:

    1. Doctest always enables -XTemplateHaskell. So it is safe to use Template Haskell in examples without enabling the extension explicitly.
    2. Doctest filters out -XTemplateHaskell from single-line :set-statements. So it is still safe to include :set -XTemplateHaskell in examples for documentation purposes. It may just not work as intended in ghci due to that GHC bug.

    Doctest does not filter out -XTemplateHaskell from multi-line :set-statements. So if you e.g. use

    >>> :{
    :set -XTemplateHaskell
    :}
    

    then you are on your own.

    Note that all platforms that support --interactive also support -XTemplateHaskell. So this approach does not reduce Doctest's platform support.

  • Modules that are rejected by haddock will not work with doctest. This can mean that doctest fails on input that is accepted by GHC (e.g. #251).

  • Doctest works best with UTF-8. If your locale is e.g. LC_ALL=C, you may want to invoke doctest with LC_ALL=C.UTF-8.

Doctest in the wild

You can find real world examples of Doctest being used below:

Development

Discuss your ideas first, ideally by opening an issue on GitHub.

Add tests for new features, and make sure that the test suite passes with your changes.

cabal build --enable-tests && cabal exec -- cabal test --test-show-details=direct

Contributors

  • Adam Vogt
  • Alan Zimmerman
  • Alexander Bernauer
  • Alexandre Esteves
  • Anders Persson
  • Andreas Abel
  • Ankit Ahuja
  • Artyom Kazak
  • Edward Kmett
  • Gabor Greif
  • Hiroki Hattori
  • Ignat Insarov
  • Jens Petersen
  • Joachim Breitner
  • John Chee
  • João Cristóvão
  • Julian Arni
  • Kazu Yamamoto
  • Leon Schoorl
  • Levent Erkok
  • Luke Murphy
  • Matvey Aksenov
  • Michael Orlitzky
  • Michael Snoyman
  • Mitchell Rosen
  • Nick Smallbone
  • Nikos Baxevanis
  • Oleg Grenrus
  • quasicomputational
  • Ryan Scott
  • Sakari Jokinen
  • Simon Hengel
  • Sönke Hahn
  • Takano Akio
  • Tamar Christina
  • Veronika Romashkina

For up-to-date list, query

git shortlog -s

More Repositories

1

hpack

hpack: A modern format for Haskell packages
Haskell
611
star
2

markdown-unlit

Literate Haskell support for Markdown
Haskell
125
star
3

tinc

A dependency manager for Haskell
Haskell
97
star
4

aeson-qq

JSON quasiquoter for Haskell
Haskell
78
star
5

reserve

Universal and robust reloading for Haskell web applications
Haskell
70
star
6

pwsafe

A command-line password manager
Haskell
31
star
7

interpolate

String interpolation for Haskell done right!
Haskell
31
star
8

v8

Haskell bindings to Google's V8 JavaScript Engine
Haskell
14
star
9

logging-facade

An experimental logging API for Haskell, inspired by slf4j
Haskell
11
star
10

mpd

Mirror of git://git.musicpd.org/master/mpd.git
C
11
star
11

cucumber-haskell

Cucumber for Haskell
9
star
12

hspec-tutorial

Developing a tiny time-service with WAI and Hspec
Haskell
7
star
13

with-location

DEPRECATED, use https://github.com/sol/call-stack instead
Haskell
6
star
14

haskell-platform-versions-comparison-chart

NOTE: This project is superseded by http://www.haskell.org/platform/changelog.html
Haskell
6
star
15

rewrite-with-location

Proposal: A simple and extensible mechanism for source locations in library code
Haskell
6
star
16

haskell-autotag

A GitHub Action that creates a Git tag on .cabal version bumps
Haskell
5
star
17

http-kit

A low-level HTTP library
Haskell
5
star
18

pandoc-unlit

Literate Haskell support for GitHub's Markdown flavor
Haskell
5
star
19

aenea-box

Linux speech recognition setup based on Vagrant, Aenea, Dragonfly, NatLink and Dragon
Makefile
5
star
20

hspec-test-framework

Run test-framework tests with Hspec
Haskell
4
star
21

strafunski

Mirror of http://code.google.com/p/strafunski/
Haskell
4
star
22

HackageOneFive

Reverse dependency lookup for all packages on Hackage
Haskell
4
star
23

inject

Inject the output of shell commands into arbitrary text templates
Haskell
3
star
24

call-stack

Use GHC call-stacks in a backward compatible way
Haskell
3
star
25

attoparsec-parsec

An Attoparsec compatibility layer for Parsec
Haskell
3
star
26

solid

A functional scripting environment
Haskell
3
star
27

hspec-shouldbe

Convenience wrapper and utilities for hspec
Vim Script
3
star
28

stringbuilder

A writer monad for multi-line string literals
Haskell
3
star
29

hackage-jsonp

Latest package versions on Hackage as JSONP
Haskell
2
star
30

mpdinfo

A libmpd-haskell toy project, prints the currently played song and elapsed time
Haskell
2
star
31

string

An effort to provide all functions from Data.Text for strings
Haskell
2
star
32

darcs-mirrors

Haskell
2
star
33

json-fu

JSON serialization / deserialization
Haskell
2
star
34

GHC2021

Use GHC2021 with existing versions of GHC
2
star
35

logsink

A logging framework for Haskell
Haskell
2
star
36

test-framework-spec

A DSL for building Hspec/RSpec style test suites for test-framework
Haskell
2
star
37

stats

Haskell
2
star
38

haskell-code-edit

Haskell
2
star
39

infer-license

Infer software license from a given license file
Haskell
2
star
40

format

String interpolation for Haskell
Haskell
2
star
41

get-bootstrapped

Stable links for bootstrap releases
JavaScript
2
star
42

config-ng

Format preserving parser and renderer for INI-style config files
Haskell
2
star
43

depends

A simple configuration management tool for Haskell
Haskell
2
star
44

display

Convert Haskell values to a textual representation
Haskell
2
star
45

run-haskell-tests

2
star
46

ghc-env

Automatically download and manage multiple versions of GHC
Haskell
2
star
47

uu-parsinglib

A mirror of uu-parsinglib's svn repository
Haskell
2
star
48

hexpat

A mirror of http://code.haskell.org/hexpat/
C
1
star
49

qreadline

A drop-in replacement for QLineEdit with emacs-like key bindings
C++
1
star
50

ci

Haskell
1
star
51

cg1

A simple OpenGL application
C++
1
star
52

setenv

A cross-platform Haskell library for setting environment variables
Haskell
1
star
53

rest-client

Haskell
1
star
54

aeson-toolkit

A generalization of Aeson over Failure
Haskell
1
star
55

fancontrol

Minimalistic fan control for my x200
Haskell
1
star
56

json-stream

Haskell
1
star
57

KBDevBoard

A development board for rapid prototyping of keyboard circuitry âš¡
1
star
58

conduit-issue

Haskell
1
star
59

better-process

A *better* incarnation of Haskell's `process` library
Haskell
1
star
60

simdjson

Haskell
1
star
61

hspec-experimental

An experimental DSL for testing on top of Hspec
Haskell
1
star