Coder Social home page Coder Social logo

phadej / doctest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sol/doctest

0.0 2.0 0.0 633 KB

An implementation of Python's doctest for Haskell

Home Page: http://hackage.haskell.org/package/doctest

License: MIT License

Haskell 99.55% Shell 0.43% C 0.03%

doctest's Introduction

Doctest: Test interactive Haskell examples

doctest is a small program, that checks [examples in Haddock comments] (http://www.haskell.org/haddock/doc/html/ch03s08.html#id566093). It is similar to the [popular Python module with the same name] (http://docs.python.org/library/doctest.html).

Installation

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

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 it's C:\Documents And Settings\user\Application Data\cabal\bin.

For more information, see the [section on paths in the Cabal User Guide] (http://www.haskell.org/cabal/users-guide/installing-packages.html#paths-in-the-simple-build-system).

Usage

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.

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 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/

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 not tried, because let n = x + y fails (y is not in scope!).

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
-- $setup
-- >>> let x = 23 :: Int

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

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 of for the lines following the first: this 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 the x and y are 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 (since version 2.13.0) 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 a dependency of the test-suite or executable running doctest.

<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] 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] language-pragma in your source files. (Doctest will not look at your cabal file.)
  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] (#hiding-examples-from-haddock), e.g.:

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

Cabal integration

Doctest provides both, an executable and a library. The library exposes a function doctest of type:

doctest :: [String] -> IO ()

Doctest's own main is simply:

main = getArgs >>= doctest

Consequently, it is possible to create a custom executable for a project, by passing all command-line arguments that are required for that project to doctest. A simple example looks like this:

-- file doctests.hs
import Test.DocTest
main = doctest ["-isrc", "src/Main.hs"]

And a corresponding Cabal test suite section like this:

test-suite doctests
  type:          exitcode-stdio-1.0
  ghc-options:   -threaded
  main-is:       doctests.hs
  build-depends: base, doctest >= 0.8

Doctest in the wild

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

Doctest extensions

Development Build Status

Join in at #hspec on freenode.

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 configure --enable-tests && cabal build && cabal test

Contributors

  • Adam Vogt
  • Anders Persson
  • Ankit Ahuja
  • Edward Kmett
  • Hiroki Hattori
  • Joachim Breitner
  • João Cristóvão
  • Julian Arni
  • Kazu Yamamoto
  • Levent Erkok
  • Luke Murphy
  • Matvey Aksenov
  • Michael Orlitzky
  • Michael Snoyman
  • Nick Smallbone
  • Sakari Jokinen
  • Simon Hengel
  • Sönke Hahn

doctest's People

Contributors

aavogt avatar alanz avatar ankit avatar cheecheeo avatar decentral1se avatar ekmett avatar emwap avatar jcristovao avatar jkarni avatar kazu-yamamoto avatar leventerkok avatar nick8325 avatar nomeata avatar orlitzky avatar sakari avatar seagull-kamome avatar snoyberg avatar soenkehahn avatar sol avatar supki avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.