Skip to content

Instantly share code, notes, and snippets.

@nh2
Created August 3, 2012 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nh2/3244391 to your computer and use it in GitHub Desktop.
Save nh2/3244391 to your computer and use it in GitHub Desktop.
Hspec processes workaround
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
module Main where
import Test.Hspec
import qualified Test.Hspec.Core as Core
import qualified Test.Hspec.Internal as Internal
instance Example (IO Spec) where
evaluateExample ioSpecM = do
specM <- ioSpecM
let coreSpecs = runSpecM specM
go coreSpecs []
where
go [] _ = return Internal.Success
go (coreSpec:rest) reqPath = case coreSpec of
Internal.SpecGroup groupReq xs -> go xs (groupReq:reqPath)
Internal.SpecExample req e -> do
result <- Internal.safeEvaluateExample e
case result of
Core.Success -> go rest reqPath
Core.Pending _ -> go rest reqPath
Core.Fail msg -> return $ Core.Fail failPath
where
failMsg = if msg == "" then "[no fail message]" else msg
failPath = intercalate " -> " (reverse (failMsg:req:reqPath))
main = hspec $ do
describe "process Hspec" $ do
it "does normal pure hspec" True
it "runs process 1" $ do
putStr "some IO here"
return $ it "does step 1" $ do
putStr "some IO here"
return $ it "toes step 1.1" False
it "runs process 2" $ do
putStr "some IO here"
return $ it "does step 1" $ do
putStr "some IO here"
fail "using fail"
return $ it "is done" True
it "runs process 3" $ do
putStr "some IO here"
return $ describe "step 1" $ do
it "does step 1.1" True
it "does step 1.2" $ do
putStr "some IO here"
fail "using fail"
return $ it "is done" True
it "does step 1.3" True
it "runs process 4" $ do
putStr "some IO here"
return $ describe "step 1" $ do
it "does step 1.1" True
it "does step 1.2" $ do
putStr "some IO here"
return $ it "is done" True
it "does step 1.3" True
{- OUTPUT
process Hspec
- does normal pure hspec
- runs process 1 FAILED [1]
- runs process 2 FAILED [2]
- runs process 3 FAILED [3]
- runs process 4
1) process Hspec runs process 1 FAILED
does step 1 -> toes step 1.1 -> [no fail message]
2) process Hspec runs process 2 FAILED
does step 1 -> user error (using fail)
3) process Hspec runs process 3 FAILED
step 1 -> does step 1.2 -> user error (using fail)
Finished in 0.0009 seconds, used 0.0000 seconds of CPU time
5 examples, 3 failures
-}
{- PROBLEMS
- The successful steps can not be reported.
- Missing type safety:
it "does a simple server <-> client interaction" $ do
someVar <- [some IO setup]
return $ it "server receives client request" $ do
r <- someFun someVar
r @?= "request 1"
return $ it "client receives server response" $ do
r <- someFun someVar
r @?= "response 1"
This will ONLY execute the last `return $ it ...`.
The correct way is (but this is not type-enforced!):
it "does a simple server <-> client interaction" $ do
someVar <- [some IO setup]
return $ describe "with the connection set up" $ do
it "server receives client request" $ do
r <- someFun someVar
r @?= "request 1"
it "client receives server response" $ do
r <- someFun someVar
r @?= "response 1"
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment