Copyright | © 2016 Mark Karpov |
---|---|
License | BSD 3 clause |
Maintainer | Mark Karpov <markkarpov@openmailbox.org> |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Utility functions for testing Megaparsec parsers with Hspec.
- shouldParse :: (Ord t, ShowToken t, ShowErrorComponent e, Eq a, Show a) => Either (ParseError t e) a -> a -> Expectation
- parseSatisfies :: (Ord t, ShowToken t, ShowErrorComponent e, Show a) => Either (ParseError t e) a -> (a -> Bool) -> Expectation
- shouldSucceedOn :: (Ord t, ShowToken t, ShowErrorComponent e, Show a) => (s -> Either (ParseError t e) a) -> s -> Expectation
- shouldFailOn :: Show a => (s -> Either (ParseError t e) a) -> s -> Expectation
- shouldFailWith :: (Ord t, ShowToken t, ShowErrorComponent e, Show a) => Either (ParseError t e) a -> ParseError t e -> Expectation
- err :: NonEmpty SourcePos -> EC t e -> ParseError t e
- posI :: NonEmpty SourcePos
- posN :: forall s n. (Stream s, Integral n) => n -> s -> NonEmpty SourcePos
- data EC t e
- utok :: (Ord t, Ord e) => t -> EC t e
- utoks :: (Ord t, Ord e) => [t] -> EC t e
- ulabel :: (Ord t, Ord e) => String -> EC t e
- ueof :: (Ord t, Ord e) => EC t e
- etok :: (Ord t, Ord e) => t -> EC t e
- etoks :: (Ord t, Ord e) => [t] -> EC t e
- elabel :: (Ord t, Ord e) => String -> EC t e
- eeof :: (Ord t, Ord e) => EC t e
- cstm :: e -> EC t e
- failsLeaving :: (Show a, Eq s, Show s, Stream s) => (State s, Either (ParseError (Token s) e) a) -> s -> Expectation
- succeedsLeaving :: (ShowToken (Token s), ShowErrorComponent e, Show a, Eq s, Show s, Stream s) => (State s, Either (ParseError (Token s) e) a) -> s -> Expectation
- initialState :: s -> State s
Basic expectations
:: (Ord t, ShowToken t, ShowErrorComponent e, Eq a, Show a) | |
=> Either (ParseError t e) a | Result of parsing as returned by function like |
-> a | Desired result |
-> Expectation |
Create an expectation by saying what the result should be.
parse letterChar "" "x" `shouldParse` 'x'
:: (Ord t, ShowToken t, ShowErrorComponent e, Show a) | |
=> Either (ParseError t e) a | Result of parsing as returned by function like |
-> (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)
:: (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 some given input.
parse (char 'x') "" `shouldSucceedOn` "x"
:: 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 some given input.
parse (char 'x') "" `shouldFailOn` "a"
Testing of error messages
shouldFailWith :: (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')
Error message construction
When you wish to test error message on failure, the need to construct a error message for comparison arises. These helpers allow to construct virtually any sort of error message easily.
:: NonEmpty SourcePos |
|
-> EC t e | Error components |
-> ParseError t e | Resulting |
posN :: forall s n. (Stream s, Integral n) => n -> s -> NonEmpty SourcePos Source #
posN n s
returns source position achieved by applying updatePos
method corresponding to type of stream s
n
times.
Since: 0.3.0
Auxiliary type for construction of ParseError
s. Note that it's a
monoid.
Since: 0.3.0
utok :: (Ord t, Ord e) => t -> EC t e Source #
Construct “unexpected token” error component.
Since: 0.3.0
utoks :: (Ord t, Ord e) => [t] -> EC t e Source #
Construct “unexpected tokens” error component. Empty string produces
EndOfInput
.
Since: 0.3.0
ulabel :: (Ord t, Ord e) => String -> EC t e Source #
Construct “unexpected label” error component. Do not use with empty strings (for empty strings it's bottom).
Since: 0.3.0
ueof :: (Ord t, Ord e) => EC t e Source #
Construct “unexpected end of input” error component.
Since: 0.3.0
etok :: (Ord t, Ord e) => t -> EC t e Source #
Construct “expected token” error component.
Since: 0.3.0
etoks :: (Ord t, Ord e) => [t] -> EC t e Source #
Construct “expected tokens” error component. Empty string produces
EndOfInput
.
Since: 0.3.0
elabel :: (Ord t, Ord e) => String -> EC t e Source #
Construct “expected label” error component. Do not use with empty strings.
Since: 0.3.0
eeof :: (Ord t, Ord e) => EC t e Source #
Construct “expected end of input” error component.
Since: 0.3.0
Incremental parsing
:: (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 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
.
:: (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).