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

Copyright© 2016–2018 Mark Karpov
LicenseBSD 3 clause
MaintainerMark Karpov <markkarpov92@gmail.com>
Stabilityexperimental
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

Test.Hspec.Megaparsec

Contents

Description

Utility functions for testing Megaparsec parsers with Hspec.

Synopsis

Basic expectations

shouldParse Source #

Arguments

:: (HasCallStack, Ord t, ShowToken t, ShowErrorComponent e, Eq a, Show a) 
=> Either (ParseError t 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, Ord t, ShowToken t, ShowErrorComponent e, Show a) 
=> Either (ParseError t 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, Ord t, ShowToken t, ShowErrorComponent e, Show a) 
=> (s -> Either (ParseError t 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 (ParseError t 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 :: (HasCallStack, Ord t, ShowToken t, ShowErrorComponent e, Show a) => Either (ParseError t e) a -> ParseError t e -> Expectation Source #

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')

Incremental parsing

failsLeaving Source #

Arguments

:: (HasCallStack, Show a, Eq s, Show s, Stream s) 
=> (State s, Either (ParseError (Token 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, ShowToken (Token s), ShowErrorComponent e, Show a, Eq s, Show s, Stream s) 
=> (State s, Either (ParseError (Token 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 Source #

Given input for parsing, construct initial state for parser (that is, with empty file name, default tab width and position at 1 line and 1 column).

Re-exports