hspec-attoparsec-0.1.0.2: Utility functions for testing your attoparsec parsers with hspec

Copyright | (c) 2014 Alp Mestanogullari
License | BSD3
Maintainer | alpmestan@gmail.com
Stability | experimental
Safe Haskell | None
Language | Haskell2010

Test.Hspec.Attoparsec

Contents

- Equality-based combinator
- Predicate-based combinator
- Inspecting the result
- Inspecting unconsumed input
- The `Source` class, connecting parsers and inputs
- The `Leftover` class, letting us inspect unconsumed input

Description

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

Synopsis

- shouldParse :: (Eq a, Show a) => Either String a -> a -> Expectation
- parseSatisfies :: Show a => Either String a -> (a -> Bool) -> Expectation
- shouldSucceedOn :: forall p s s' (r :: Type -> Type) a. (Source p s s' r, Show a) => p s' a -> s -> Expectation
- shouldFailOn :: forall p s s' (r :: Type -> Type) a. (Source p s s' r, Show a) => p s' a -> s -> Expectation
- leavesUnconsumed :: forall (p :: Type -> Type -> Type) s s' r a. (Source p s s' r, Leftover r s) => r a -> s -> Expectation
- class (Eq string, Show string, IsString string) => Source (parser :: Type -> Type -> Type) string string' (result :: Type -> Type) | string -> parser, string -> result, string -> string' where
  - (~>) :: string -> parser string' a -> Either String a
  - (~?>) :: string -> parser string' a -> result a
- class Leftover (r :: Type -> Type) s | r -> s where
  - leftover :: r a -> Maybe s

# Equality-based combinator

shouldParse :: (Eq a, Show a) => Either String a -> a -> Expectation Source #

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

parseSatisfies :: Show a => Either String a -> (a -> Bool) -> Expectation Source #

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

shouldSucceedOn :: forall p s s' (r :: Type -> Type) a. (Source p s s' r, Show a) => p s' a -> s -> Expectation Source #

Check that a parser succeeds on some given input

    char 'x' `shouldSucceedOn` "x"

shouldFailOn :: forall p s s' (r :: Type -> Type) a. (Source p s s' r, Show a) => p s' a -> s -> Expectation Source #

Check that a parser fails on some given input

    char 'x' `shouldFailOn` "a"

# Inspecting unconsumed input

leavesUnconsumed :: forall (p :: Type -> Type -> Type) s s' r a. (Source p s s' r, Leftover r s) => r a -> s -> Expectation Source #

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

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

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 Source #

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 Source #

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.

#### Instances

Instances details

Source Parser ByteString ByteString Result Source # |
Instance details Defined in Test.Hspec.Attoparsec.Source Methods (~>) :: ByteString -> Parser ByteString a -> Either String a Source # (~?>) :: ByteString -> Parser ByteString a -> Result a Source #
Source Parser ByteString ByteString Result Source # |
Instance details Defined in Test.Hspec.Attoparsec.Source Methods (~>) :: ByteString -> Parser ByteString a -> Either String a Source # (~?>) :: ByteString -> Parser ByteString a -> Result a Source #
Source Parser Text Text Result Source # |
Instance details Defined in Test.Hspec.Attoparsec.Source Methods (~>) :: Text -> Parser Text a -> Either String a Source # (~?>) :: Text -> Parser Text a -> Result a Source #
Source Parser Text Text Result Source # |
Instance details Defined in Test.Hspec.Attoparsec.Source Methods (~>) :: Text -> Parser Text a -> Either String a Source # (~?>) :: Text -> Parser Text a -> Result a Source #

# The `Leftover` class, letting us inspect unconsumed input

class Leftover (r :: Type -> Type) s | r -> s where Source #

Class for generically inspecting unconsumed input

Methods

leftover :: r a -> Maybe s Source #

Get the unconsumed input from the result of a parser

Returns `Nothing` if the unconsumed input is ""

#### Instances

Instances details

Leftover Result ByteString Source # |
Instance details Defined in Test.Hspec.Attoparsec.Source Methods leftover :: Result a -> Maybe ByteString Source #
Leftover Result ByteString Source # |
Instance details Defined in Test.Hspec.Attoparsec.Source Methods leftover :: Result a -> Maybe ByteString Source #
Leftover Result Text Source # |
Instance details Defined in Test.Hspec.Attoparsec.Source Methods leftover :: Result a -> Maybe Text Source #
Leftover Result Text Source # |
Instance details Defined in Test.Hspec.Attoparsec.Source Methods leftover :: Result a -> Maybe Text Source #