2015-08-08 12:49:57 +03:00
|
|
|
-- -*- Mode: Haskell; -*-
|
|
|
|
--
|
|
|
|
-- QuickCheck tests for Megaparsec, utility functions for parser testing.
|
|
|
|
--
|
|
|
|
-- Copyright © 2015 Megaparsec contributors
|
|
|
|
--
|
|
|
|
-- Redistribution and use in source and binary forms, with or without
|
|
|
|
-- modification, are permitted provided that the following conditions are
|
|
|
|
-- met:
|
|
|
|
--
|
|
|
|
-- * Redistributions of source code must retain the above copyright notice,
|
|
|
|
-- this list of conditions and the following disclaimer.
|
|
|
|
--
|
|
|
|
-- * Redistributions in binary form must reproduce the above copyright
|
|
|
|
-- notice, this list of conditions and the following disclaimer in the
|
|
|
|
-- documentation and/or other materials provided with the distribution.
|
|
|
|
--
|
|
|
|
-- This software is provided by the copyright holders "as is" and any
|
|
|
|
-- express or implied warranties, including, but not limited to, the implied
|
|
|
|
-- warranties of merchantability and fitness for a particular purpose are
|
|
|
|
-- disclaimed. In no event shall the copyright holders be liable for any
|
|
|
|
-- direct, indirect, incidental, special, exemplary, or consequential
|
|
|
|
-- damages (including, but not limited to, procurement of substitute goods
|
|
|
|
-- or services; loss of use, data, or profits; or business interruption)
|
|
|
|
-- however caused and on any theory of liability, whether in contract,
|
|
|
|
-- strict liability, or tort (including negligence or otherwise) arising in
|
|
|
|
-- any way out of the use of this software, even if advised of the
|
|
|
|
-- possibility of such damage.
|
|
|
|
|
|
|
|
module Util
|
2015-08-12 20:51:06 +03:00
|
|
|
( checkParser
|
|
|
|
, simpleParse
|
|
|
|
, checkChar
|
|
|
|
, checkString
|
|
|
|
, posErr
|
|
|
|
, uneStr
|
|
|
|
, uneCh
|
|
|
|
, exStr
|
|
|
|
, exCh
|
|
|
|
, exSpec
|
|
|
|
, showToken )
|
2015-08-08 12:49:57 +03:00
|
|
|
where
|
|
|
|
|
2015-08-08 21:42:47 +03:00
|
|
|
import Data.Maybe (maybeToList)
|
|
|
|
|
2015-08-08 12:49:57 +03:00
|
|
|
import Test.QuickCheck
|
|
|
|
|
|
|
|
import Text.Megaparsec.Combinator
|
|
|
|
import Text.Megaparsec.Error
|
|
|
|
import Text.Megaparsec.Pos
|
|
|
|
import Text.Megaparsec.Prim
|
|
|
|
import Text.Megaparsec.ShowToken
|
|
|
|
import Text.Megaparsec.String
|
|
|
|
|
2015-08-08 21:42:47 +03:00
|
|
|
-- | @checkParser p r s@ tries to run parser @p@ on input @s@ to parse
|
2015-08-08 12:49:57 +03:00
|
|
|
-- entire @s@. Result of the parsing is compared with expected result @r@,
|
|
|
|
-- it should match, otherwise the property doesn't hold and the test fails.
|
|
|
|
|
|
|
|
checkParser :: (Eq a, Show a) =>
|
2015-08-08 21:42:47 +03:00
|
|
|
Parser a -> Either ParseError a -> String -> Property
|
2015-08-11 00:22:29 +03:00
|
|
|
checkParser p r s = simpleParse p s === r
|
2015-08-08 21:42:47 +03:00
|
|
|
|
2015-08-11 00:22:29 +03:00
|
|
|
-- | @simpleParse p s@ runs parser @p@ on input @s@ and returns corresponding
|
|
|
|
-- result of type @Either ParseError a@, where @a@ is type of parsed
|
|
|
|
-- value. This parser tries to parser end of file too and name of input file
|
|
|
|
-- is always empty string.
|
|
|
|
|
|
|
|
simpleParse :: Parser a -> String -> Either ParseError a
|
|
|
|
simpleParse p = parse (p <* eof) ""
|
|
|
|
|
|
|
|
-- | @checkChar p test label s@ runs parser @p@ on input @s@ and checks if
|
2015-08-08 21:42:47 +03:00
|
|
|
-- the parser correctly parses single character that satisfies @test@. The
|
|
|
|
-- character may be labelled, in this case @label@ is used to check quality
|
|
|
|
-- of error messages.
|
|
|
|
|
2015-08-11 00:22:29 +03:00
|
|
|
checkChar :: Parser Char -> (Char -> Bool) ->
|
2015-08-08 21:42:47 +03:00
|
|
|
Maybe String -> String -> Property
|
2015-08-11 00:22:29 +03:00
|
|
|
checkChar p f l' s = checkParser p r s
|
2015-08-12 20:51:06 +03:00
|
|
|
where h = head s
|
|
|
|
l = exSpec <$> maybeToList l'
|
|
|
|
r | null s = posErr 0 s (uneStr "" : l)
|
|
|
|
| length s == 1 && f h = Right h
|
|
|
|
| not (f h) = posErr 0 s (uneCh h : l)
|
|
|
|
| otherwise = posErr 1 s [uneCh (s !! 1), exStr ""]
|
2015-08-08 12:49:57 +03:00
|
|
|
|
2015-08-11 00:22:29 +03:00
|
|
|
-- | @checkString p a label s@ runs parser @p@ on input @s@ and checks if
|
|
|
|
-- the result is equal to @a@ and also quality of error messages. @label@ is
|
|
|
|
-- used as expected representation of parser's result in error messages.
|
|
|
|
|
|
|
|
checkString :: Parser String -> String -> String -> String -> Property
|
|
|
|
checkString p a' l s' = checkParser p (w a' 0 s') s'
|
2015-08-12 20:51:06 +03:00
|
|
|
where w [] _ [] = Right a'
|
|
|
|
w [] i (s:_) = posErr i s' [uneCh s, exStr ""]
|
|
|
|
w _ 0 [] = posErr 0 s' [uneStr "", exSpec l]
|
|
|
|
w _ i [] = posErr 0 s' [uneStr (take i s'), exSpec l]
|
|
|
|
w (a:as) i (s:ss)
|
|
|
|
| a == s = w as i' ss
|
|
|
|
| otherwise = posErr 0 s' [uneStr (take i' s'), exSpec l]
|
|
|
|
where i' = succ i
|
2015-08-11 00:22:29 +03:00
|
|
|
|
2015-08-08 12:49:57 +03:00
|
|
|
-- | @posErr pos s ms@ is an easy way to model result of parser that
|
|
|
|
-- fails. @pos@ is how many tokens (characters) has been consumed before
|
|
|
|
-- failure. @s@ is input of the parser. @ms@ is a list, collection of
|
|
|
|
-- 'Message's. See 'uneStr', 'uneCh', 'exStr', and 'exCh' for easy ways to
|
|
|
|
-- create error messages.
|
|
|
|
|
|
|
|
posErr :: Int -> String -> [Message] -> Either ParseError a
|
|
|
|
posErr pos s = Left . foldr addErrorMessage (newErrorUnknown errPos)
|
2015-08-12 20:51:06 +03:00
|
|
|
where errPos = updatePosString (initialPos "") (take pos s)
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
|
|
-- | @uneStr s@ returns message created with 'UnExpect' constructor that
|
2015-08-11 05:54:33 +03:00
|
|
|
-- tells the system that string @s@ is unexpected. This can be used to
|
|
|
|
-- expect end of input, if @s@ is empty.
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
|
|
uneStr :: String -> Message
|
2015-08-11 05:54:33 +03:00
|
|
|
uneStr s = Unexpected $ if null s then "end of input" else showToken s
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
|
|
-- | @uneCh s@ returns message created with 'UnExpect' constructor that
|
|
|
|
-- tells the system that char @s@ is unexpected.
|
|
|
|
|
|
|
|
uneCh :: Char -> Message
|
2015-08-11 05:54:33 +03:00
|
|
|
uneCh s = Unexpected $ showToken s
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
|
|
-- | @exStr s@ returns message created with 'Expect' constructor that tells
|
|
|
|
-- the system that string @s@ is expected. This can be used to expect end of
|
|
|
|
-- input, if @s@ is empty.
|
|
|
|
|
|
|
|
exStr :: String -> Message
|
2015-08-11 05:54:33 +03:00
|
|
|
exStr s = Expected $ if null s then "end of input" else showToken s
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
|
|
-- | @exCh s@ returns message created with 'Expect' constructor that tells
|
|
|
|
-- the system that character @s@ is expected.
|
|
|
|
|
|
|
|
exCh :: Char -> Message
|
2015-08-11 05:54:33 +03:00
|
|
|
exCh s = Expected $ showToken s
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
|
|
-- | @exSpec s@ returns message created with 'Expect' constructor that tells
|
2015-08-11 00:22:29 +03:00
|
|
|
-- the system that string @s@ is expected. This is different from 'exStr' in
|
|
|
|
-- that it doesn't use 'showToken' but rather pass its argument unaltered
|
|
|
|
-- allowing for “special” labels.
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
|
|
exSpec :: String -> Message
|
2015-08-11 05:54:33 +03:00
|
|
|
exSpec = Expected
|