fixed test for ‘Text.Megaparsec.Char.string'’

Closes #52.

Also added one new test for that function. The test is courtesy of
Benjamin Kästner (@bkaestner).
This commit is contained in:
mrkkrp 2015-10-05 00:10:59 +06:00
parent 17ba4fb4ee
commit 1d0e390593
2 changed files with 18 additions and 4 deletions

View File

@ -79,7 +79,8 @@ tests = testGroup "Character parsers"
, testProperty "noneOf" prop_noneOf , testProperty "noneOf" prop_noneOf
, testProperty "noneOf'" prop_noneOf' , testProperty "noneOf'" prop_noneOf'
, testProperty "string" prop_string , testProperty "string" prop_string
, testProperty "string'" prop_string' ] , testProperty "string'" prop_string'_0
, testProperty "string' (case)" prop_string'_1 ]
instance Arbitrary GeneralCategory where instance Arbitrary GeneralCategory where
arbitrary = elements arbitrary = elements
@ -238,8 +239,21 @@ prop_noneOf' a = checkChar (noneOf' a) (`notElemi` a) Nothing
prop_string :: String -> String -> Property prop_string :: String -> String -> Property
prop_string a = checkString (string a) a (==) (showToken a) prop_string a = checkString (string a) a (==) (showToken a)
prop_string' :: String -> String -> Property prop_string'_0 :: String -> String -> Property
prop_string' a = checkString (string' a) a casei (showToken a) prop_string'_0 a = checkString (string' a) a casei (showToken a)
-- | Randomly change the case in the given string.
fuzzyCase :: String -> Gen String
fuzzyCase s = do
b <- vector (length s)
return $ zipWith f s b
where f k True = if isLower k then toUpper k else toLower k
f k False = k
prop_string'_1 :: String -> Property
prop_string'_1 a = forAll (fuzzyCase a) $ \s ->
checkString (string' a) a casei (showToken a) s
-- | Case-insensitive equality test for characters. -- | Case-insensitive equality test for characters.

View File

@ -101,7 +101,7 @@ checkChar p f l' s = checkParser p r s
checkString :: Parser String -> String -> (Char -> Char -> Bool) checkString :: Parser String -> String -> (Char -> Char -> Bool)
-> String -> String -> Property -> String -> String -> Property
checkString p a' test l s' = checkParser p (w a' 0 s') s' checkString p a' test l s' = checkParser p (w a' 0 s') s'
where w [] _ [] = Right a' where w [] _ [] = Right s'
w [] i (s:_) = posErr i s' [uneCh s, exEof] w [] i (s:_) = posErr i s' [uneCh s, exEof]
w _ 0 [] = posErr 0 s' [uneEof, exSpec l] w _ 0 [] = posErr 0 s' [uneEof, exSpec l]
w _ i [] = posErr 0 s' [uneStr (take i s'), exSpec l] w _ i [] = posErr 0 s' [uneStr (take i s'), exSpec l]