hspec 0.9.0 → 0.9.1
raw patch · 9 files changed
+47/−41 lines, 9 filessetup-changed
Files
- Setup.lhs +1/−3
- Specs.hs +6/−4
- Test/Hspec.hs +5/−5
- Test/Hspec/Core.hs +11/−9
- Test/Hspec/Formatters.hs +1/−1
- Test/Hspec/Monadic.hs +18/−15
- Test/Hspec/QuickCheck.hs +1/−2
- Test/Hspec/Runner.hs +3/−1
- hspec.cabal +1/−1
Setup.lhs view
@@ -1,6 +1,5 @@ #!/usr/bin/runhaskell > module Main where-> import System > import Distribution.Simple > import Distribution.Simple.Setup > import Distribution.PackageDescription@@ -12,8 +11,7 @@ > > main :: IO () > main = defaultMainWithHooks hooks-> where hooks = simpleUserHooks { runTests = \ _ _ _ _ -> runspecs,-> preSDist = \ _ _ -> runspecs >> return emptyHookedBuildInfo }+> where hooks = simpleUserHooks { preSDist = \ _ _ -> runspecs >> return emptyHookedBuildInfo } > > runspecs :: IO () > runspecs = specs >>= hspecX
Specs.hs view
@@ -31,7 +31,9 @@ "Step 1, write descriptions and examples of your desired behavior", "> module Myabs where", ">",- "> specs :: IO Specs",+ "> import Test.Hspec",+ ">",+ "> specs :: Specs", "> specs = describe \"myabs\" [", "> it \"returns the original number when given a positive input\"", "> (myabs 1 == 1),",@@ -139,13 +141,13 @@ it "displays one row for each behavior" (HUnit.assertEqual "" 29 (length report)), - it "displays a row for successfull examples"- (any (==" - pass") report),+ it "displays a row for each successfull, failed, or pending example"+ (any (==" - pass") report && any (==" - fail 1 FAILED [1]") report), it "displays a detailed list of failed examples" (any (=="1) Example fail 1 FAILED") report), - it "displays a '#' and an additional message for pending examples"+ it "displays a '#' with an additional message for pending examples" (any (==" # pending message") report ), it "summarizes the time it takes to finish"
Test/Hspec.hs view
@@ -30,7 +30,7 @@ -- > formatPhoneNumber :: String -> String -- > formatPhoneNumber number = undefined ----- The "describe" function takes a list of behaviors and examples bound together with the "it" function+-- The 'describe' function takes a list of behaviors and examples bound together with the 'it' function -- -- > mySpecs = describe "unformatPhoneNumber" [ --@@ -39,19 +39,19 @@ -- > it "removes dashes, spaces, and parenthesies" -- > (unformatPhoneNumber "(555) 555-1234" == "5555551234"), ----- The "pending" function marks a behavior as pending an example. The example doesn't count as failing.+-- The 'pending' function marks a behavior as pending an example. The example doesn't count as failing. -- -- > it "handles non-US phone numbers" -- > (pending "need to look up how other cultures format phone numbers"), ----- An HUnit "Test" can act as a behavior's example. (must import @Test.Hspec.HUnit@)+-- An HUnit 'Test' can act as a behavior's example. (must import @Test.Hspec.HUnit@) -- -- > it "removes the \"ext\" prefix of the extension" -- > (TestCase $ let expected = "5555551234135" -- > actual = unformatPhoneNumber "(555) 555-1234 ext 135" -- > in assertEqual "remove extension" expected actual), ----- An @IO()@ action is treated like an HUnit "TestCase". (must import @Test.Hspec.HUnit@)+-- An @IO()@ action is treated like an HUnit 'TestCase'. (must import @Test.Hspec.HUnit@) -- -- > it "converts letters to numbers" -- > (do@@ -59,7 +59,7 @@ -- > let actual = unformatPhoneNumber "NUMBERS" -- > assertEqual "letters to numbers" expected actual), ----- The "property" function allows a QuickCheck property to act as an example. (must import @Test.Hspec.HUnit@)+-- The 'property' function allows a QuickCheck property to act as an example. (must import @Test.Hspec.HUnit@) -- -- > it "can add and remove formatting without changing the number" -- > (property $ forAll phoneNumber $
Test/Hspec/Core.hs view
@@ -55,16 +55,18 @@ descriptions :: [[Spec]] -> [Spec] descriptions = concat --- | Evaluate a Result. Any exceptions (undefined, etc.) are treated as failures.-safely :: Result -> IO Result-safely f = Control.Exception.catch ok failed- where ok = silence $ f `seq` return f- failed e = return $ Fail (show (e :: SomeException)) - evaluateSpec :: Spec -> IO Spec evaluateSpec (UnevaluatedSpec name' requirement' example' depth') = do- r <- evaluateExample example'+ r <- evaluateExample example' `catches` [+ -- Re-throw AsyncException, otherwise execution will not terminate on+ -- SIGINT (ctrl-c). All AsyncExceptions are re-thrown (not just+ -- UserInterrupt) because all of them indicate severe conditions and+ -- should not occur during normal test runs.+ Handler (\e -> throw (e :: AsyncException)),++ Handler (\e -> return $ Fail (show (e :: SomeException)))+ ] return $ Spec name' requirement' r depth' evaluateSpec spec = return spec @@ -84,10 +86,10 @@ evaluateExample :: a -> IO Result instance Example Bool where- evaluateExample bool = safely $ if bool then Success else Fail ""+ evaluateExample bool = evaluateExample $ if bool then Success else Fail "" instance Example Result where- evaluateExample result' = safely result'+ evaluateExample result' = silence $ result' `seq` return result' -- | An existentially quantified @Example@. This way they can be mixed within the same set of Specs data AnyExample = forall a. Example a => AnyExample a
Test/Hspec/Formatters.hs view
@@ -1,4 +1,4 @@--- | This module contains formaatters that take a set of specs and write to a given handle.+-- | This module contains formatters that take a set of specs and write to a given handle. -- They follow a structure similar to RSpec formatters. -- module Test.Hspec.Formatters (
Test/Hspec/Monadic.hs view
@@ -1,17 +1,17 @@-+{-# LANGUAGE GeneralizedNewtypeDeriving #-} -- | This module contains the runners that take a set of specs, specified in a monadic style, evaluate their examples, and -- report to a given handle. -- -- The three functions you'll use the most are 'hspec', 'describe', and 'it'. Here is an -- example of functions that format and unformat phone numbers and the specs for them. ----- > import Test.Hspec+-- > import Test.Hspec.Monadic -- > import Test.Hspec.QuickCheck -- > import Test.Hspec.HUnit -- > import Test.QuickCheck hiding (property) -- > import Test.HUnit -- >--- > main = hspecX $ do+-- > main = hspec mySpecs -- -- Since the specs are often used to tell you what to implement, it's best to start with -- undefined functions. Once we have some specs, then you can implement each behavior@@ -23,28 +23,28 @@ -- > formatPhoneNumber :: String -> String -- > formatPhoneNumber number = undefined ----- The "describe" function takes a list of behaviors and examples bound together with the "it" function+-- The 'describe' function takes a list of behaviors and examples bound together with the 'it' function ----- > describe "unformatPhoneNumber" $ do+-- > mySpecs = describe "unformatPhoneNumber" $ do -- -- A boolean expression can act as a behavior's example. -- -- > it "removes dashes, spaces, and parenthesies" -- > (unformatPhoneNumber "(555) 555-1234" == "5555551234") ----- The "pending" function marks a behavior as pending an example. The example doesn't count as failing.+-- The 'pending' function marks a behavior as pending an example. The example doesn't count as failing. -- -- > it "handles non-US phone numbers" -- > (pending "need to look up how other cultures format phone numbers") ----- An HUnit "Test" can act as a behavior's example. (must import @Test.Hspec.HUnit@)+-- An HUnit 'Test' can act as a behavior's example. (must import @Test.Hspec.HUnit@) -- -- > it "removes the \"ext\" prefix of the extension" -- > (TestCase $ let expected = "5555551234135" -- > actual = unformatPhoneNumber "(555) 555-1234 ext 135" -- > in assertEqual "remove extension" expected actual) ----- An @IO()@ action is treated like an HUnit "TestCase". (must import @Test.Hspec.HUnit@)+-- An @IO()@ action is treated like an HUnit 'TestCase'. (must import @Test.Hspec.HUnit@) -- -- > it "converts letters to numbers" -- > (do@@ -52,7 +52,7 @@ -- > let actual = unformatPhoneNumber "NUMBERS" -- > assertEqual "letters to numbers" expected actual) ----- The "property" function allows a QuickCheck property to act as an example. (must import @Test.Hspec.HUnit@)+-- The 'property' function allows a QuickCheck property to act as an example. (must import @Test.Hspec.HUnit@) -- -- > it "can add and remove formatting without changing the number" -- > (property $ forAll phoneNumber $@@ -82,8 +82,11 @@ import Control.Monad.Trans.Writer (Writer, execWriter, tell) -type Specs = Writer [Spec] ()+type Specs = SpecM () +newtype SpecM a = SpecM (Writer [Spec] a)+ deriving Monad+ -- | Create a document of the given specs and write it to stdout. hspec :: Specs -> IO [Spec] hspec = Runner.hspec . runSpecM@@ -104,16 +107,16 @@ hHspec h = Runner.hHspec h . runSpecM runSpecM :: Specs -> [Spec]-runSpecM specs = execWriter specs+runSpecM (SpecM specs) = execWriter specs -describe :: String -> Writer [Spec] () -> Specs-describe label action = tell $ Core.describe label [execWriter action]+describe :: String -> Specs -> Specs+describe label action = SpecM . tell $ Core.describe label [runSpecM action] -- | Combine a list of descriptions. (Note that descriptions can also -- be combined with monadic sequencing.) descriptions :: [Specs] -> Specs descriptions = sequence_ -it :: Example v => String -> v -> Writer [Spec] ()-it label action = tell $ Core.it label action+it :: Example v => String -> v -> Specs+it label action = SpecM . tell $ Core.it label action
Test/Hspec/QuickCheck.hs view
@@ -22,7 +22,6 @@ -- just for the prop shortcut import qualified Test.Hspec.Monadic as DSL-import Control.Monad.Trans.Writer (Writer) data QuickCheckProperty a = QuickCheckProperty a @@ -30,7 +29,7 @@ property = QuickCheckProperty -- | Monadic DSL shortcut, use this instead of @it@-prop :: QC.Testable t => String -> t -> Writer [Spec] ()+prop :: QC.Testable t => String -> t -> DSL.Specs prop n p = DSL.it n (QuickCheckProperty p)
Test/Hspec/Runner.hs view
@@ -56,7 +56,9 @@ -- > writeReport filename specs = withFile filename WriteMode (\ h -> hHspec h specs) -- hHspec :: Handle -> Specs -> IO Specs-hHspec h = hHspecWithFormat (specdoc $ h == stdout) h+hHspec h specs = do+ useColor <- hIsTerminalDevice h+ hHspecWithFormat (specdoc useColor) h specs -- | Create a document of the given specs and write it to the given handle. -- THIS IS LIKELY TO CHANGE
hspec.cabal view
@@ -1,5 +1,5 @@ name: hspec-version: 0.9.0+version: 0.9.1 cabal-version: >= 1.8 build-type: Custom license: BSD3