From 7cffdf5f6c88908761f303fba64b7e3f53ff52ac Mon Sep 17 00:00:00 2001 From: Nathan Kot Date: Wed, 10 Jun 2015 03:06:03 +0900 Subject: [PATCH] Generate spec file --- yesod-bin/AddHandler.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/yesod-bin/AddHandler.hs b/yesod-bin/AddHandler.hs index 59da880e..8bbad758 100644 --- a/yesod-bin/AddHandler.hs +++ b/yesod-bin/AddHandler.hs @@ -9,6 +9,7 @@ import Data.Maybe (fromMaybe) import qualified Data.Text as T import qualified Data.Text.IO as TIO import System.Directory (getDirectoryContents, doesFileExist) +import Control.Monad (unless) data RouteError = EmptyRoute | RouteCaseError @@ -78,7 +79,11 @@ addHandlerFiles cabal (name, handlerFile) pattern methods = do modify cabal $ fixCabal name modify "config/routes" $ fixRoutes name pattern methods writeFile handlerFile $ mkHandler name pattern methods + specExists <- doesFileExist specFile + unless specExists $ + writeFile specFile $ mkSpec name pattern methods where + specFile = "test/Handler/" ++ name ++ "Spec.hs" modify fp f = readFile fp >>= writeFile fp . f getCabal :: IO FilePath @@ -142,6 +147,24 @@ fixRoutes name pattern methods fileContents = ] startingCharacter = if "\n" `isSuffixOf` fileContents then "" else "\n" +mkSpec :: String -> String -> String -> String +mkSpec name _ methods = unlines + $ ("module Handler." ++ name ++ "Spec (spec) where") + : "" + : "import TestImport" + : "" + : "spec :: Spec" + : "spec = withApp $ do" + : concatMap go (words methods) + where + go method = + [ "" + , " describe \"" ++ func ++ "\" $ do" + , " error \"Spec not implemented: " ++ func ++ "\"" + , ""] + where + func = concat [map toLower method, name, "R"] + mkHandler :: String -> String -> String -> String mkHandler name pattern methods = unlines $ ("module Handler." ++ name ++ " where")