papa-0.7.0: test/PropertiesText.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
{- HLINT ignore "Avoid restricted function" -}
-- Reason: these are the properties of the total head, last, init, tail,
-- maximum and minimum of the Text and ByteString modules, compared with the
-- partial functions of text and bytestring that they replace, whose names
-- HLint restricts whatever module they come from.
-- | Hedgehog properties of the Text and ByteString modules: each total
-- function agrees with the partial one it replaces wherever that one returns,
-- and is 'Nothing' (or an error) exactly where it throws; each checked
-- function agrees with the unchecked one wherever that one does not truncate;
-- and the I/O actions round-trip through a file.
module PropertiesText (
propertiesText,
) where
import Agree (agrees)
import qualified Control.Exception as E
import qualified Data.ByteString as SB
import qualified Data.ByteString.Builder as SBB
import qualified Data.ByteString.Char8 as SC
import qualified Data.ByteString.Lazy as SL
import qualified Data.ByteString.Lazy.Char8 as SLC
import qualified Data.List as List
import qualified Data.Text as ST
import qualified Data.Text.Encoding as STE
import qualified Data.Text.Lazy as STL
import qualified Data.Text.Lazy.Builder as STB
import qualified Data.Text.Lazy.Builder.Int as STBI
import qualified Data.Text.Lazy.Encoding as STLE
import qualified Data.Text.Read as STR
import Hedgehog (Gen, Group (Group), Property, PropertyName, PropertyT, evalIO, forAll, property, withTests, (===))
import Hedgehog.Function (Fn, apply, fn)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Papa
import qualified Papa.Bytestring.Data.ByteString as B
import qualified Papa.Bytestring.Data.ByteString.Builder as BB
import qualified Papa.Bytestring.Data.ByteString.Char8 as BC
import qualified Papa.Bytestring.Data.ByteString.Lazy as BL
import qualified Papa.Bytestring.Data.ByteString.Lazy.Char8 as BLC
import qualified Papa.Text.Data.Text as T
import qualified Papa.Text.Data.Text.Encoding as TE
import qualified Papa.Text.Data.Text.IO as TIO
import qualified Papa.Text.Data.Text.Lazy as TL
import qualified Papa.Text.Data.Text.Lazy.Builder as TB
import qualified Papa.Text.Data.Text.Lazy.Builder.Int as TBI
import qualified Papa.Text.Data.Text.Lazy.Encoding as TLE
import qualified Papa.Text.Data.Text.Lazy.Read as TLR
import qualified Papa.Text.Data.Text.Read as TR
import Papa.Unsafe.Numeric (Natural)
import qualified System.Directory as D
import qualified System.IO as IO
import System.IO.Error (isDoesNotExistError, isEOFError)
import qualified Prelude as P
-- | The properties of the Text and ByteString modules.
propertiesText :: Group
propertiesText =
Group "text and bytestring" (text <> lazyText <> encoding <> textReads <> builders <> bytestring <> lazyBytestring <> char8 <> lazyChar8 <> bytestringBuilder <> io)
prop :: PropertyName -> Property -> [(PropertyName, Property)]
prop n p =
[(n, p)]
-- Generators: short, over few letters, so that needles are found, with the
-- empty and singleton cases often.
genText :: Gen Text
genText =
Gen.text (Range.linear 0 12) (Gen.element ("abc" :: [Char]))
genUnicode :: Gen Text
genUnicode =
Gen.text (Range.linear 0 30) Gen.unicode
genNeedle :: Gen Text
genNeedle =
Gen.text (Range.linear 0 3) (Gen.element ("abc" :: [Char]))
genLazyText :: Gen STL.Text
genLazyText =
STL.fromChunks <$> Gen.list (Range.linear 0 4) genText
genLazyNeedle :: Gen STL.Text
genLazyNeedle =
STL.fromStrict <$> genNeedle
genBytes :: Gen ByteString
genBytes =
Gen.bytes (Range.linear 0 12)
-- | Lines of bytes, some of them not UTF-8: overlong forms, surrogates,
-- code points above U+10FFFF, truncated sequences, stray continuations.
genLineBytes :: Gen ByteString
genLineBytes =
fmap SB.concat (Gen.list (Range.linear 0 30) (Gen.element (fmap SB.pack [[10], [13, 10], [97], [98, 99], [0xCE, 0xBB], [0xE2, 0x82, 0xAC], [0xF0, 0x9F, 0x98, 0x80], [0xC0, 0xAF], [0xE0, 0x80, 0xAF], [0xED, 0xA0, 0x80], [0xF4, 0x90, 0x80, 0x80], [0xFF], [0x80], [0xBF], [0xCE], [0xE2, 0x82], [0xF0, 0x9F]])))
-- | Every line of a file, read through a handle of the buffer size and
-- encoding, until the end of the file: the text, or which failure.
readLines :: FilePath -> Maybe Int -> IO.TextEncoding -> Int -> (IO.Handle -> IO (Either Text Text)) -> IO [Either Text Text]
readLines p n e size readLine =
IO.withFile p IO.ReadMode (\h -> IO.hSetEncoding h e *> IO.hSetBuffering h (IO.BlockBuffering n) *> go h (size + 2))
where
go _ 0 = pure []
go h k =
readLine h >>= \r -> bool (fmap (r :) (go h (k - 1 :: Int))) (pure [r]) (r == Left (pack "eof"))
-- | A line by TextIO.hGetLine: the text, or which failure.
papaLine :: IO.Handle -> IO (Either Text Text)
papaLine h =
fmap (over _Left (\f -> bool (bool (pack "other") (pack "eof") (anyOf _IOException isEOFError f)) (pack "decode") (has _DecodeFailed f))) (runExceptT (TIO.hGetLine h))
-- | A line by Data.ByteString.Char8's hGetLine, then decodeUtf8': the text,
-- or which failure.
referenceLine :: IO.Handle -> IO (Either Text Text)
referenceLine h =
fmap (either (Left . bool (pack "other") (pack "eof") . isEOFError) (set _Left (pack "decode") . STE.decodeUtf8')) (E.try (SC.hGetLine h) :: IO (Either IOException ByteString))
genLazyBytes :: Gen SL.ByteString
genLazyBytes =
SL.fromChunks <$> Gen.list (Range.linear 0 4) genBytes
genChar :: Gen Char
genChar =
Gen.frequency [(9, Gen.latin1), (1, Gen.element ("\256\1000\1114111" :: [Char]))]
genString :: Gen [Char]
genString =
Gen.string (Range.linear 0 12) genChar
genInteger :: Range.Range Integer -> Gen Integer
genInteger =
Gen.integral
genIndex :: Gen Int
genIndex =
Gen.int (Range.linear (-3) 15)
genWordFn :: Gen (Fn (Int, Int) Word8)
genWordFn =
fn (Gen.word8 Range.linearBounded)
-- | A generated function of two bytes (hedgehog-fn has no Arg Word8).
wordFn :: Fn (Int, Int) Word8 -> Word8 -> Word8 -> Word8
wordFn f a b =
apply f (intCast a, intCast b)
-- | The text, or bytes, of a total function that agrees with the partial
-- one: 'Nothing' where it throws.
agreesBy :: (Eq b, Show b, NFData b) => (a -> b) -> Maybe a -> a -> PropertyT IO ()
agreesBy f total partial =
agrees (fmap f total) (f partial)
-- Papa.Text.Data.Text
text :: [(PropertyName, Property)]
text =
prop "Text.head agrees with Data.Text" (property (do t <- forAll genText; agrees (T.head t) (ST.head t)))
<> prop "Text.last agrees with Data.Text" (property (do t <- forAll genText; agrees (T.last t) (ST.last t)))
<> prop "Text.tail agrees with Data.Text" (property (do t <- forAll genText; agrees (T.tail t) (ST.tail t)))
<> prop "Text.init agrees with Data.Text" (property (do t <- forAll genText; agrees (T.init t) (ST.init t)))
<> prop "Text.foldl1 agrees with Data.Text" (property (do t <- forAll genText; agrees (T.foldl1 max t) (ST.foldl1 max t)))
<> prop "Text.foldl1' agrees with Data.Text" (property (do t <- forAll genText; agrees (T.foldl1' min t) (ST.foldl1' min t)))
<> prop "Text.foldr1 agrees with Data.Text" (property (do t <- forAll genText; agrees (T.foldr1 const t) (ST.foldr1 const t)))
<> prop "Text.maximum agrees with Data.Text" (property (do t <- forAll genText; agrees (T.maximum t) (ST.maximum t)))
<> prop "Text.minimum agrees with Data.Text" (property (do t <- forAll genText; agrees (T.minimum t) (ST.minimum t)))
<> prop "Text.index agrees with Data.Text" (property (do t <- forAll genText; i <- forAll genIndex; agrees (T.index t i) (ST.index t i)))
<> prop "Text.splitOn agrees with Data.Text" (property (do p <- forAll genNeedle; t <- forAll genText; agrees (T.splitOn p t) (ST.splitOn p t)))
<> prop "Text.breakOn agrees with Data.Text" (property (do p <- forAll genNeedle; t <- forAll genText; agrees (T.breakOn p t) (ST.breakOn p t)))
<> prop "Text.breakOnEnd agrees with Data.Text" (property (do p <- forAll genNeedle; t <- forAll genText; agrees (T.breakOnEnd p t) (ST.breakOnEnd p t)))
<> prop "Text.breakOnAll agrees with Data.Text" (property (do p <- forAll genNeedle; t <- forAll genText; agrees (T.breakOnAll p t) (ST.breakOnAll p t)))
<> prop "Text.count agrees with Data.Text" (property (do p <- forAll genNeedle; t <- forAll genText; agrees (T.count p t) (ST.count p t)))
<> prop "Text.replace agrees with Data.Text" (property (do p <- forAll genNeedle; r <- forAll genNeedle; t <- forAll genText; agrees (T.replace p r t) (ST.replace p r t)))
<> prop "display is pack . show" (property (do n <- forAll (Gen.int Range.linearBounded); display n === ST.pack (P.show n)))
-- Papa.Text.Data.Text.Lazy
lazyText :: [(PropertyName, Property)]
lazyText =
prop "LazyText.head agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; agrees (TL.head t) (STL.head t)))
<> prop "LazyText.last agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; agrees (TL.last t) (STL.last t)))
<> prop "LazyText.tail agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; agrees (TL.tail t) (STL.tail t)))
<> prop "LazyText.init agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; agrees (TL.init t) (STL.init t)))
<> prop "LazyText.foldl1 agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; agrees (TL.foldl1 max t) (STL.foldl1 max t)))
<> prop "LazyText.foldl1' agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; agrees (TL.foldl1' min t) (STL.foldl1' min t)))
<> prop "LazyText.foldr1 agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; agrees (TL.foldr1 const t) (STL.foldr1 const t)))
<> prop "LazyText.maximum agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; agrees (TL.maximum t) (STL.maximum t)))
<> prop "LazyText.minimum agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; agrees (TL.minimum t) (STL.minimum t)))
<> prop "LazyText.cycle agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; agreesBy (STL.take 20) (TL.cycle t) (STL.cycle t)))
<> prop "LazyText.index agrees with Data.Text.Lazy" (property (do t <- forAll genLazyText; i <- forAll genIndex; agrees (TL.index t (intCast i)) (STL.index t (intCast i))))
<> prop "LazyText.splitOn agrees with Data.Text.Lazy" (property (do p <- forAll genLazyNeedle; t <- forAll genLazyText; agrees (TL.splitOn p t) (STL.splitOn p t)))
<> prop "LazyText.breakOn agrees with Data.Text.Lazy" (property (do p <- forAll genLazyNeedle; t <- forAll genLazyText; agrees (TL.breakOn p t) (STL.breakOn p t)))
<> prop "LazyText.breakOnEnd agrees with Data.Text.Lazy" (property (do p <- forAll genLazyNeedle; t <- forAll genLazyText; agrees (TL.breakOnEnd p t) (STL.breakOnEnd p t)))
<> prop "LazyText.breakOnAll agrees with Data.Text.Lazy" (property (do p <- forAll genLazyNeedle; t <- forAll genLazyText; agrees (TL.breakOnAll p t) (STL.breakOnAll p t)))
<> prop "LazyText.count agrees with Data.Text.Lazy" (property (do p <- forAll genLazyNeedle; t <- forAll genLazyText; agrees (TL.count p t) (STL.count p t)))
<> prop "LazyText.replace agrees with Data.Text.Lazy" (property (do p <- forAll genLazyNeedle; r <- forAll genLazyNeedle; t <- forAll genLazyText; agrees (TL.replace p r t) (STL.replace p r t)))
-- Papa.Text.Data.Text.Encoding and Papa.Text.Data.Text.Lazy.Encoding
encoding :: [(PropertyName, Property)]
encoding =
prop "Encoding.decodeUtf8 agrees with Data.Text.Encoding" (property (do b <- forAll genEncoded; agreesBy id (preview _Right (TE.decodeUtf8 b)) (STE.decodeUtf8 b)))
<> prop "Encoding.decodeASCII agrees with Data.Text.Encoding" (property (do b <- forAll genEncoded; agrees (TE.decodeASCII b) (STE.decodeASCII b)))
<> prop "Encoding.decodeUtf16LE agrees with Data.Text.Encoding" (property (do b <- forAll genUtf16; agrees (preview _Right (TE.decodeUtf16LE b)) (STE.decodeUtf16LE b)))
<> prop "Encoding.decodeUtf16BE agrees with Data.Text.Encoding" (property (do b <- forAll genUtf16; agrees (preview _Right (TE.decodeUtf16BE b)) (STE.decodeUtf16BE b)))
<> prop "Encoding.decodeUtf32LE agrees with Data.Text.Encoding" (property (do b <- forAll genUtf32; agrees (preview _Right (TE.decodeUtf32LE b)) (STE.decodeUtf32LE b)))
<> prop "Encoding.decodeUtf32BE agrees with Data.Text.Encoding" (property (do b <- forAll genUtf32; agrees (preview _Right (TE.decodeUtf32BE b)) (STE.decodeUtf32BE b)))
<> prop "Encoding.decodeUtf16LE . encodeUtf16LE == Right" (property (do t <- forAll genUnicode; TE.decodeUtf16LE (TE.encodeUtf16LE t) === Right t))
<> prop "Encoding.decodeUtf32BE . encodeUtf32BE == Right" (property (do t <- forAll genUnicode; TE.decodeUtf32BE (TE.encodeUtf32BE t) === Right t))
<> prop "LazyEncoding.decodeUtf8 agrees with Data.Text.Lazy.Encoding" (property (do b <- forAll (SL.fromStrict <$> genEncoded); agrees (preview _Right (TLE.decodeUtf8 b)) (STLE.decodeUtf8 b)))
<> prop "LazyEncoding.decodeASCII agrees with Data.Text.Lazy.Encoding" (property (do b <- forAll (SL.fromStrict <$> genEncoded); agrees (TLE.decodeASCII b) (STLE.decodeASCII b)))
<> prop "LazyEncoding.decodeUtf16LE agrees with Data.Text.Lazy.Encoding" (property (do b <- forAll (SL.fromStrict <$> genUtf16); agrees (preview _Right (TLE.decodeUtf16LE b)) (STLE.decodeUtf16LE b)))
<> prop "LazyEncoding.decodeUtf16BE agrees with Data.Text.Lazy.Encoding" (property (do b <- forAll (SL.fromStrict <$> genUtf16); agrees (preview _Right (TLE.decodeUtf16BE b)) (STLE.decodeUtf16BE b)))
<> prop "LazyEncoding.decodeUtf32LE agrees with Data.Text.Lazy.Encoding" (property (do b <- forAll (SL.fromStrict <$> genUtf32); agrees (preview _Right (TLE.decodeUtf32LE b)) (STLE.decodeUtf32LE b)))
<> prop "LazyEncoding.decodeUtf32BE agrees with Data.Text.Lazy.Encoding" (property (do b <- forAll (SL.fromStrict <$> genUtf32); agrees (preview _Right (TLE.decodeUtf32BE b)) (STLE.decodeUtf32BE b)))
-- | Bytes that are UTF-8 (and often ASCII), or are not.
genEncoded :: Gen ByteString
genEncoded =
Gen.choice [STE.encodeUtf8 <$> genUnicode, STE.encodeUtf8 <$> genText, genBytes]
-- | Bytes that are UTF-16, or are not.
genUtf16 :: Gen ByteString
genUtf16 =
Gen.choice [STE.encodeUtf16LE <$> genUnicode, STE.encodeUtf16BE <$> genUnicode, genBytes]
-- | Bytes that are UTF-32, or are not.
genUtf32 :: Gen ByteString
genUtf32 =
Gen.choice [STE.encodeUtf32LE <$> genUnicode, STE.encodeUtf32BE <$> genUnicode, genBytes]
-- Papa.Text.Data.Text.Read and Papa.Text.Data.Text.Lazy.Read
textReads :: [(PropertyName, Property)]
textReads =
prop "Read.decimal: Data.Text.Read's at Integer, where it fits a Word8" (property (do n <- forAll (genInteger (Range.linear 0 600)); s <- forAll genText; let { t = display n <> s }; (TR.decimal t :: Either [Char] (Word8, Text)) === fmap (first P.fromInteger) (bool (Left "number out of range") (STR.decimal t) (n <= 255))))
<> prop "Read.hexadecimal: Data.Text.Read's at Integer" (property (do n <- forAll (genInteger (Range.linear 0 100000)); (TR.hexadecimal (display n) :: Either [Char] (Integer, Text)) === STR.hexadecimal (display n)))
<> prop "Read.signed decimal: Data.Text.Read's at Int" (property (do n <- forAll (Gen.int (Range.linear (-1000) 1000)); (TR.signed TR.decimal (display n) :: Either [Char] (Int, Text)) === STR.signed STR.decimal (display n)))
<> prop "Read.signed decimal: out of range at Word8 and Natural for a negative number" (property (do n <- forAll (genInteger (Range.linear (-1000) (-1))); (TR.signed TR.decimal (display n) :: Either [Char] (Word8, Text)) === Left "number out of range"; (TR.signed TR.decimal (display n) :: Either [Char] (Natural, Text)) === Left "number out of range"))
<> prop "Read.decimal: every Int round-trips" (property (do n <- forAll (Gen.int Range.linearBounded); TR.signed TR.decimal (display n) === Right (n, "")))
<> prop "Read.decimal: maxBound + 1 does not fit an Int" (property ((TR.decimal (display (toInteger (maxBound :: Int) + 1)) :: Either [Char] (Int, Text)) === Left "number out of range"))
<> prop "LazyRead.decimal and hexadecimal: Data.Text.Read's on the strict text" (property (do t <- forAll (Gen.choice [genText, display <$> Gen.int Range.linearBounded, ("0x" <>) <$> genText, ("0x" <>) . display <$> Gen.int (Range.linear 0 1000)]); (TLR.decimal (STL.fromStrict t) :: Either [Char] (Int, STL.Text)) === fmap (second STL.fromStrict) (TR.decimal t); (TLR.hexadecimal (STL.fromStrict t) :: Either [Char] (Int, STL.Text)) === fmap (second STL.fromStrict) (TR.hexadecimal t)))
<> prop "LazyRead.signed decimal: Data.Text.Lazy.Read's at Integer" (property (do n <- forAll (genInteger (Range.linear (-1000) 1000)); let { t = STL.fromStrict (display n) }; (TLR.signed TLR.decimal t :: Either [Char] (Integer, STL.Text)) === Right (n, "")))
-- Papa.Text.Data.Text.Lazy.Builder and .Int
builders :: [(PropertyName, Property)]
builders =
prop "Builder.toLazyTextWith: the text, at any size" (property (do n <- forAll (Gen.int (Range.linear (-10) 10)); t <- forAll genUnicode; TB.toLazyTextWith n (STB.fromText t) === STL.fromStrict t))
<> prop "BuilderInt.hexadecimal agrees with Data.Text.Lazy.Builder.Int" (property (do n <- forAll (Gen.int (Range.linear (-1000) 1000)); agreesBy STB.toLazyText (TBI.hexadecimal n) (STBI.hexadecimal n)))
-- Papa.Bytestring.Data.ByteString
bytestring :: [(PropertyName, Property)]
bytestring =
prop "ByteString.head agrees with Data.ByteString" (property (do b <- forAll genBytes; agrees (B.head b) (SB.head b)))
<> prop "ByteString.last agrees with Data.ByteString" (property (do b <- forAll genBytes; agrees (B.last b) (SB.last b)))
<> prop "ByteString.tail agrees with Data.ByteString" (property (do b <- forAll genBytes; agrees (B.tail b) (SB.tail b)))
<> prop "ByteString.init agrees with Data.ByteString" (property (do b <- forAll genBytes; agrees (B.init b) (SB.init b)))
<> prop "ByteString.foldl1 agrees with Data.ByteString" (property (do b <- forAll genBytes; f <- forAll genWordFn; agrees (B.foldl1 (wordFn f) b) (SB.foldl1 (wordFn f) b)))
<> prop "ByteString.foldl1' agrees with Data.ByteString" (property (do b <- forAll genBytes; f <- forAll genWordFn; agrees (B.foldl1' (wordFn f) b) (SB.foldl1' (wordFn f) b)))
<> prop "ByteString.foldr1 agrees with Data.ByteString" (property (do b <- forAll genBytes; f <- forAll genWordFn; agrees (B.foldr1 (wordFn f) b) (SB.foldr1 (wordFn f) b)))
<> prop "ByteString.foldr1' agrees with Data.ByteString" (property (do b <- forAll genBytes; f <- forAll genWordFn; agrees (B.foldr1' (wordFn f) b) (SB.foldr1' (wordFn f) b)))
<> prop "ByteString.maximum agrees with Data.ByteString" (property (do b <- forAll genBytes; agrees (B.maximum b) (SB.maximum b)))
<> prop "ByteString.minimum agrees with Data.ByteString" (property (do b <- forAll genBytes; agrees (B.minimum b) (SB.minimum b)))
<> prop "ByteString.index agrees with Data.ByteString" (property (do b <- forAll genBytes; i <- forAll genIndex; agrees (B.index b i) (SB.index b i)))
-- Papa.Bytestring.Data.ByteString.Lazy
lazyBytestring :: [(PropertyName, Property)]
lazyBytestring =
prop "LazyByteString.head agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; agrees (BL.head b) (SL.head b)))
<> prop "LazyByteString.last agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; agrees (BL.last b) (SL.last b)))
<> prop "LazyByteString.tail agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; agrees (BL.tail b) (SL.tail b)))
<> prop "LazyByteString.init agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; agrees (BL.init b) (SL.init b)))
<> prop "LazyByteString.foldl1 agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; f <- forAll genWordFn; agrees (BL.foldl1 (wordFn f) b) (SL.foldl1 (wordFn f) b)))
<> prop "LazyByteString.foldl1' agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; f <- forAll genWordFn; agrees (BL.foldl1' (wordFn f) b) (SL.foldl1' (wordFn f) b)))
<> prop "LazyByteString.foldr1 agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; f <- forAll genWordFn; agrees (BL.foldr1 (wordFn f) b) (SL.foldr1 (wordFn f) b)))
<> prop "LazyByteString.foldr1' agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; f <- forAll genWordFn; agrees (BL.foldr1' (wordFn f) b) (SL.foldr1' (wordFn f) b)))
<> prop "LazyByteString.maximum agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; agrees (BL.maximum b) (SL.maximum b)))
<> prop "LazyByteString.minimum agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; agrees (BL.minimum b) (SL.minimum b)))
<> prop "LazyByteString.cycle agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; agreesBy (SL.take 20) (BL.cycle b) (SL.cycle b)))
<> prop "LazyByteString.index agrees with Data.ByteString.Lazy" (property (do b <- forAll genLazyBytes; i <- forAll genIndex; agrees (BL.index b (intCast i)) (SL.index b (intCast i))))
-- Papa.Bytestring.Data.ByteString.Char8
char8 :: [(PropertyName, Property)]
char8 =
prop "Char8.head agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; agrees (BC.head b) (SC.head b)))
<> prop "Char8.last agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; agrees (BC.last b) (SC.last b)))
<> prop "Char8.tail agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; agrees (BC.tail b) (SC.tail b)))
<> prop "Char8.init agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; agrees (BC.init b) (SC.init b)))
<> prop "Char8.foldl1 agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; agrees (BC.foldl1 max b) (SC.foldl1 max b)))
<> prop "Char8.foldl1' agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; agrees (BC.foldl1' min b) (SC.foldl1' min b)))
<> prop "Char8.foldr1 agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; agrees (BC.foldr1 const b) (SC.foldr1 const b)))
<> prop "Char8.foldr1' agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; agrees (BC.foldr1' max b) (SC.foldr1' max b)))
<> prop "Char8.foldl1: an intermediate character above '\\255' is kept" (property (BC.foldl1 (\_ _ -> '\300') "ab" === Just '\300'))
<> prop "Char8.maximum agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; agrees (BC.maximum b) (SC.maximum b)))
<> prop "Char8.minimum agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; agrees (BC.minimum b) (SC.minimum b)))
<> prop "Char8.index agrees with Data.ByteString.Char8" (property (do b <- forAll genBytes; i <- forAll genIndex; agrees (BC.index b i) (SC.index b i)))
<> prop "Char8.pack: Data.ByteString.Char8's where every character fits a byte" (property (do s <- forAll genString; BC.pack s === (SC.pack s <$ guard (all fits s))))
<> prop "Char8.pack . unpack == Just" (property (do b <- forAll genBytes; BC.pack (BC.unpack b) === Just b))
<> prop "Char8.singleton: Data.ByteString.Char8's where the character fits a byte" (property (do c <- forAll genChar; BC.singleton c === (SC.singleton c <$ guard (fits c))))
<> prop "Char8.cons: Data.ByteString.Char8's where the character fits a byte" (property (do c <- forAll genChar; b <- forAll genBytes; BC.cons c b === (SC.cons c b <$ guard (fits c))))
<> prop "Char8.snoc: Data.ByteString.Char8's where the character fits a byte" (property (do c <- forAll genChar; b <- forAll genBytes; BC.snoc b c === (SC.snoc b c <$ guard (fits c))))
<> prop "Char8.intersperse: Data.ByteString.Char8's where the character fits a byte" (property (do c <- forAll genChar; b <- forAll genBytes; BC.intersperse c b === (SC.intersperse c b <$ guard (fits c))))
<> prop "Char8.replicate: Data.ByteString.Char8's where the character fits a byte" (property (do n <- forAll genIndex; c <- forAll genChar; BC.replicate n c === (SC.replicate n c <$ guard (fits c))))
<> prop "Char8.map: Data.ByteString.Char8's where every result fits a byte" (property (do c <- forAll genChar; b <- forAll genBytes; let { f x = bool x c (x == 'a') }; BC.map f b === (SC.map f b <$ guard (SC.all (fits . f) b))))
<> prop "Char8.unfoldr: pack of the unfolded string" (property (do s <- forAll genString; BC.unfoldr uncons s === BC.pack s))
<> prop "Char8.unzip: the packed firsts and seconds" (property (do s <- forAll genString; t <- forAll genString; BC.unzip (zip s t) === ((SC.pack (fmap fst (zip s t)), SC.pack (fmap snd (zip s t))) <$ guard (all (fits . fst) (zip s t) && all (fits . snd) (zip s t)))))
<> prop "Char8.elem: the list's" (property (do c <- forAll genChar; b <- forAll genBytes; BC.elem c b === List.elem c (SC.unpack b)))
<> prop "Char8.notElem: the list's" (property (do c <- forAll genChar; b <- forAll genBytes; BC.notElem c b === List.notElem c (SC.unpack b)))
<> prop "Char8.elemIndex: the list's" (property (do c <- forAll genChar; b <- forAll genBytes; BC.elemIndex c b === List.elemIndex c (SC.unpack b)))
<> prop "Char8.elemIndices: the list's" (property (do c <- forAll genChar; b <- forAll genBytes; BC.elemIndices c b === List.elemIndices c (SC.unpack b)))
<> prop "Char8.elemIndexEnd: the list's last" (property (do c <- forAll genChar; b <- forAll genBytes; BC.elemIndexEnd c b === preview _last (List.elemIndices c (SC.unpack b))))
<> prop "Char8.count: the list's" (property (do c <- forAll genChar; b <- forAll genBytes; BC.count c b === length (List.elemIndices c (SC.unpack b))))
<> prop "Char8.split: splitWith (== c)" (property (do c <- forAll genChar; b <- forAll genBytes; BC.split c b === SC.splitWith (== c) b))
<> prop "Char8.split: Data.ByteString.Char8's where the character fits a byte" (property (do c <- forAll Gen.latin1; b <- forAll genBytes; BC.split c b === SC.split c b))
-- | Whether a character fits a byte.
fits :: Char -> Bool
fits c =
c <= '\255'
-- Papa.Bytestring.Data.ByteString.Lazy.Char8
lazyChar8 :: [(PropertyName, Property)]
lazyChar8 =
prop "LazyChar8.head agrees with Data.ByteString.Lazy.Char8" (property (do b <- forAll genLazyBytes; agrees (BLC.head b) (SLC.head b)))
<> prop "LazyChar8.foldr1 agrees with Data.ByteString.Lazy.Char8" (property (do b <- forAll genLazyBytes; agrees (BLC.foldr1 const b) (SLC.foldr1 const b)))
<> prop "LazyChar8.maximum agrees with Data.ByteString.Lazy.Char8" (property (do b <- forAll genLazyBytes; agrees (BLC.maximum b) (SLC.maximum b)))
<> prop "LazyChar8.index agrees with Data.ByteString.Lazy.Char8" (property (do b <- forAll genLazyBytes; i <- forAll genIndex; agrees (BLC.index b (intCast i)) (SLC.index b (intCast i))))
<> prop "LazyChar8.cycle agrees with Data.ByteString.Lazy.Char8" (property (do b <- forAll genLazyBytes; agreesBy (SLC.take 20) (BLC.cycle b) (SLC.cycle b)))
<> prop "LazyChar8.pack: Data.ByteString.Lazy.Char8's where every character fits a byte" (property (do s <- forAll genString; BLC.pack s === (SLC.pack s <$ guard (all fits s))))
<> prop "LazyChar8.cons': Data.ByteString.Lazy.Char8's where the character fits a byte" (property (do c <- forAll genChar; b <- forAll genLazyBytes; BLC.cons' c b === (SLC.cons' c b <$ guard (fits c))))
<> prop "LazyChar8.repeat: Data.ByteString.Lazy.Char8's where the character fits a byte" (property (do c <- forAll genChar; fmap (SLC.take 5) (BLC.repeat c) === (SLC.take 5 (SLC.repeat c) <$ guard (fits c))))
<> prop "LazyChar8.replicate: Data.ByteString.Lazy.Char8's where the character fits a byte" (property (do n <- forAll genIndex; c <- forAll genChar; BLC.replicate (intCast n) c === (SLC.replicate (intCast n) c <$ guard (fits c))))
<> prop "LazyChar8.elem: the list's" (property (do c <- forAll genChar; b <- forAll genLazyBytes; BLC.elem c b === List.elem c (SLC.unpack b)))
<> prop "LazyChar8.count: the list's" (property (do c <- forAll genChar; b <- forAll genLazyBytes; BLC.count c b === intCast (length (List.elemIndices c (SLC.unpack b)))))
<> prop "LazyChar8.split: splitWith (== c)" (property (do c <- forAll genChar; b <- forAll genLazyBytes; BLC.split c b === SLC.splitWith (== c) b))
-- Papa.Bytestring.Data.ByteString.Builder
bytestringBuilder :: [(PropertyName, Property)]
bytestringBuilder =
prop "Builder.char7: Data.ByteString.Builder's where the character is ASCII" (property (do c <- forAll genChar; fmap SBB.toLazyByteString (BB.char7 c) === (SBB.toLazyByteString (SBB.char7 c) <$ guard (c <= '\127'))))
<> prop "Builder.string7: Data.ByteString.Builder's where every character is ASCII" (property (do s <- forAll genString; fmap SBB.toLazyByteString (BB.string7 s) === (SBB.toLazyByteString (SBB.string7 s) <$ guard (all (<= '\127') s))))
<> prop "Builder.char8: Data.ByteString.Builder's where the character fits a byte" (property (do c <- forAll genChar; fmap SBB.toLazyByteString (BB.char8 c) === (SBB.toLazyByteString (SBB.char8 c) <$ guard (fits c))))
<> prop "Builder.string8: Data.ByteString.Builder's where every character fits a byte" (property (do s <- forAll genString; fmap SBB.toLazyByteString (BB.string8 s) === (SBB.toLazyByteString (SBB.string8 s) <$ guard (all fits s))))
-- I/O, through a temporary file.
-- | Run an action on the path of a new temporary file, and remove it.
withTemp :: (FilePath -> PropertyT IO a) -> PropertyT IO a
withTemp k = do
path <- evalIO (D.getTemporaryDirectory >>= \d -> IO.openTempFile d "papa-test.txt" >>= \(p, h) -> p <$ IO.hClose h)
a <- k path
evalIO (D.removeFile path)
pure a
io :: [(PropertyName, Property)]
io =
prop "TextIO.readFile . TextIO.writeFile == Right" (property (do t <- forAll genUnicode; r <- withTemp (\p -> evalIO (runExceptT (TIO.writeFile p t >> TIO.readFile p))); r === Right t))
<> prop "TextIO.writeFile writes UTF-8" (property (do t <- forAll genUnicode; r <- withTemp (\p -> evalIO (runExceptT (TIO.writeFile p t) >> SB.readFile p)); r === STE.encodeUtf8 t))
<> prop "TextIO.appendFile appends" (property (do t <- forAll genUnicode; u <- forAll genUnicode; r <- withTemp (\p -> evalIO (runExceptT (TIO.writeFile p t >> TIO.appendFile p u >> TIO.readFile p))); r === Right (t <> u)))
<> prop "TextIO.readFile: invalid UTF-8 is a DecodeFailed of the file" (property (do b <- forAll genBytes; (p, r) <- withTemp (\p -> (,) p <$> evalIO (SB.writeFile p b >> runExceptT (TIO.readFile p))); either (\e -> (preview (_DecodeFailed . _1) e, isLeft (STE.decodeUtf8' b)) === (Just (Right p), True)) (\t -> Right t === STE.decodeUtf8' b) r))
<> prop "TextIO.readFile: a missing file is a does-not-exist error" (property (do r <- evalIO (runExceptT (TIO.readFile "/nonexistent/papa/file")); either (has (_IOException . filtered isDoesNotExistError)) (const False) r === True))
<> prop "TextIO.hGetLine: Data.ByteString.Char8's hGetLine, then decodeUtf8', at any buffer size and encoding" (withTests 1000 (property (do b <- forAll genLineBytes; n <- forAll (Gen.element [Nothing, Just 1, Just 2, Just 3, Just 5, Just 8, Just 4096]); e <- forAll (Gen.element [IO.utf8, IO.latin1]); r <- withTemp (\p -> evalIO (SB.writeFile p b *> ((,) <$> readLines p n e (SB.length b) papaLine <*> readLines p n e (SB.length b) referenceLine))); uncurry (===) r)))
<> prop "TextIO.hGetLine: the lines, then an end-of-file error" (property (do ls <- forAll (Gen.list (Range.linear 0 5) (Gen.text (Range.linear 0 8) (Gen.filter (/= '\n') Gen.unicode))); r <- withTemp (\p -> evalIO (runExceptT (TIO.writeFile p (ST.concat (fmap (<> "\n") ls))) >> IO.withFile p IO.ReadMode (\h -> traverse (const (runExceptT (TIO.hGetLine h))) (() : void ls)))); (fmap (preview _Right) r, fmap (either (has (_IOException . filtered isEOFError)) (const False)) (preview _last r)) === (fmap Just ls <> [Nothing], Just True)))
<> prop "TextIO.hGetContents . TextIO.hPutStrLn == Right" (property (do t <- forAll genUnicode; r <- withTemp (\p -> evalIO (IO.withFile p IO.WriteMode (\h -> runExceptT (TIO.hPutStrLn h t)) >> IO.withFile p IO.ReadMode (runExceptT . TIO.hGetContents))); r === Right (t <> "\n")))
<> prop "TextIO.hPrint writes display" (property (do n <- forAll (Gen.int Range.linearBounded); r <- withTemp (\p -> evalIO (IO.withFile p IO.WriteMode (\h -> runExceptT (TIO.hPrint h n)) >> SB.readFile p)); r === STE.encodeUtf8 (display n <> "\n")))
<> prop "ByteString.readFile . ByteString.writeFile == Right" (property (do b <- forAll genBytes; r <- withTemp (\p -> evalIO (runExceptT (B.writeFile p b >> B.readFile p))); r === Right b))
<> prop "ByteString.hGet: a negative size is an error" (property (do r <- withTemp (\p -> evalIO (IO.withFile p IO.ReadMode (\h -> runExceptT (B.hGet h (-1))))); isLeft r === True))
<> prop "LazyByteString.writeFile writes" (property (do b <- forAll genLazyBytes; r <- withTemp (\p -> evalIO (runExceptT (BL.writeFile p b >> BL.appendFile p b) >> SB.readFile p)); r === SL.toStrict (b <> b)))
<> prop "Char8.hPutStrLn writes a newline" (property (do b <- forAll genBytes; r <- withTemp (\p -> evalIO (IO.withFile p IO.WriteMode (\h -> runExceptT (BC.hPutStrLn h b)) >> SB.readFile p)); r === SB.snoc b 10))
<> prop "LazyChar8.writeFile writes" (property (do b <- forAll genLazyBytes; r <- withTemp (\p -> evalIO (runExceptT (BLC.writeFile p b) >> SB.readFile p)); r === SL.toStrict b))
<> prop "Builder.writeFile writes" (property (do b <- forAll genBytes; r <- withTemp (\p -> evalIO (runExceptT (BB.writeFile p (SBB.byteString b)) >> SB.readFile p)); r === b))
<> prop "Builder.hPutBuilder: a closed handle is an error" (property (do r <- withTemp (\p -> evalIO (IO.openFile p IO.WriteMode >>= \h -> IO.hClose h >> runExceptT (BB.hPutBuilder h (SBB.word8 0)))); isLeft r === True))