module UnitTests.Distribution.Compat.ReadP
( tests
) where
import Data.List
import Distribution.Compat.ReadP
import Test.Framework
import Test.Framework.Providers.QuickCheck2
tests :: [Test]
tests =
[ testProperty "Get Nil" prop_Get_Nil
, testProperty "Get Cons" prop_Get_Cons
, testProperty "Look" prop_Look
, testProperty "Fail" prop_Fail
, testProperty "Return" prop_Return
, testProperty "String Yes" prop_String_Yes
, testProperty "String Maybe" prop_String_Maybe
, testProperty "Munch" (prop_Munch evenChar)
, testProperty "Munch1" (prop_Munch1 evenChar)
]
type Bag a = [a]
(=~) :: Ord a => Bag a -> Bag a -> Bool
xs =~ ys = sort xs == sort ys
(=~.) :: Bag (Int,String) -> Bag (Int,String) -> Bool
(=~.) = (=~)
prop_Get_Nil :: Bool
prop_Get_Nil =
readP_to_S get [] =~ []
prop_Get_Cons :: Char -> [Char] -> Bool
prop_Get_Cons c s =
readP_to_S get (c:s) =~ [(c,s)]
prop_Look :: String -> Bool
prop_Look s =
readP_to_S look s =~ [(s,s)]
prop_Fail :: String -> Bool
prop_Fail s =
readP_to_S pfail s =~. []
prop_Return :: Int -> String -> Bool
prop_Return x s =
readP_to_S (return x) s =~. [(x,s)]
prop_String_Yes :: String -> [Char] -> Bool
prop_String_Yes this s =
readP_to_S (string this) (this ++ s) =~
[(this,s)]
prop_String_Maybe :: String -> String -> Bool
prop_String_Maybe this s =
readP_to_S (string this) s =~
[(this, drop (length this) s) | this `isPrefixOf` s]
prop_Munch :: (Char -> Bool) -> String -> Bool
prop_Munch p s =
readP_to_S (munch p) s =~
[(takeWhile p s, dropWhile p s)]
prop_Munch1 :: (Char -> Bool) -> String -> Bool
prop_Munch1 p s =
readP_to_S (munch1 p) s =~
[(res,s') | let (res,s') = (takeWhile p s, dropWhile p s), not (null res)]
evenChar :: Char -> Bool
evenChar = even . fromEnum