HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulehspec-attoparsec-0.1.0.2Haskell2010

Test.Hspec.Attoparsec

Utility functions for testing attoparsec parsers, each one providing an example of how to use it.

Contents
  1. Equality-based combinator
  2. Predicate-based combinator
  3. Inspecting the result
  4. Inspecting unconsumed input
  5. The Source class, connecting parsers and inputs
  6. The Leftover class, letting us inspect unconsumed input
Synopsis

Equality-based combinator

valueshouldParse :: (Eq a, Show a) => Either String a -> a -> Expectation

Create an expectation by saying what the result should be. Intended to be used with ~> as follows:

  "<!-- foo -->" ~> htmlCommentParser
    `shouldParse` TagComment " foo "

Predicate-based combinator

valueparseSatisfies :: Show a => Either String a -> (a -> Bool) -> Expectation

Create an expectation by saying that the parser should successfully parse a value and that this value should satisfy some predicate.

This can fail if the parsing doesn't succeed or if it succeeds but the value doesn't match the predicate.

">>>" ~> many (char '>')
  `parseSatisfies` ((==3) . length)

Inspecting the result

Inspecting unconsumed input

valueleavesUnconsumed
  1. :: (Source p s s' r, Leftover r s)
  2. => r a
  3. -> s
  4. -> Expectation

Checking that the given parser succeeds and yields the given part of the input unconsumed. Intended to be used in conjunction with ~?>

("xa" :: Text) ~?> char 'x'
  `leavesUnconsumed` "a"

The Source class, connecting parsers and inputs

classclass (Eq string, Show string, IsString string) => Source (parser :: Type -> Type -> Type) string string' (result :: Type -> Type) | string -> parser, string -> result, string -> string' where

A class where each instance will just teach how to get an Either or the specific result type associated to the parser for the given input type.

Methods

  • (~>) :: string -> parser string' a -> Either String a

    Feed some input to a parser and extract the result as either a failure String or an actually parsed value. Can be read as fed to.

    -- "<a ...>" fed to an HTML parser 
    "<a href=\"/foo\">Go to foo</a>" ~> htmlParser :: Either String a
  • (~?>) :: string -> parser string' a -> result a

    Feed some input to a parser and extract it as the appropriate result type from that module.

    This is not currently useful in the library per se, but is used in test-suites directly where we generally only deal with one concrete set of parser, input and result types. This lets us inspect the result in any way we want, e.g in conjunction with shouldSatisfy or a custom hspec combinator.

Instances4Source

The Leftover class, letting us inspect unconsumed input

classclass Leftover (r :: Type -> Type) s | r -> s where

Class for generically inspecting unconsumed input

Methods

  • leftover :: r a -> Maybe s

    Get the unconsumed input from the result of a parser

    Returns Nothing if the unconsumed input is ""

Instances4Leftover