hspec-megaparsec-2.2.1: Utility functions for testing Megaparsec parsers with Hspec

Copyright | © 2016–present Mark Karpov
License | BSD 3 clause
Maintainer | Mark Karpov <markkarpov92@gmail.com>
Stability | experimental
Portability | portable
Safe Haskell | Safe-Inferred
Language | GHC2021

Test.Hspec.Megaparsec

Contents

- Basic expectations
- Testing of error messages
- Incremental parsing
- Re-exports

Description

Utility functions for testing Megaparsec parsers with Hspec.

Synopsis

- shouldParse :: (HasCallStack, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s, Show a, Eq a) => Either (ParseErrorBundle s e) a -> a -> Expectation
- parseSatisfies :: (HasCallStack, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s, Show a, Eq a) => Either (ParseErrorBundle s e) a -> (a -> Bool) -> Expectation
- shouldSucceedOn :: (HasCallStack, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s, Show a) => (s -> Either (ParseErrorBundle s e) a) -> s -> Expectation
- shouldFailOn :: (HasCallStack, Show a) => (s -> Either (ParseErrorBundle s e) a) -> s -> Expectation
- shouldFailWith :: (HasCallStack, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s, Show a, Eq e) => Either (ParseErrorBundle s e) a -> ParseError s e -> Expectation
- shouldFailWithM :: (HasCallStack, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s, Show a, Eq e) => Either (ParseErrorBundle s e) a -> [ParseError s e] -> Expectation
- failsLeaving :: (HasCallStack, Show a, Eq s, Show s) => (State s e, Either (ParseErrorBundle s e) a) -> s -> Expectation
- succeedsLeaving :: (HasCallStack, Show a, Eq s, Show s, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s) => (State s e, Either (ParseErrorBundle s e) a) -> s -> Expectation
- initialState :: s -> State s e
- initialPosState :: s -> PosState s
- module Text.Megaparsec.Error.Builder

# Basic expectations

shouldParse Source #

Arguments

:: (HasCallStack, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s, Show a, Eq a) |
=> Either (ParseErrorBundle s e) a | Result of parsing as returned by function like `parse`
-> a | Desired result
-> Expectation |

Create an expectation by saying what the result should be.

    parse letterChar "" "x" `shouldParse` 'x'

parseSatisfies Source #

Arguments

:: (HasCallStack, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s, Show a, Eq a) |
=> Either (ParseErrorBundle s e) a | Result of parsing as returned by function like `parse`
-> (a -> Bool) | Predicate
-> Expectation |

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

    parse (many punctuationChar) "" "?!!" `parseSatisfies` ((== 3) . length)

shouldSucceedOn Source #

Arguments

:: (HasCallStack, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s, Show a) |
=> (s -> Either (ParseErrorBundle s e) a) | Parser that takes stream and produces result or error message
-> s | Input that the parser should succeed on
-> Expectation |

Check that a parser succeeds on a given input.

    parse (char 'x') "" `shouldSucceedOn` "x"

shouldFailOn Source #

Arguments

:: (HasCallStack, Show a) |
=> (s -> Either (ParseErrorBundle s e) a) | Parser that takes stream and produces result or error message
-> s | Input that the parser should fail on
-> Expectation |

Check that a parser fails on a given input.

    parse (char 'x') "" `shouldFailOn` "a"

# Testing of error messages

shouldFailWith Source #

Arguments

:: (HasCallStack, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s, Show a, Eq e) |
=> Either (ParseErrorBundle s e) a | The result of parsing
-> ParseError s e | Expected parse errors
-> Expectation |

Create an expectation that parser should fail producing certain `ParseError`. Use the `err` function from this module to construct a `ParseError` to compare with.

    parse (char 'x') "" "b" `shouldFailWith` err posI (utok 'b' <> etok 'x')

shouldFailWithM Source #

Arguments

:: (HasCallStack, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s, Show a, Eq e) |
=> Either (ParseErrorBundle s e) a | The result of parsing
-> [ParseError s e] | Expected parse errors, the argument is a normal linked list (as opposed to the more correct `NonEmpty` list) as a syntactical convenience for the user, passing empty list here will result in an error
-> Expectation |

Similar to `shouldFailWith`, but allows us to check parsers that can report more than one parse error at a time.

Since: 2.0.0

# Incremental parsing

failsLeaving Source #

Arguments

:: (HasCallStack, Show a, Eq s, Show s) |
=> (State s e, Either (ParseErrorBundle s e) a) | Parser that takes stream and produces result along with actual state information
-> s | Part of input that should be left unconsumed
-> Expectation |

Check that a parser fails and leaves a certain part of input unconsumed. Use it with functions like `runParser'` and `runParserT'` that support incremental parsing.

    runParser' (many (char 'x') <* eof) (initialState "xxa")
      `failsLeaving` "a"

See also: `initialState`.

succeedsLeaving Source #

Arguments

:: (HasCallStack, Show a, Eq s, Show s, ShowErrorComponent e, Stream s, VisualStream s, TraversableStream s) |
=> (State s e, Either (ParseErrorBundle s e) a) | Parser that takes stream and produces result along with actual state information
-> s | Part of input that should be left unconsumed
-> Expectation |

Check that a parser succeeds and leaves certain part of input unconsumed. Use it with functions like `runParser'` and `runParserT'` that support incremental parsing.

    runParser' (many (char 'x')) (initialState "xxa")
      `succeedsLeaving` "a"

See also: `initialState`.

initialState :: s -> State s e Source #

Given input for parsing, construct initial state for parser.

initialPosState :: s -> PosState s Source #

Given input for parsing, construct initial positional state.

Since: 2.0.0

# Re-exports

module Text.Megaparsec.Error.Builder