on master help sign in
Fix and generalise I/O write-then-read properties

--HG-- extra : convert_revision : e8ce204659b9204f3ca68837b7ed7b411b3daa2f

commit7ca8b32939158a4fcf5bab9566087ba191caafe3 authorBryan O'Sullivan <bos@serpentine.com> dateFri 27 Aug 2010, 06:00 (16 years ago) parent7132962
1 file changed+35−20
tests/Properties.hs+35−20
tests/Properties.hs+35−20
@@ -12,6 +12,7 @@ import Data.Monoid (Monoid(..))
1212import Data.String (fromString)
1313import Debug.Trace (trace)
1414import Control.Arrow ((***), second)
15+import Control.DeepSeq
1516import Data.Word (Word8, Word16, Word32)
1617import qualified Data.Text as T
1718import qualified Data.Text.IO as T
@@ -582,14 +583,42 @@ t_builderAssociative s1 s2 s3 = TB.toLazyText (b1 `mappend` (b2 `mappend` b3)) =
582583
583584-- Input and output.
584585
585−write_read writer reader locale t = monadicIO $ assert . (==t) =<< run act
586+-- Work around lack of Show instance for TextEncoding.
587+data Encoding = E String TextEncoding
588+
589+instance Show Encoding where show (E n _) = "utf" ++ n
590+
591+instance Arbitrary Encoding where
592+ arbitrary = oneof . map return $
593+ [ E "8" utf8, E "8_bom" utf8_bom, E "16" utf16, E "16le" utf16le,
594+ E "16be" utf16be, E "32" utf32, E "32le" utf32le, E "32be" utf32be ]
595+
596+windowsNewlineMode = NewlineMode { inputNL = CRLF, outputNL = CRLF }
597+
598+instance Show Newline where
599+ show CRLF = "CRLF"
600+ show LF = "LF"
601+
602+instance Show NewlineMode where
603+ show (NewlineMode i o) = "NewlineMode { inputNL = " ++ show i ++
604+ ", outputNL = " ++ show o ++ " }"
605+
606+instance Arbitrary NewlineMode where
607+ arbitrary = oneof . map return $
608+ [ noNewlineTranslation, universalNewlineMode, nativeNewlineMode,
609+ windowsNewlineMode ]
610+
611+write_read writer reader (E _ e) m t = monadicIO $ assert . (==t) =<< run act
586612 where act = withTempFile $ \path h -> do
587− hSetEncoding h locale
613+ hSetEncoding h e
614+ hSetNewlineMode h m
588615 () <- writer h t
589616 hClose h
590617 bracket (openFile path ReadMode) hClose $ \h' -> do
591− hSetEncoding h' locale
592− reader h'
618+ hSetEncoding h' e
619+ hSetNewlineMode h' m
620+ r <- reader h'
621+ r `deepseq` return r
593622
594623t_write_read = write_read T.hPutStr T.hGetContents
595624
@@ -987,22 +1016,8 @@ tests = [
9871016 ],
9881017
9891018 testGroup "input-output" [
990− testProperty "t_write_read_8" $ t_write_read utf8,
991− testProperty "t_write_read_8bom" $ t_write_read utf8_bom,
992− testProperty "t_write_read_16" $ t_write_read utf16,
993− testProperty "t_write_read_16le" $ t_write_read utf16le,
994− testProperty "t_write_read_16be" $ t_write_read utf16be,
995− testProperty "t_write_read_32" $ t_write_read utf32,
996− testProperty "t_write_read_32le" $ t_write_read utf32le,
997− testProperty "t_write_read_32be" $ t_write_read utf32be,
998− testProperty "tl_write_read_8" $ tl_write_read utf8,
999− testProperty "tl_write_read_8bom" $ tl_write_read utf8_bom,
1000− testProperty "tl_write_read_16" $ tl_write_read utf16,
1001− testProperty "tl_write_read_16le" $ tl_write_read utf16le,
1002− testProperty "tl_write_read_16be" $ tl_write_read utf16be,
1003− testProperty "tl_write_read_32" $ tl_write_read utf32,
1004− testProperty "tl_write_read_32le" $ tl_write_read utf32le,
1005− testProperty "tl_write_read_32be" $ tl_write_read utf32be
1019+ testProperty "t_write_read" t_write_read,
1020+ testProperty "tl_write_read" tl_write_read
10061021 ],
10071022
10081023 testGroup "lowlevel" [