ANNOUNCE: hspec-0.3.0 (better reporting of failed examples)
hspec version 0.3.0, Behavior Driven Development for Haskell, is now available! Improvements include fixing a base dependancy problem and better reporting of failed examples. Much thanks to Greg Weber for fixes and ideas! http://hackage.haskell.org/package/hspec https://github.com/trystan/hspec As always, any advice, comments, or questions are welcome, Trystan Spangler For those who would like to see an example, running this program:
import Test.Hspec import Test.Hspec.HUnit import Test.Hspec.QuickCheck (property) import Test.HUnit (assertBool, assertEqual)
main = hspec specs
specs = describe "hspec 0.3.0" [ it "still allows a Bool expression to act as an example" (replicate 5 1 == [1,1,1,1,1]),
it "still allows HUnit assertions to act as examples" (assertBool "lame example" True),
it "still allows QuickCheck properties to act as examples" (property $ \ i -> (i::Int) == i + 1 - 1),
it "now shows a list of failed examples" (undefined :: Bool), -- expected to fail
it "now collects better data from failed HUnit examples" (assertEqual "this should fail" 1 2), -- expected to fail
it "now collects better data from failed QuickCheck examples" (property $ \ i -> (i::Int) * 2 - 2 /= i) -- expected to fail ]
should send this to stdout: hspec 0.3.0 - still allows a Bool expression to act as an example - still allows HUnit assertions to act as examples - still allows QuickCheck properties to act as examples x now shows a list of failed examples [1] x now collects better data from failed HUnit examples [2] x now collects better data from failed QuickCheck examples [3] 1) hspec 0.3.0 now shows a list of failed examples FAILED Prelude.undefined 2) hspec 0.3.0 now collects better data from failed HUnit examples FAILED this should fail expected: 1 but got: 2 3) hspec 0.3.0 now collects better data from failed QuickCheck examples FAILED *** Failed! Falsifiable (after 4 tests): 2 Finished in 0.0494 seconds 6 examples, 3 failures
Announcing Silently, a package with two simple functions to run an IO action while preventing it from writing to stdout (or the given handle): silently :: IO a -> IO a hSilently :: Handle -> IO a -> IO a An example in GHCI: Prelude> :m + System.IO.Silently Prelude System.IO.Silently> let printFail s = undefined :: IO() Prelude System.IO.Silently> let test1 = print 1 >> silently (print 2) >> print 3 Prelude System.IO.Silently> let test2 = print 1 >> silently (printFail 2) >> print 3 Prelude System.IO.Silently> test1 1 3 Prelude System.IO.Silently> test2 1 *** Exception: Prelude.undefined Prelude System.IO.Silently> print "stdout still works after exceptions" "stdout still works after exceptions" http://hackage.haskell.org/package/silently https://github.com/trystan/silently Any advice, comments, or questions are welcome, Trystan Spangler
On 7 March 2011 02:28, Trystan Spangler <trystan.s@comcast.net> wrote:
Announcing Silently, a package with two simple functions to run an IO action while preventing it from writing to stdout (or the given handle)
Useful package! I tiny remark about the code: -- | Run an IO action while ignoring all output to stdout. silently :: IO a -> IO a silently = hSilently stdout -- | Run an IO action while ignoring all output to the given handle. hSilently :: Handle -> IO a -> IO a hSilently handle action = do oldHandle <- hDuplicate handle (tmpFile, tmpHandle) <- openTempFile "." "silently" hDuplicateTo tmpHandle handle finally action (hDuplicateTo oldHandle handle >> hClose tmpHandle >> removeFile tmpFile) Think about what happens if an asynchronous exception is thrown to the thread executing hSilently just after opening the temp file. It will abort the computation which causes the temp file to be left on your system and keep the tmpHandle open. You need to use bracket to ensure the temp file we always be closed and deleted. I send you a pull-request. Regards, Bas
Silently has doubled in size with two new functions to capture the output of an IO action: capture :: IO a -> IO (String, a) hCapture :: Handle -> IO a -> IO (String, a) All 4 functions now clean up any temp file even if there's an asynchronous exception but it still uses temp files behind-the-scenes; I'll get to that when I can in a week or so. http://hackage.haskell.org/package/silently https://github.com/trystan/silently A big thanks to everyone who had comments and suggestions! Trystan Spangler
participants (2)
-
Bas van Dijk -
Trystan Spangler