import Test.Torch import Control.Monad.Trans import System.IO (hPutStrLn,stderr) import System.IO.Capture (capture) main = run $ do (out,err) <- liftIO $ capture (putStrLn "foo") [] is out "foo\n" "stdout is captured" ok (null err) "stderr is also captured" (out,err) <- liftIO $ capture (getLine >>= putStrLn >> getLine >>= hPutStrLn stderr) "foo\nbar\n" is out "foo\n" "stdin can be given; stdout is still captured" is err "bar\n" "stdin can be given; stderr is still captured" (_,err) <- liftIO $ capture undefined [] is err "*** Exception: Prelude.undefined\n" "error message in stderr is also captured" (out,err) <- liftIO $ capture (getContents >>= putStr) "foobarbaz\nquux\n" is out "foobarbaz\nquux\n" "lazy io also works; stdout captured" ok (null err) "lazy io also works; stderr captured" (out,err) <- liftIO $ capture (getLine >>= putStrLn >> getLine >>= hPutStrLn stderr) "foo\nbar\n" is out "foo\n" "after lazy-io, stdout is still captured" is err "bar\n" "after lazy-io, stderr is still captured"