Wiki

Clone wiki

hunit-parsec / Home

hunit-parsec

This package is soon to be on Hackage...

Test.HUnit.Parsec

This module defines a type for concise expression of Parsec test cases:

data ParsecTest tok st t = ParsecTest {
  parser :: GenParser tok st t, 
  initState :: st,
  positiveCases :: [(t, [[tok]])], -- Multiple inputs with the same expected value.
  negativeCases :: [[tok]]
  }

instance (Show tok, Eq t, Show t) => Testable (ParsecTest tok st t) where
  test (ParsecTest ...) = ...

Example

Here's an example unit test module for a parser that parses one or more x characters prefixed by discarded whitespace:

module Main where

import Test.HUnit
import Test.HUnit.Parsec
import Text.ParserCombinators.Parsec


main :: IO ()
main = runTestTT (test parserCase) >> return ()
  where parserCase =
          ParsecTest {
            parser = many (char ' ') >> (many1 (char 'x')),
            initState = (),
            positiveCases =
              [("x", ["x", " x", "       x"]),
               ("xxx", ["xxx", " xxx"])],
            negativeCases =
              ["", "y", "x ", "xy", "   xx"]
          }

Updated