2015-08-03 10:19:23 +03:00
|
|
|
|
--
|
|
|
|
|
-- QuickCheck tests for Megaparsec's primitive parser combinators.
|
|
|
|
|
--
|
2016-01-09 15:56:33 +03:00
|
|
|
|
-- Copyright © 2015–2016 Megaparsec contributors
|
2015-08-03 10:19:23 +03:00
|
|
|
|
--
|
|
|
|
|
-- 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.
|
|
|
|
|
--
|
2015-10-30 16:41:21 +03:00
|
|
|
|
-- 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.
|
2015-08-03 10:19:23 +03:00
|
|
|
|
|
2016-02-18 20:36:26 +03:00
|
|
|
|
{-# LANGUAGE Rank2Types #-}
|
2015-08-20 14:12:44 +03:00
|
|
|
|
{-# OPTIONS -fno-warn-orphans #-}
|
|
|
|
|
|
2015-08-03 10:19:23 +03:00
|
|
|
|
module Prim (tests) where
|
|
|
|
|
|
2015-08-20 14:12:44 +03:00
|
|
|
|
import Control.Applicative
|
2015-09-21 16:59:04 +03:00
|
|
|
|
import Data.Char (isLetter, toUpper)
|
2015-08-26 13:15:30 +03:00
|
|
|
|
import Data.Foldable (asum)
|
2015-08-24 13:35:58 +03:00
|
|
|
|
import Data.List (isPrefixOf)
|
2016-02-17 20:32:48 +03:00
|
|
|
|
import Data.Maybe (maybeToList)
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
2015-10-25 19:20:30 +03:00
|
|
|
|
import Control.Monad.Cont
|
|
|
|
|
import Control.Monad.Except
|
2015-10-25 22:24:48 +03:00
|
|
|
|
import Control.Monad.Identity
|
2015-09-21 18:40:27 +03:00
|
|
|
|
import Control.Monad.Reader
|
|
|
|
|
import qualified Control.Monad.State.Lazy as L
|
|
|
|
|
import qualified Control.Monad.State.Strict as S
|
|
|
|
|
import qualified Control.Monad.Writer.Lazy as L
|
|
|
|
|
import qualified Control.Monad.Writer.Strict as S
|
2015-09-21 16:59:04 +03:00
|
|
|
|
|
2015-08-03 10:19:23 +03:00
|
|
|
|
import Test.Framework
|
2016-02-17 21:12:27 +03:00
|
|
|
|
import Test.Framework.Providers.HUnit (testCase)
|
2015-08-20 14:12:44 +03:00
|
|
|
|
import Test.Framework.Providers.QuickCheck2 (testProperty)
|
2015-09-18 12:41:18 +03:00
|
|
|
|
import Test.QuickCheck hiding (label)
|
2016-02-18 20:36:26 +03:00
|
|
|
|
import Test.HUnit (Assertion)
|
2015-08-03 10:19:23 +03:00
|
|
|
|
|
2015-08-20 14:12:44 +03:00
|
|
|
|
import Text.Megaparsec.Char
|
2016-02-17 23:07:08 +03:00
|
|
|
|
import Text.Megaparsec.Combinator
|
2016-02-07 21:37:35 +03:00
|
|
|
|
import Text.Megaparsec.Error
|
2015-08-21 22:13:20 +03:00
|
|
|
|
import Text.Megaparsec.Pos
|
2015-08-03 10:19:23 +03:00
|
|
|
|
import Text.Megaparsec.Prim
|
2015-08-20 14:12:44 +03:00
|
|
|
|
import Text.Megaparsec.String
|
|
|
|
|
|
|
|
|
|
import Pos ()
|
2015-10-26 10:52:21 +03:00
|
|
|
|
import Error ()
|
2015-08-20 14:12:44 +03:00
|
|
|
|
import Util
|
2015-08-03 10:19:23 +03:00
|
|
|
|
|
|
|
|
|
tests :: Test
|
|
|
|
|
tests = testGroup "Primitive parser combinators"
|
2016-02-08 10:30:57 +03:00
|
|
|
|
[ testProperty "ParsecT functor" prop_functor
|
|
|
|
|
, testProperty "ParsecT applicative (<*>)" prop_applicative_0
|
|
|
|
|
, testProperty "ParsecT applicative (*>)" prop_applicative_1
|
|
|
|
|
, testProperty "ParsecT applicative (<*)" prop_applicative_2
|
|
|
|
|
, testProperty "ParsecT alternative empty and (<|>)" prop_alternative_0
|
|
|
|
|
, testProperty "ParsecT alternative (<|>)" prop_alternative_1
|
|
|
|
|
, testProperty "ParsecT alternative (<|>) pos" prop_alternative_2
|
|
|
|
|
, testProperty "ParsecT alternative (<|>) hints" prop_alternative_3
|
|
|
|
|
, testProperty "ParsecT alternative many" prop_alternative_4
|
|
|
|
|
, testProperty "ParsecT alternative some" prop_alternative_5
|
|
|
|
|
, testProperty "ParsecT alternative optional" prop_alternative_6
|
|
|
|
|
, testProperty "ParsecT monad return" prop_monad_0
|
|
|
|
|
, testProperty "ParsecT monad (>>)" prop_monad_1
|
|
|
|
|
, testProperty "ParsecT monad (>>=)" prop_monad_2
|
|
|
|
|
, testProperty "ParsecT monad fail" prop_monad_3
|
|
|
|
|
, testProperty "ParsecT monad laws: left identity" prop_monad_left_id
|
|
|
|
|
, testProperty "ParsecT monad laws: right identity" prop_monad_right_id
|
|
|
|
|
, testProperty "ParsecT monad laws: associativity" prop_monad_assoc
|
|
|
|
|
, testProperty "ParsecT monad reader ask" prop_monad_reader_ask
|
|
|
|
|
, testProperty "ParsecT monad reader local" prop_monad_reader_local
|
|
|
|
|
, testProperty "ParsecT monad state get" prop_monad_state_get
|
|
|
|
|
, testProperty "ParsecT monad state put" prop_monad_state_put
|
|
|
|
|
, testProperty "ParsecT monad cont" prop_monad_cont
|
|
|
|
|
, testProperty "ParsecT monad error: throw" prop_monad_error_throw
|
|
|
|
|
, testProperty "ParsecT monad error: catch" prop_monad_error_catch
|
|
|
|
|
, testProperty "combinator unexpected" prop_unexpected
|
|
|
|
|
, testProperty "combinator failure" prop_failure
|
|
|
|
|
, testProperty "combinator label" prop_label
|
|
|
|
|
, testProperty "combinator hidden hints" prop_hidden_0
|
|
|
|
|
, testProperty "combinator hidden error" prop_hidden_1
|
|
|
|
|
, testProperty "combinator try" prop_try
|
|
|
|
|
, testProperty "combinator lookAhead" prop_lookAhead_0
|
|
|
|
|
, testProperty "combinator lookAhead hints" prop_lookAhead_1
|
|
|
|
|
, testProperty "combinator lookAhead messages" prop_lookAhead_2
|
2016-02-17 21:12:27 +03:00
|
|
|
|
, testCase "combinator lookAhead cerr" case_lookAhead_3
|
2016-02-08 10:30:57 +03:00
|
|
|
|
, testProperty "combinator notFollowedBy" prop_notFollowedBy_0
|
|
|
|
|
, testProperty "combinator notFollowedBy twice" prop_notFollowedBy_1
|
|
|
|
|
, testProperty "combinator notFollowedBy eof" prop_notFollowedBy_2
|
2016-02-17 21:12:27 +03:00
|
|
|
|
, testCase "combinator notFollowedBy cerr" case_notFollowedBy_3a
|
|
|
|
|
, testCase "combinator notFollowedBy cerr" case_notFollowedBy_3b
|
|
|
|
|
, testCase "combinator notFollowedBy eerr" case_notFollowedBy_4a
|
|
|
|
|
, testCase "combinator notFollowedBy eerr" case_notFollowedBy_4b
|
2016-02-17 23:07:08 +03:00
|
|
|
|
, testProperty "combinator withRecovery" prop_withRecovery_0
|
|
|
|
|
, testCase "combinator withRecovery eok" case_withRecovery_1
|
|
|
|
|
, testCase "combinator withRecovery meerr-rcerr" case_withRecovery_2
|
|
|
|
|
, testCase "combinator withRecovery meerr-reok" case_withRecovery_3a
|
|
|
|
|
, testCase "combinator withRecovery meerr-reok" case_withRecovery_3b
|
|
|
|
|
, testCase "combinator withRecovery mcerr-rcok" case_withRecovery_4a
|
|
|
|
|
, testCase "combinator withRecovery mcerr-rcok" case_withRecovery_4b
|
|
|
|
|
, testCase "combinator withRecovery mcerr-rcerr" case_withRecovery_5
|
|
|
|
|
, testCase "combinator withRecovery mcerr-reok" case_withRecovery_6a
|
|
|
|
|
, testCase "combinator withRecovery mcerr-reok" case_withRecovery_6b
|
|
|
|
|
, testCase "combinator withRecovery mcerr-reerr" case_withRecovery_7
|
2016-02-17 21:12:27 +03:00
|
|
|
|
, testCase "combinator eof return value" case_eof
|
2016-02-08 10:30:57 +03:00
|
|
|
|
, testProperty "combinator token" prop_token
|
|
|
|
|
, testProperty "combinator tokens" prop_tokens
|
|
|
|
|
, testProperty "parser state position" prop_state_pos
|
|
|
|
|
, testProperty "parser state input" prop_state_input
|
|
|
|
|
, testProperty "parser state tab width" prop_state_tab
|
|
|
|
|
, testProperty "parser state general" prop_state
|
|
|
|
|
, testProperty "custom state parsing" prop_runParser'
|
|
|
|
|
, testProperty "custom state parsing (transformer)" prop_runParserT'
|
|
|
|
|
, testProperty "state on failure (mplus)" prop_stOnFail_0
|
|
|
|
|
, testProperty "state on failure (tab)" prop_stOnFail_1
|
|
|
|
|
, testProperty "state on failure (eof)" prop_stOnFail_2
|
|
|
|
|
, testProperty "state on failure (notFollowedBy)" prop_stOnFail_3
|
|
|
|
|
, testProperty "ReaderT try" prop_ReaderT_try
|
|
|
|
|
, testProperty "ReaderT notFollowedBy" prop_ReaderT_notFollowedBy
|
|
|
|
|
, testProperty "StateT alternative (<|>)" prop_StateT_alternative
|
|
|
|
|
, testProperty "StateT lookAhead" prop_StateT_lookAhead
|
|
|
|
|
, testProperty "StateT notFollowedBy" prop_StateT_notFollowedBy
|
|
|
|
|
, testProperty "WriterT" prop_WriterT ]
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
2015-09-18 12:41:18 +03:00
|
|
|
|
instance Arbitrary (State String) where
|
2015-10-25 22:24:48 +03:00
|
|
|
|
arbitrary = State <$> arbitrary <*> arbitrary <*> choose (1, 20)
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
|
|
|
|
-- Functor instance
|
|
|
|
|
|
|
|
|
|
prop_functor :: Integer -> Integer -> Property
|
|
|
|
|
prop_functor n m =
|
|
|
|
|
((+ m) <$> return n) /=\ n + m .&&. ((* n) <$> return m) /=\ n * m
|
|
|
|
|
|
|
|
|
|
-- Applicative instance
|
|
|
|
|
|
|
|
|
|
prop_applicative_0 :: Integer -> Integer -> Property
|
|
|
|
|
prop_applicative_0 n m = ((+) <$> pure n <*> pure m) /=\ n + m
|
|
|
|
|
|
|
|
|
|
prop_applicative_1 :: Integer -> Integer -> Property
|
|
|
|
|
prop_applicative_1 n m = (pure n *> pure m) /=\ m
|
|
|
|
|
|
|
|
|
|
prop_applicative_2 :: Integer -> Integer -> Property
|
|
|
|
|
prop_applicative_2 n m = (pure n <* pure m) /=\ n
|
|
|
|
|
|
|
|
|
|
-- Alternative instance
|
|
|
|
|
|
|
|
|
|
prop_alternative_0 :: Integer -> Property
|
|
|
|
|
prop_alternative_0 n = (empty <|> return n) /=\ n
|
|
|
|
|
|
|
|
|
|
prop_alternative_1 :: String -> String -> Property
|
|
|
|
|
prop_alternative_1 s0 s1
|
2015-08-24 13:35:58 +03:00
|
|
|
|
| s0 == s1 = checkParser p (Right s0) s1
|
|
|
|
|
| null s0 = checkParser p (posErr 0 s1 [uneCh (head s1), exEof]) s1
|
|
|
|
|
| s0 `isPrefixOf` s1 =
|
|
|
|
|
checkParser p (posErr s0l s1 [uneCh (s1 !! s0l), exEof]) s1
|
2015-08-20 14:12:44 +03:00
|
|
|
|
| otherwise = checkParser p (Right s0) s0 .&&. checkParser p (Right s1) s1
|
2016-02-17 20:32:48 +03:00
|
|
|
|
where p = string s0 <|> string s1
|
2015-08-24 13:35:58 +03:00
|
|
|
|
s0l = length s0
|
|
|
|
|
|
|
|
|
|
prop_alternative_2 :: Char -> Char -> Char -> Bool -> Property
|
|
|
|
|
prop_alternative_2 a b c l = checkParser p r s
|
|
|
|
|
where p = char a <|> (char b >> char a)
|
|
|
|
|
r | l = Right a
|
|
|
|
|
| a == b = posErr 1 s [uneCh c, exEof]
|
|
|
|
|
| a == c = Right a
|
|
|
|
|
| otherwise = posErr 1 s [uneCh c, exCh a]
|
|
|
|
|
s = if l then [a] else [b,c]
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
2015-08-26 13:15:30 +03:00
|
|
|
|
prop_alternative_3 :: Property
|
|
|
|
|
prop_alternative_3 = checkParser p r s
|
2016-02-17 20:32:48 +03:00
|
|
|
|
where p = asum [empty, string ">>>", empty, return "foo"] <?> "bar"
|
|
|
|
|
p' = bsum [empty, string ">>>", empty, return "foo"] <?> "bar"
|
2015-08-26 13:15:30 +03:00
|
|
|
|
bsum = foldl (<|>) empty
|
|
|
|
|
r = simpleParse p' s
|
|
|
|
|
s = ">>"
|
|
|
|
|
|
2015-09-23 16:46:24 +03:00
|
|
|
|
prop_alternative_4 :: NonNegative Int -> NonNegative Int
|
|
|
|
|
-> NonNegative Int -> Property
|
2015-08-26 13:15:30 +03:00
|
|
|
|
prop_alternative_4 a' b' c' = checkParser p r s
|
2015-08-20 14:12:44 +03:00
|
|
|
|
where [a,b,c] = getNonNegative <$> [a',b',c']
|
|
|
|
|
p = (++) <$> many (char 'a') <*> many (char 'b')
|
|
|
|
|
r | null s = Right s
|
2015-08-21 17:08:15 +03:00
|
|
|
|
| c > 0 = posErr (a + b) s $ [uneCh 'c', exCh 'b', exEof]
|
|
|
|
|
++ [exCh 'a' | b == 0]
|
2015-08-20 14:12:44 +03:00
|
|
|
|
| otherwise = Right s
|
2015-08-21 17:08:15 +03:00
|
|
|
|
s = abcRow a b c
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
2015-09-23 16:46:24 +03:00
|
|
|
|
prop_alternative_5 :: NonNegative Int -> NonNegative Int
|
|
|
|
|
-> NonNegative Int -> Property
|
2015-08-26 13:15:30 +03:00
|
|
|
|
prop_alternative_5 a' b' c' = checkParser p r s
|
2015-08-20 14:12:44 +03:00
|
|
|
|
where [a,b,c] = getNonNegative <$> [a',b',c']
|
|
|
|
|
p = (++) <$> some (char 'a') <*> some (char 'b')
|
|
|
|
|
r | null s = posErr 0 s [uneEof, exCh 'a']
|
|
|
|
|
| a == 0 = posErr 0 s [uneCh (head s), exCh 'a']
|
|
|
|
|
| b == 0 = posErr a s $ [exCh 'a', exCh 'b'] ++
|
|
|
|
|
if c > 0 then [uneCh 'c'] else [uneEof]
|
|
|
|
|
| c > 0 = posErr (a + b) s [uneCh 'c', exCh 'b', exEof]
|
|
|
|
|
| otherwise = Right s
|
2015-08-21 17:08:15 +03:00
|
|
|
|
s = abcRow a b c
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
2015-08-26 13:15:30 +03:00
|
|
|
|
prop_alternative_6 :: Bool -> Bool -> Bool -> Property
|
|
|
|
|
prop_alternative_6 a b c = checkParser p r s
|
2015-08-20 14:12:44 +03:00
|
|
|
|
where p = f <$> optional (char 'a') <*> optional (char 'b')
|
|
|
|
|
f x y = maybe "" (:[]) x ++ maybe "" (:[]) y
|
|
|
|
|
r | c = posErr ab s $ [uneCh 'c', exEof] ++
|
|
|
|
|
[exCh 'a' | not a && not b] ++ [exCh 'b' | not b]
|
|
|
|
|
| otherwise = Right s
|
2016-02-17 21:12:27 +03:00
|
|
|
|
s = abcRow a b c
|
2015-08-20 14:12:44 +03:00
|
|
|
|
ab = fromEnum a + fromEnum b
|
|
|
|
|
|
|
|
|
|
-- Monad instance
|
|
|
|
|
|
|
|
|
|
prop_monad_0 :: Integer -> Property
|
|
|
|
|
prop_monad_0 n = checkParser (return n) (Right n) ""
|
|
|
|
|
|
|
|
|
|
prop_monad_1 :: Char -> Char -> Maybe Char -> Property
|
|
|
|
|
prop_monad_1 a b c = checkParser p r s
|
|
|
|
|
where p = char a >> char b
|
|
|
|
|
r = simpleParse (char a *> char b) s
|
|
|
|
|
s = a : b : maybeToList c
|
|
|
|
|
|
|
|
|
|
prop_monad_2 :: Char -> Char -> Maybe Char -> Property
|
|
|
|
|
prop_monad_2 a b c = checkParser p r s
|
|
|
|
|
where p = char a >>= \x -> char b >> return x
|
|
|
|
|
r = simpleParse (char a <* char b) s
|
|
|
|
|
s = a : b : maybeToList c
|
|
|
|
|
|
|
|
|
|
prop_monad_3 :: String -> Property
|
|
|
|
|
prop_monad_3 m = checkParser p r s
|
|
|
|
|
where p = fail m :: Parser ()
|
|
|
|
|
r | null m = posErr 0 s []
|
|
|
|
|
| otherwise = posErr 0 s [msg m]
|
|
|
|
|
s = ""
|
|
|
|
|
|
2015-10-25 19:20:30 +03:00
|
|
|
|
prop_monad_left_id :: Integer -> Integer -> Property
|
2015-11-07 13:24:45 +03:00
|
|
|
|
prop_monad_left_id a b = (return a >>= f) !=! f a
|
2015-10-25 19:20:30 +03:00
|
|
|
|
where f x = return $ x + b
|
2015-08-21 22:13:20 +03:00
|
|
|
|
|
2015-10-25 19:20:30 +03:00
|
|
|
|
prop_monad_right_id :: Integer -> Property
|
|
|
|
|
prop_monad_right_id a = (m >>= return) !=! m
|
|
|
|
|
where m = return a
|
2015-09-21 18:40:27 +03:00
|
|
|
|
|
2015-10-25 19:20:30 +03:00
|
|
|
|
prop_monad_assoc :: Integer -> Integer -> Integer -> Property
|
|
|
|
|
prop_monad_assoc a b c = ((m >>= f) >>= g) !=! (m >>= (\x -> f x >>= g))
|
|
|
|
|
where m = return a
|
|
|
|
|
f x = return $ x + b
|
|
|
|
|
g x = return $ x + c
|
2015-09-21 18:40:27 +03:00
|
|
|
|
|
2015-10-25 19:20:30 +03:00
|
|
|
|
-- MonadReader instance
|
|
|
|
|
|
|
|
|
|
prop_monad_reader_ask :: Integer -> Property
|
|
|
|
|
prop_monad_reader_ask a = runReader (runParserT ask "" "") a === Right a
|
|
|
|
|
|
|
|
|
|
prop_monad_reader_local :: Integer -> Integer -> Property
|
|
|
|
|
prop_monad_reader_local a b = runReader (runParserT p "" "") a === Right (a + b)
|
|
|
|
|
where p = local (+ b) ask
|
|
|
|
|
|
|
|
|
|
-- MonadState instance
|
|
|
|
|
|
|
|
|
|
prop_monad_state_get :: Integer -> Property
|
|
|
|
|
prop_monad_state_get a = L.evalState (runParserT L.get "" "") a === Right a
|
|
|
|
|
|
|
|
|
|
prop_monad_state_put :: Integer -> Integer -> Property
|
|
|
|
|
prop_monad_state_put a b = L.execState (runParserT (L.put b) "" "") a === b
|
|
|
|
|
|
|
|
|
|
-- MonadCont instance
|
|
|
|
|
|
|
|
|
|
prop_monad_cont :: Integer -> Integer -> Property
|
|
|
|
|
prop_monad_cont a b = runCont (runParserT p "" "") id === Right (max a b)
|
|
|
|
|
where p = do x <- callCC $ \e -> when (a > b) (e a) >> return b
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
-- MonadError instance
|
|
|
|
|
|
|
|
|
|
prop_monad_error_throw :: Integer -> Integer -> Property
|
|
|
|
|
prop_monad_error_throw a b = runExcept (runParserT p "" "") === Left a
|
|
|
|
|
where p = throwError a >> return b
|
|
|
|
|
|
|
|
|
|
prop_monad_error_catch :: Integer -> Integer -> Property
|
|
|
|
|
prop_monad_error_catch a b =
|
|
|
|
|
runExcept (runParserT p "" "") === Right (Right $ a + b)
|
|
|
|
|
where p = (throwError a >> return b) `catchError` handler
|
|
|
|
|
handler e = return $ e + b
|
2015-08-21 22:13:20 +03:00
|
|
|
|
|
2015-08-20 14:12:44 +03:00
|
|
|
|
-- Primitive combinators
|
|
|
|
|
|
|
|
|
|
prop_unexpected :: String -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_unexpected m = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
|
|
|
|
p = unexpected m
|
|
|
|
|
r = posErr 0 s $ if null m then [] else [uneSpec m]
|
2015-10-26 10:52:21 +03:00
|
|
|
|
s = ""
|
|
|
|
|
|
|
|
|
|
prop_failure :: [Message] -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_failure msgs = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
|
|
|
|
p = failure msgs
|
2015-10-26 10:52:21 +03:00
|
|
|
|
r | null msgs = posErr 0 s []
|
|
|
|
|
| otherwise = Left $ newErrorMessages msgs (initialPos "")
|
2015-08-20 14:12:44 +03:00
|
|
|
|
s = ""
|
|
|
|
|
|
2015-09-23 16:46:24 +03:00
|
|
|
|
prop_label :: NonNegative Int -> NonNegative Int
|
|
|
|
|
-> NonNegative Int -> String -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_label a' b' c' l = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
2015-08-21 17:08:15 +03:00
|
|
|
|
p = (++) <$> many (char 'a') <*> (many (char 'b') <?> l)
|
|
|
|
|
r | null s = Right s
|
2015-09-22 12:09:40 +03:00
|
|
|
|
| c > 0 = posErr (a + b) s $ [uneCh 'c', exEof]
|
2015-08-21 17:08:15 +03:00
|
|
|
|
++ [exCh 'a' | b == 0]
|
2015-09-22 12:09:40 +03:00
|
|
|
|
++ [if b == 0 || null l
|
|
|
|
|
then exSpec l
|
|
|
|
|
else exSpec $ "rest of " ++ l]
|
2015-08-21 17:08:15 +03:00
|
|
|
|
| otherwise = Right s
|
|
|
|
|
s = abcRow a b c
|
2016-02-18 20:36:26 +03:00
|
|
|
|
[a,b,c] = getNonNegative <$> [a',b',c']
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
2015-09-23 16:46:24 +03:00
|
|
|
|
prop_hidden_0 :: NonNegative Int -> NonNegative Int
|
|
|
|
|
-> NonNegative Int -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_hidden_0 a' b' c' = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
2015-08-21 17:08:15 +03:00
|
|
|
|
p = (++) <$> many (char 'a') <*> hidden (many (char 'b'))
|
|
|
|
|
r | null s = Right s
|
|
|
|
|
| c > 0 = posErr (a + b) s $ [uneCh 'c', exEof]
|
|
|
|
|
++ [exCh 'a' | b == 0]
|
|
|
|
|
| otherwise = Right s
|
|
|
|
|
s = abcRow a b c
|
2016-02-18 20:36:26 +03:00
|
|
|
|
[a,b,c] = getNonNegative <$> [a',b',c']
|
2015-08-21 17:08:15 +03:00
|
|
|
|
|
2016-02-17 20:32:48 +03:00
|
|
|
|
prop_hidden_1 :: NonEmptyList Char -> String -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_hidden_1 c' s = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m (Maybe String)
|
2016-02-17 20:32:48 +03:00
|
|
|
|
p = optional (hidden $ string c)
|
|
|
|
|
r | null s = Right Nothing
|
|
|
|
|
| c == s = Right (Just s)
|
|
|
|
|
| c `isPrefixOf` s = posErr cn s [uneCh (s !! cn), exEof]
|
|
|
|
|
| otherwise = posErr 0 s [uneCh (head s), exEof]
|
2016-02-18 20:36:26 +03:00
|
|
|
|
c = getNonEmpty c'
|
|
|
|
|
cn = length c
|
2016-02-17 20:32:48 +03:00
|
|
|
|
|
|
|
|
|
prop_try :: Char -> Char -> Char -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_try pre ch1 ch2 = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
|
|
|
|
p = try (sequence [char pre, char ch1])
|
|
|
|
|
<|> sequence [char pre, char ch2]
|
2016-02-17 20:32:48 +03:00
|
|
|
|
r = posErr 1 s [uneEof, exCh ch1, exCh ch2]
|
|
|
|
|
s = [pre]
|
2015-08-21 17:08:15 +03:00
|
|
|
|
|
|
|
|
|
prop_lookAhead_0 :: Bool -> Bool -> Bool -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_lookAhead_0 a b c = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m Char
|
|
|
|
|
p = do
|
2015-08-21 17:08:15 +03:00
|
|
|
|
l <- lookAhead (oneOf "ab" <?> "label")
|
|
|
|
|
guard (l == h)
|
|
|
|
|
char 'a'
|
|
|
|
|
h = head s
|
|
|
|
|
r | null s = posErr 0 s [uneEof, exSpec "label"]
|
|
|
|
|
| s == "a" = Right 'a'
|
|
|
|
|
| h == 'b' = posErr 0 s [uneCh 'b', exCh 'a']
|
|
|
|
|
| h == 'c' = posErr 0 s [uneCh 'c', exSpec "label"]
|
|
|
|
|
| otherwise = posErr 1 s [uneCh (s !! 1), exEof]
|
2016-02-17 21:12:27 +03:00
|
|
|
|
s = abcRow a b c
|
2015-08-21 17:08:15 +03:00
|
|
|
|
|
|
|
|
|
prop_lookAhead_1 :: String -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_lookAhead_1 s = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m ()
|
|
|
|
|
p = lookAhead (some letterChar) >> fail "failed"
|
2015-08-21 17:08:15 +03:00
|
|
|
|
h = head s
|
|
|
|
|
r | null s = posErr 0 s [uneEof, exSpec "letter"]
|
|
|
|
|
| isLetter h = posErr 0 s [msg "failed"]
|
|
|
|
|
| otherwise = posErr 0 s [uneCh h, exSpec "letter"]
|
|
|
|
|
|
2015-08-21 22:13:20 +03:00
|
|
|
|
prop_lookAhead_2 :: Bool -> Bool -> Bool -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_lookAhead_2 a b c = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m Char
|
|
|
|
|
p = lookAhead (some (char 'a')) >> char 'b'
|
2015-08-21 22:13:20 +03:00
|
|
|
|
r | null s = posErr 0 s [uneEof, exCh 'a']
|
|
|
|
|
| a = posErr 0 s [uneCh 'a', exCh 'b']
|
|
|
|
|
| otherwise = posErr 0 s [uneCh (head s), exCh 'a']
|
2016-02-17 21:12:27 +03:00
|
|
|
|
s = abcRow a b c
|
|
|
|
|
|
|
|
|
|
case_lookAhead_3 :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_lookAhead_3 = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
|
|
|
|
p = lookAhead (char 'a' *> fail emsg)
|
|
|
|
|
r = posErr 1 s [msg emsg]
|
2016-02-17 21:12:27 +03:00
|
|
|
|
emsg = "ops!"
|
2016-02-18 20:36:26 +03:00
|
|
|
|
s = "abc"
|
2015-08-21 22:13:20 +03:00
|
|
|
|
|
2015-09-23 16:46:24 +03:00
|
|
|
|
prop_notFollowedBy_0 :: NonNegative Int -> NonNegative Int
|
|
|
|
|
-> NonNegative Int -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_notFollowedBy_0 a' b' c' = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
2015-08-21 17:08:15 +03:00
|
|
|
|
p = many (char 'a') <* notFollowedBy (char 'b') <* many (char 'c')
|
2016-02-18 20:36:26 +03:00
|
|
|
|
r | b > 0 = posErr a s [uneCh 'b', exCh 'a']
|
2015-08-21 17:08:15 +03:00
|
|
|
|
| otherwise = Right (replicate a 'a')
|
|
|
|
|
s = abcRow a b c
|
2016-02-18 20:36:26 +03:00
|
|
|
|
[a,b,c] = getNonNegative <$> [a',b',c']
|
2015-08-21 17:08:15 +03:00
|
|
|
|
|
2015-09-23 16:46:24 +03:00
|
|
|
|
prop_notFollowedBy_1 :: NonNegative Int -> NonNegative Int
|
|
|
|
|
-> NonNegative Int -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_notFollowedBy_1 a' b' c' = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
|
|
|
|
p = many (char 'a')
|
|
|
|
|
<* (notFollowedBy . notFollowedBy) (char 'c')
|
|
|
|
|
<* many (char 'c')
|
2015-08-21 17:08:15 +03:00
|
|
|
|
r | b == 0 && c > 0 = Right (replicate a 'a')
|
|
|
|
|
| b > 0 = posErr a s [uneCh 'b', exCh 'a']
|
|
|
|
|
| otherwise = posErr a s [uneEof, exCh 'a']
|
|
|
|
|
s = abcRow a b c
|
2016-02-18 20:36:26 +03:00
|
|
|
|
[a,b,c] = getNonNegative <$> [a',b',c']
|
2015-08-21 17:08:15 +03:00
|
|
|
|
|
2015-09-23 16:46:24 +03:00
|
|
|
|
prop_notFollowedBy_2 :: NonNegative Int -> NonNegative Int
|
|
|
|
|
-> NonNegative Int -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_notFollowedBy_2 a' b' c' = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
2015-08-21 17:08:15 +03:00
|
|
|
|
p = many (char 'a') <* notFollowedBy eof <* many anyChar
|
|
|
|
|
r | b > 0 || c > 0 = Right (replicate a 'a')
|
|
|
|
|
| otherwise = posErr a s [uneEof, exCh 'a']
|
|
|
|
|
s = abcRow a b c
|
2016-02-18 20:36:26 +03:00
|
|
|
|
[a,b,c] = getNonNegative <$> [a',b',c']
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
2016-02-17 21:12:27 +03:00
|
|
|
|
case_notFollowedBy_3a :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_notFollowedBy_3a = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m ()
|
|
|
|
|
p = notFollowedBy (char 'a' *> char 'c')
|
|
|
|
|
r = Right ()
|
|
|
|
|
s = "ab"
|
2016-02-17 21:12:27 +03:00
|
|
|
|
|
|
|
|
|
case_notFollowedBy_3b :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_notFollowedBy_3b = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m ()
|
|
|
|
|
p = notFollowedBy (char 'a' *> char 'd') <* char 'c'
|
|
|
|
|
r = posErr 0 s [uneCh 'a', exCh 'c']
|
2016-02-17 21:12:27 +03:00
|
|
|
|
s = "ab"
|
|
|
|
|
|
|
|
|
|
case_notFollowedBy_4a :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_notFollowedBy_4a = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m ()
|
2016-02-19 13:04:49 +03:00
|
|
|
|
p = notFollowedBy mzero
|
2016-02-18 20:36:26 +03:00
|
|
|
|
r = Right ()
|
|
|
|
|
s = "ab"
|
2016-02-17 21:12:27 +03:00
|
|
|
|
|
|
|
|
|
case_notFollowedBy_4b :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_notFollowedBy_4b = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m ()
|
2016-02-19 13:04:49 +03:00
|
|
|
|
p = notFollowedBy mzero <* char 'c'
|
2016-02-18 20:36:26 +03:00
|
|
|
|
r = posErr 0 s [uneCh 'a', exCh 'c']
|
2016-02-17 21:12:27 +03:00
|
|
|
|
s = "ab"
|
|
|
|
|
|
2016-02-17 23:07:08 +03:00
|
|
|
|
prop_withRecovery_0 :: NonNegative Int -> NonNegative Int
|
2016-02-18 20:36:26 +03:00
|
|
|
|
-> NonNegative Int -> Property
|
|
|
|
|
prop_withRecovery_0 a' b' c' = checkParser' p r s
|
|
|
|
|
where
|
|
|
|
|
p :: MonadParsec s m Char => m (Either ParseError String)
|
|
|
|
|
p = let g = count' 1 3 . char in v <$>
|
|
|
|
|
withRecovery (\e -> Left e <$ g 'b') (Right <$> g 'a') <*> g 'c'
|
|
|
|
|
v (Right x) y = Right (x ++ y)
|
|
|
|
|
v (Left m) _ = Left m
|
|
|
|
|
r | a == 0 && b == 0 && c == 0 = posErr 0 s [uneEof, exCh 'a']
|
|
|
|
|
| a == 0 && b == 0 && c > 3 = posErr 0 s [uneCh 'c', exCh 'a']
|
|
|
|
|
| a == 0 && b == 0 = posErr 0 s [uneCh 'c', exCh 'a']
|
|
|
|
|
| a == 0 && b > 3 = posErr 3 s [uneCh 'b', exCh 'a', exCh 'c']
|
|
|
|
|
| a == 0 && c == 0 = posErr b s [uneEof, exCh 'a', exCh 'c']
|
|
|
|
|
| a == 0 && c > 3 = posErr (b + 3) s [uneCh 'c', exEof]
|
|
|
|
|
| a == 0 = Right (posErr 0 s [uneCh 'b', exCh 'a'])
|
|
|
|
|
| a > 3 = posErr 3 s [uneCh 'a', exCh 'c']
|
|
|
|
|
| b == 0 && c == 0 = posErr a s $ [uneEof, exCh 'c'] ++ ma
|
|
|
|
|
| b == 0 && c > 3 = posErr (a + 3) s [uneCh 'c', exEof]
|
|
|
|
|
| b == 0 = Right (Right s)
|
|
|
|
|
| otherwise = posErr a s $ [uneCh 'b', exCh 'c'] ++ ma
|
|
|
|
|
ma = [exCh 'a' | a < 3]
|
|
|
|
|
s = abcRow a b c
|
|
|
|
|
[a,b,c] = getNonNegative <$> [a',b',c']
|
2016-02-17 23:07:08 +03:00
|
|
|
|
|
|
|
|
|
case_withRecovery_1 :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_withRecovery_1 = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
|
|
|
|
p = withRecovery (const $ return "bar") (return "foo")
|
|
|
|
|
r = Right "foo"
|
|
|
|
|
s = "abc"
|
2016-02-17 23:07:08 +03:00
|
|
|
|
|
|
|
|
|
case_withRecovery_2 :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_withRecovery_2 = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
2016-02-19 13:04:49 +03:00
|
|
|
|
p = withRecovery (\_ -> char 'a' *> mzero) (string "cba")
|
2016-02-18 20:36:26 +03:00
|
|
|
|
r = posErr 0 s [uneCh 'a', exStr "cba"]
|
2016-02-17 23:07:08 +03:00
|
|
|
|
s = "abc"
|
|
|
|
|
|
|
|
|
|
case_withRecovery_3a :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_withRecovery_3a = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
|
|
|
|
p = withRecovery (const $ return "abd") (string "cba")
|
|
|
|
|
r = Right "abd"
|
|
|
|
|
s = "abc"
|
2016-02-17 23:07:08 +03:00
|
|
|
|
|
|
|
|
|
case_withRecovery_3b :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_withRecovery_3b = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
|
|
|
|
p = withRecovery (const $ return "abd") (string "cba") <* char 'd'
|
|
|
|
|
r = posErr 0 s [uneCh 'a', exStr "cba", exCh 'd']
|
2016-02-17 23:07:08 +03:00
|
|
|
|
s = "abc"
|
|
|
|
|
|
|
|
|
|
case_withRecovery_4a :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_withRecovery_4a = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
2016-02-19 13:04:49 +03:00
|
|
|
|
p = withRecovery (const $ string "bc") (char 'a' *> mzero)
|
2016-02-18 20:36:26 +03:00
|
|
|
|
r = Right "bc"
|
|
|
|
|
s = "abc"
|
2016-02-17 23:07:08 +03:00
|
|
|
|
|
|
|
|
|
case_withRecovery_4b :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_withRecovery_4b = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
|
|
|
|
p = withRecovery (const $ string "bc")
|
|
|
|
|
(char 'a' *> char 'd' *> pure "foo") <* char 'f'
|
|
|
|
|
r = posErr 3 s [uneEof, exCh 'f']
|
2016-02-17 23:07:08 +03:00
|
|
|
|
s = "abc"
|
|
|
|
|
|
|
|
|
|
case_withRecovery_5 :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_withRecovery_5 = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
2016-02-17 23:07:08 +03:00
|
|
|
|
p = withRecovery (\_ -> char 'b' *> fail emsg) (char 'a' *> fail emsg)
|
2016-02-18 20:36:26 +03:00
|
|
|
|
r = posErr 1 s [msg emsg]
|
2016-02-17 23:07:08 +03:00
|
|
|
|
emsg = "ops!"
|
|
|
|
|
s = "abc"
|
|
|
|
|
|
|
|
|
|
case_withRecovery_6a :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_withRecovery_6a = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m String
|
2016-02-19 13:04:49 +03:00
|
|
|
|
p = withRecovery (const $ return "abd") (char 'a' *> mzero)
|
2016-02-18 20:36:26 +03:00
|
|
|
|
r = Right "abd"
|
|
|
|
|
s = "abc"
|
2016-02-17 23:07:08 +03:00
|
|
|
|
|
|
|
|
|
case_withRecovery_6b :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_withRecovery_6b = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m Char
|
|
|
|
|
p = withRecovery (const $ return 'g') (char 'a' *> char 'd') <* char 'f'
|
|
|
|
|
r = posErr 1 s [uneCh 'b', exCh 'd', exCh 'f']
|
2016-02-17 23:07:08 +03:00
|
|
|
|
s = "abc"
|
|
|
|
|
|
|
|
|
|
case_withRecovery_7 :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_withRecovery_7 = checkCase p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m Char
|
2016-02-19 13:04:49 +03:00
|
|
|
|
p = withRecovery (const mzero) (char 'a' *> char 'd')
|
2016-02-18 20:36:26 +03:00
|
|
|
|
r = posErr 1 s [uneCh 'b', exCh 'd']
|
2016-02-17 23:07:08 +03:00
|
|
|
|
s = "abc"
|
|
|
|
|
|
2016-02-17 21:12:27 +03:00
|
|
|
|
case_eof :: Assertion
|
2016-02-18 20:36:26 +03:00
|
|
|
|
case_eof = checkCase eof (Right ()) ""
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
2015-08-21 22:13:20 +03:00
|
|
|
|
prop_token :: String -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_token s = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec s m Char => m Char
|
|
|
|
|
p = token updatePosChar testChar
|
2015-09-23 13:47:17 +03:00
|
|
|
|
testChar x = if isLetter x
|
2016-02-18 20:36:26 +03:00
|
|
|
|
then Right x
|
|
|
|
|
else Left . pure . Unexpected . showToken $ x
|
2015-08-21 22:13:20 +03:00
|
|
|
|
h = head s
|
|
|
|
|
r | null s = posErr 0 s [uneEof]
|
|
|
|
|
| isLetter h && length s == 1 = Right (head s)
|
|
|
|
|
| isLetter h && length s > 1 = posErr 1 s [uneCh (s !! 1), exEof]
|
|
|
|
|
| otherwise = posErr 0 s [uneCh h]
|
|
|
|
|
|
|
|
|
|
prop_tokens :: String -> String -> Property
|
2015-09-04 15:12:59 +03:00
|
|
|
|
prop_tokens a = checkString p a (==) (showToken a)
|
|
|
|
|
where p = tokens updatePosString (==) a
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
|
|
|
|
-- Parser state combinators
|
|
|
|
|
|
|
|
|
|
prop_state_pos :: SourcePos -> Property
|
|
|
|
|
prop_state_pos pos = p /=\ pos
|
|
|
|
|
where p = setPosition pos >> getPosition
|
|
|
|
|
|
|
|
|
|
prop_state_input :: String -> Property
|
|
|
|
|
prop_state_input s = p /=\ s
|
|
|
|
|
where p = do
|
|
|
|
|
st0 <- getInput
|
|
|
|
|
guard (null st0)
|
|
|
|
|
setInput s
|
|
|
|
|
result <- string s
|
|
|
|
|
st1 <- getInput
|
|
|
|
|
guard (null st1)
|
|
|
|
|
return result
|
|
|
|
|
|
2015-09-23 13:47:17 +03:00
|
|
|
|
prop_state_tab :: Int -> Property
|
|
|
|
|
prop_state_tab w = p /=\ w
|
|
|
|
|
where p = setTabWidth w >> getTabWidth
|
|
|
|
|
|
2015-09-18 12:41:18 +03:00
|
|
|
|
prop_state :: State String -> State String -> Property
|
2016-02-18 20:36:26 +03:00
|
|
|
|
prop_state s1 s2 = checkParser' p r s
|
|
|
|
|
where p :: MonadParsec String m Char => m (State String)
|
2015-08-20 14:12:44 +03:00
|
|
|
|
p = do
|
|
|
|
|
st <- getParserState
|
2016-02-18 20:36:26 +03:00
|
|
|
|
guard (st == State s (initialPos "") defaultTabWidth)
|
2015-08-20 14:12:44 +03:00
|
|
|
|
setParserState s1
|
|
|
|
|
updateParserState (f s2)
|
2016-02-18 20:36:26 +03:00
|
|
|
|
liftM2 const getParserState (setInput "")
|
|
|
|
|
f (State s1' pos w) (State s2' _ _) = State (max s1' s2' ) pos w
|
|
|
|
|
r = Right (f s2 s1)
|
|
|
|
|
s = ""
|
2015-08-20 14:12:44 +03:00
|
|
|
|
|
2015-10-25 22:24:48 +03:00
|
|
|
|
-- Running a parser
|
|
|
|
|
|
|
|
|
|
prop_runParser' :: State String -> String -> Property
|
|
|
|
|
prop_runParser' st s = runParser' p st === r
|
|
|
|
|
where p = string s
|
|
|
|
|
r = emulateStrParsing st s
|
|
|
|
|
|
|
|
|
|
prop_runParserT' :: State String -> String -> Property
|
|
|
|
|
prop_runParserT' st s = runIdentity (runParserT' p st) === r
|
|
|
|
|
where p = string s
|
|
|
|
|
r = emulateStrParsing st s
|
|
|
|
|
|
|
|
|
|
emulateStrParsing :: State String
|
|
|
|
|
-> String
|
|
|
|
|
-> (State String, Either ParseError String)
|
|
|
|
|
emulateStrParsing st@(State i pos t) s =
|
|
|
|
|
if l == length s
|
|
|
|
|
then (State (drop l i) (updatePosString t pos s) t, Right s)
|
|
|
|
|
else let uneStuff = if null i then uneEof else uneStr (take (l + 1) i)
|
|
|
|
|
in (st, Left $ newErrorMessages (exStr s : [uneStuff]) pos)
|
|
|
|
|
where l = length $ takeWhile id $ zipWith (==) s i
|
|
|
|
|
|
2016-02-07 21:37:35 +03:00
|
|
|
|
-- Additional tests to check returned state on failure
|
|
|
|
|
|
|
|
|
|
prop_stOnFail_0 :: Positive Int -> Positive Int -> Property
|
|
|
|
|
prop_stOnFail_0 na' nb' = runParser' p (stateFromInput s) === (i, r)
|
|
|
|
|
where i = let (Left x) = r in State "" (errorPos x) defaultTabWidth
|
|
|
|
|
na = getPositive na'
|
|
|
|
|
nb = getPositive nb'
|
|
|
|
|
p = try (many (char 'a') <* many (char 'b') <* char 'c')
|
|
|
|
|
<|> (many (char 'a') <* char 'c')
|
|
|
|
|
r = posErr (na + nb) s [exCh 'b', exCh 'c', uneEof]
|
|
|
|
|
s = replicate na 'a' ++ replicate nb 'b'
|
|
|
|
|
|
|
|
|
|
prop_stOnFail_1 :: Positive Int -> Positive Int -> Property
|
|
|
|
|
prop_stOnFail_1 na' t' = runParser' p (stateFromInput s) === (i, r)
|
|
|
|
|
where i = let (Left x) = r in State "" (errorPos x) t
|
|
|
|
|
na = getPositive na'
|
|
|
|
|
t = getPositive t'
|
|
|
|
|
p = many (char 'a') <* setTabWidth t <* fail myMsg
|
|
|
|
|
r = posErr na s [msg myMsg]
|
|
|
|
|
s = replicate na 'a'
|
|
|
|
|
myMsg = "failing now!"
|
|
|
|
|
|
|
|
|
|
prop_stOnFail_2 :: String -> Char -> Property
|
|
|
|
|
prop_stOnFail_2 s' ch = runParser' p (stateFromInput s) === (i, r)
|
|
|
|
|
where i = let (Left x) = r in State [ch] (errorPos x) defaultTabWidth
|
|
|
|
|
r = posErr (length s') s [uneCh ch, exEof]
|
|
|
|
|
p = string s' <* eof
|
|
|
|
|
s = s' ++ [ch]
|
|
|
|
|
|
|
|
|
|
prop_stOnFail_3 :: String -> Property
|
|
|
|
|
prop_stOnFail_3 s = runParser' p (stateFromInput s) === (i, r)
|
|
|
|
|
where i = let (Left x) = r in State s (errorPos x) defaultTabWidth
|
|
|
|
|
r = posErr 0 s [if null s then uneEof else uneCh (head s)]
|
|
|
|
|
p = notFollowedBy (string s)
|
|
|
|
|
|
2016-03-05 06:33:32 +03:00
|
|
|
|
stateFromInput :: s -> State s
|
2016-02-07 21:37:35 +03:00
|
|
|
|
stateFromInput s = State s (initialPos "") defaultTabWidth
|
|
|
|
|
|
2015-09-21 18:40:27 +03:00
|
|
|
|
-- ReaderT instance of MonadParsec
|
|
|
|
|
|
2016-02-17 20:32:48 +03:00
|
|
|
|
prop_ReaderT_try :: Char -> Char -> Char -> Property
|
|
|
|
|
prop_ReaderT_try pre ch1 ch2 = checkParser (runReaderT p (s1, s2)) r s
|
|
|
|
|
where s1 = pre : [ch1]
|
|
|
|
|
s2 = pre : [ch2]
|
|
|
|
|
getS1 = asks fst
|
|
|
|
|
getS2 = asks snd
|
|
|
|
|
p = try (g =<< getS1) <|> (g =<< getS2)
|
|
|
|
|
g = sequence . fmap char
|
|
|
|
|
r = posErr 1 s [uneEof, exCh ch1, exCh ch2]
|
|
|
|
|
s = [pre]
|
2015-09-21 16:59:04 +03:00
|
|
|
|
|
2015-09-21 18:40:27 +03:00
|
|
|
|
prop_ReaderT_notFollowedBy :: NonNegative Int -> NonNegative Int
|
|
|
|
|
-> NonNegative Int -> Property
|
|
|
|
|
prop_ReaderT_notFollowedBy a' b' c' = checkParser (runReaderT p 'a') r s
|
2015-09-21 16:59:04 +03:00
|
|
|
|
where [a,b,c] = getNonNegative <$> [a',b',c']
|
2015-09-21 18:40:27 +03:00
|
|
|
|
p = many (char =<< ask) <* notFollowedBy eof <* many anyChar
|
2015-09-21 16:59:04 +03:00
|
|
|
|
r | b > 0 || c > 0 = Right (replicate a 'a')
|
|
|
|
|
| otherwise = posErr a s [uneEof, exCh 'a']
|
|
|
|
|
s = abcRow a b c
|
|
|
|
|
|
2015-09-21 18:40:27 +03:00
|
|
|
|
-- StateT instance of MonadParsec
|
2015-09-21 16:59:04 +03:00
|
|
|
|
|
2015-09-21 20:12:17 +03:00
|
|
|
|
prop_StateT_alternative :: Integer -> Property
|
2015-10-28 14:51:35 +03:00
|
|
|
|
prop_StateT_alternative n =
|
|
|
|
|
checkParser (L.evalStateT p 0) (Right n) "" .&&.
|
|
|
|
|
checkParser (S.evalStateT p' 0) (Right n) ""
|
2015-09-23 16:46:24 +03:00
|
|
|
|
where p = L.put n >> ((L.modify (* 2) >>
|
|
|
|
|
void (string "xxx")) <|> return ()) >> L.get
|
|
|
|
|
p' = S.put n >> ((S.modify (* 2) >>
|
|
|
|
|
void (string "xxx")) <|> return ()) >> S.get
|
2015-09-21 20:12:17 +03:00
|
|
|
|
|
|
|
|
|
prop_StateT_lookAhead :: Integer -> Property
|
2015-10-28 14:51:35 +03:00
|
|
|
|
prop_StateT_lookAhead n =
|
|
|
|
|
checkParser (L.evalStateT p 0) (Right n) "" .&&.
|
|
|
|
|
checkParser (S.evalStateT p' 0) (Right n) ""
|
2015-09-21 20:12:17 +03:00
|
|
|
|
where p = L.put n >> lookAhead (L.modify (* 2) >> eof) >> L.get
|
|
|
|
|
p' = S.put n >> lookAhead (S.modify (* 2) >> eof) >> S.get
|
|
|
|
|
|
|
|
|
|
prop_StateT_notFollowedBy :: Integer -> Property
|
|
|
|
|
prop_StateT_notFollowedBy n = checkParser (L.runStateT p 0) r "abx" .&&.
|
|
|
|
|
checkParser (S.runStateT p' 0) r "abx"
|
2015-09-21 16:59:04 +03:00
|
|
|
|
where p = do
|
2015-09-21 18:40:27 +03:00
|
|
|
|
L.put n
|
|
|
|
|
let notEof = notFollowedBy (L.modify (* 2) >> eof)
|
2015-09-21 16:59:04 +03:00
|
|
|
|
some (try (anyChar <* notEof)) <* char 'x'
|
2015-09-21 20:12:17 +03:00
|
|
|
|
p' = do
|
2015-09-21 18:40:27 +03:00
|
|
|
|
S.put n
|
|
|
|
|
let notEof = notFollowedBy (S.modify (* 2) >> eof)
|
2015-09-21 16:59:04 +03:00
|
|
|
|
some (try (anyChar <* notEof)) <* char 'x'
|
2015-09-21 18:40:27 +03:00
|
|
|
|
r = Right ("ab", n)
|
|
|
|
|
|
|
|
|
|
-- WriterT instance of MonadParsec
|
2015-09-21 16:59:04 +03:00
|
|
|
|
|
2015-09-21 20:12:17 +03:00
|
|
|
|
prop_WriterT :: String -> String -> Property
|
2015-10-28 14:51:35 +03:00
|
|
|
|
prop_WriterT pre post =
|
|
|
|
|
checkParser (L.runWriterT p) r "abx" .&&.
|
|
|
|
|
checkParser (S.runWriterT p') r "abx"
|
2015-09-21 20:12:17 +03:00
|
|
|
|
where logged_letter = letterChar >>= \x -> L.tell [x] >> return x
|
|
|
|
|
logged_letter' = letterChar >>= \x -> L.tell [x] >> return x
|
|
|
|
|
logged_eof = eof >> L.tell "EOF"
|
|
|
|
|
logged_eof' = eof >> L.tell "EOF"
|
2015-09-21 16:59:04 +03:00
|
|
|
|
p = do
|
2015-09-21 18:40:27 +03:00
|
|
|
|
L.tell pre
|
|
|
|
|
cs <- L.censor (fmap toUpper) $
|
2015-09-21 16:59:04 +03:00
|
|
|
|
some (try (logged_letter <* notFollowedBy logged_eof))
|
2015-09-21 18:40:27 +03:00
|
|
|
|
L.tell post
|
|
|
|
|
void logged_letter
|
2015-09-21 16:59:04 +03:00
|
|
|
|
return cs
|
2015-09-21 20:12:17 +03:00
|
|
|
|
p' = do
|
|
|
|
|
L.tell pre
|
|
|
|
|
cs <- L.censor (fmap toUpper) $
|
|
|
|
|
some (try (logged_letter' <* notFollowedBy logged_eof'))
|
|
|
|
|
L.tell post
|
|
|
|
|
void logged_letter'
|
2015-09-21 16:59:04 +03:00
|
|
|
|
return cs
|
2015-09-21 18:40:27 +03:00
|
|
|
|
r = Right ("ab", pre ++ "AB" ++ post ++ "x")
|