@@ -1,14 +1,35 @@
1+-- | Miscellaneous testing utilities
3+{-# LANGUAGE ScopedTypeVariables #-}
1module Data.Text.Tests.TestUtils
4module Data.Text.Tests.TestUtils
3− withRedirect
7+ , withRedirect
4 , withTempFile
8 , withTempFile
7−import Control.Exception (bracket, bracket_)
11+import Control.Exception (SomeException, bracket, bracket_, evaluate, try)
8import Control.Monad (when)
12import Control.Monad (when)
13+import Debug.Trace (trace)
9import GHC.IO.Handle.Internals (withHandle)
14import GHC.IO.Handle.Internals (withHandle)
10import System.Directory (removeFile)
15import System.Directory (removeFile)
11−import System.IO
16+import System.IO (Handle, hClose, hFlush, hIsOpen, hIsWritable, openTempFile)
17+import System.IO.Unsafe (unsafePerformIO)
19+-- Ensure that two potentially bottom values (in the sense of crashing
20+-- for some inputs, not looping infinitely) either both crash, or both
21+-- give comparable results for some input.
22+(=^=) :: (Eq a, Show a) => a -> a -> Bool
23+i =^= j = unsafePerformIO $ do
24+ x <- try (evaluate i)
25+ y <- try (evaluate j)
27+ (Left (_ :: SomeException), Left (_ :: SomeException))
29+ (Right a, Right b) -> return (a == b)
30+ e -> trace ("*** Divergence: " ++ show e) return False
32+{-# NOINLINE (=^=) #-}
13withTempFile :: (FilePath -> Handle -> IO a) -> IO a
34withTempFile :: (FilePath -> Handle -> IO a) -> IO a
14withTempFile = bracket (openTempFile "." "crashy.txt") cleanupTemp . uncurry
35withTempFile = bracket (openTempFile "." "crashy.txt") cleanupTemp . uncurry