Skip to content

Instantly share code, notes, and snippets.

@thoferon
Created August 30, 2013 21:28
Show Gist options
  • Save thoferon/6394445 to your computer and use it in GitHub Desktop.
Save thoferon/6394445 to your computer and use it in GitHub Desktop.
So, your PM is talking about Cucumber... Just an idea
import Control.Monad
import Control.Monad.Identity
import Control.Monad.Trans
import Control.Monad.Writer.Lazy
data Step
= Action String
| Assertion Bool String
deriving (Show, Eq)
type Steps = [Step]
type PickleT m a = WriterT Steps m a
type Pickle a = PickleT Identity a
action :: Monad m => String -> PickleT m ()
action msg = tell [Action msg]
assert :: Monad m => String -> Bool -> PickleT m ()
assert msg pred = tell [Assertion pred msg]
runPickleT :: PickleT m a -> m (a, Steps)
runPickleT = runWriterT
readSourceCode :: PickleT IO String
readSourceCode = do
action "I read the source code"
liftIO $ readFile "Pickle.hs"
countLines :: String -> PickleT IO Int
countLines code = do
action "I count the number of line"
return . length $ lines code
assertMore15Lines :: Int -> PickleT IO ()
assertMore15Lines n = assert "There is more than 15 lines" $ n > 15
assertMore100Lines :: Int -> PickleT IO ()
assertMore100Lines n = assert "There is more than 100 lines" $ n > 100
test :: PickleT IO ()
test = do
code <- readSourceCode
count <- countLines code
assertMore15Lines count
assertMore100Lines count
main :: IO ()
main = do
(_, steps) <- runPickleT test
forM_ steps $ \step -> case step of
Action msg -> putStrLn $ " . " ++ msg
Assertion res msg -> if res
then putStrLn $ " + " ++ msg
else putStrLn $ "--- " ++ msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment