hspec2 0.5.0 → 0.5.1
raw patch · 5 files changed
+61/−41 lines, 5 filesdep ~hspec-meta
Dependency ranges changed: hspec-meta
Files
- hspec-discover/src/Config.hs +6/−4
- hspec-discover/src/Run.hs +27/−22
- hspec-discover/test/ConfigSpec.hs +3/−3
- hspec-discover/test/RunSpec.hs +17/−9
- hspec2.cabal +8/−3
hspec-discover/src/Config.hs view
@@ -12,20 +12,22 @@ configNested :: Bool , configFormatter :: Maybe String , configNoMain :: Bool+, configModuleName :: Maybe String } deriving (Eq, Show) defaultConfig :: Config-defaultConfig = Config False Nothing False+defaultConfig = Config False Nothing False Nothing options :: [OptDescr (Config -> Config)] options = [- Option [] ["nested"] (NoArg $ \c -> c {configNested = True}) ""+ Option [] ["nested"] (NoArg $ \c -> c {configNested = True}) "" , Option [] ["formatter"] (ReqArg (\s c -> c {configFormatter = Just s}) "FORMATTER") ""- , Option [] ["no-main"] (NoArg $ \c -> c {configNoMain = True}) ""+ , Option [] ["module-name"] (ReqArg (\s c -> c {configModuleName = Just s}) "NAME") ""+ , Option [] ["no-main"] (NoArg $ \c -> c {configNoMain = True}) "" ] usage :: String -> String-usage prog = "\nUsage: " ++ prog ++ " SRC CUR DST [--formatter=FORMATTER] [--no-main]\n"+usage prog = "\nUsage: " ++ prog ++ " SRC CUR DST [--module-name=NAME]\n" parseConfig :: String -> [String] -> Either String Config parseConfig prog args = case getOpt Permute options args of
hspec-discover/src/Run.hs view
@@ -11,7 +11,8 @@ , findSpecs , getFilesRecursive , driverWithFormatter-, moduleName+, moduleNameFromId+, pathToModule ) where import Control.Monad import Control.Applicative@@ -43,49 +44,53 @@ Left err -> do hPutStrLn stderr err exitFailure- Right c -> do- when (configNested c) (hPutStrLn stderr "hspec-discover: WARNING - The `--nested' flag is deprecated and will be removed in a future release!")+ Right conf -> do+ when (configNested conf) (hPutStrLn stderr "hspec-discover: WARNING - The `--nested' flag is deprecated and will be removed in a future release!") specs <- findSpecs src- writeFile dst (mkSpecModule src c specs)+ writeFile dst (mkSpecModule src conf specs) _ -> do hPutStrLn stderr (usage name) exitFailure mkSpecModule :: FilePath -> Config -> [Spec] -> String-mkSpecModule src c nodes =+mkSpecModule src conf nodes = ( "{-# LINE 1 " . shows src . " #-}\n" . showString "{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}\n"- . showString ("module " ++ module_ ++" where\n")+ . showString ("module " ++ moduleName src conf ++" where\n") . importList nodes . showString "import Test.Hspec.Discover\n"- . maybe driver driverWithFormatter (configFormatter c)+ . maybe driver driverWithFormatter (configFormatter conf)+ . showString "spec :: Spec\n"+ . showString "spec = " . formatSpecs nodes ) "\n" where driver =- case configNoMain c of+ case configNoMain conf of False -> showString "main :: IO ()\n"- . showString "main = hspec $ "- True ->- showString "spec :: Spec\n"- . showString "spec = "- module_ = if configNoMain c then pathToModule src else "Main"- pathToModule f = let- fileName = last $ splitDirectories f- m:ms = takeWhile (/='.') fileName- in- toUpper m:ms+ . showString "main = hspec spec\n"+ True -> "" +moduleName :: FilePath -> Config -> String+moduleName src conf = fromMaybe (if configNoMain conf then pathToModule src else "Main") (configModuleName conf) +-- | Derive module name from specified path.+pathToModule :: FilePath -> String+pathToModule f = toUpper m:ms+ where+ fileName = last $ splitDirectories f+ m:ms = takeWhile (/='.') fileName+ driverWithFormatter :: String -> ShowS driverWithFormatter f =- showString "import qualified " . showString (moduleName f) . showString "\n"+ showString "import qualified " . showString (moduleNameFromId f) . showString "\n" . showString "main :: IO ()\n"- . showString "main = hspecWithFormatter " . showString f . showString " $ "+ . showString "main = hspecWithFormatter " . showString f . showString " spec\n" -moduleName :: String -> String-moduleName = reverse . dropWhile (== '.') . dropWhile (/= '.') . reverse+-- | Return module name of a fully qualified identifier.+moduleNameFromId :: String -> String+moduleNameFromId = reverse . dropWhile (== '.') . dropWhile (/= '.') . reverse -- | Generate imports for a list of specs. importList :: [Spec] -> ShowS
hspec-discover/test/ConfigSpec.hs view
@@ -25,21 +25,21 @@ parse ["--foo"] `shouldBe` (Left . unlines) [ "hspec-discover: unrecognized option `--foo'" , ""- , "Usage: hspec-discover SRC CUR DST [--formatter=FORMATTER] [--no-main]"+ , "Usage: hspec-discover SRC CUR DST [--module-name=NAME]" ] it "returns error message on unexpected argument" $ do parse ["foo"] `shouldBe` (Left . unlines) [ "hspec-discover: unexpected argument `foo'" , ""- , "Usage: hspec-discover SRC CUR DST [--formatter=FORMATTER] [--no-main]"+ , "Usage: hspec-discover SRC CUR DST [--module-name=NAME]" ] it "returns error message on --formatter=<fmt> with --no-main" $ do parse ["--no-main", "--formatter=foo"] `shouldBe` (Left . unlines) [ "hspec-discover: option `--formatter=<fmt>' does not make sense with `--no-main'" , ""- , "Usage: hspec-discover SRC CUR DST [--formatter=FORMATTER] [--no-main]"+ , "Usage: hspec-discover SRC CUR DST [--module-name=NAME]" ] context "when option is given multiple times" $ do
hspec-discover/test/RunSpec.hs view
@@ -6,7 +6,7 @@ import System.IO import System.Directory import System.FilePath-import Data.List (intercalate, sort)+import Data.List (sort) import Run hiding (Spec) import qualified Run@@ -36,8 +36,10 @@ , "import qualified FooSpec" , "import Test.Hspec.Discover" , "main :: IO ()"- , "main = hspec $" ++ unwords [- " postProcessSpec \"hspec-discover/test-data/nested-spec/Foo/Bar/BazSpec.hs\" (describe \"Foo.Bar.Baz\" Foo.Bar.BazSpec.spec)"+ , "main = hspec spec"+ , "spec :: Spec"+ , "spec = " ++ unwords [+ "postProcessSpec \"hspec-discover/test-data/nested-spec/Foo/Bar/BazSpec.hs\" (describe \"Foo.Bar.Baz\" Foo.Bar.BazSpec.spec)" , ">> postProcessSpec \"hspec-discover/test-data/nested-spec/Foo/BarSpec.hs\" (describe \"Foo.Bar\" Foo.BarSpec.spec)" , ">> postProcessSpec \"hspec-discover/test-data/nested-spec/FooSpec.hs\" (describe \"Foo\" FooSpec.spec)" ]@@ -51,9 +53,15 @@ , "module Main where" , "import Test.Hspec.Discover" , "main :: IO ()"- , "main = hspec $ return ()"+ , "main = hspec spec"+ , "spec :: Spec"+ , "spec = return ()" ] + describe "pathToModule" $ do+ it "derives module name from a given path" $ do+ pathToModule "test/Spec.hs" `shouldBe` "Spec"+ describe "getFilesRecursive" $ do it "recursively returns all file entries of a given directory" $ do getFilesRecursive "hspec-discover/test-data" `shouldReturn` sort [@@ -91,15 +99,15 @@ describe "driverWithFormatter" $ do it "generates a test driver that uses a custom formatter" $ do- driverWithFormatter "Some.Module.formatter" "" `shouldBe` intercalate "\n" [+ driverWithFormatter "Some.Module.formatter" "" `shouldBe` unlines [ "import qualified Some.Module" , "main :: IO ()"- , "main = hspecWithFormatter Some.Module.formatter $ "+ , "main = hspecWithFormatter Some.Module.formatter spec" ] - describe "moduleName" $ do- it "returns the module name of an fully qualified identifier" $ do- moduleName "Some.Module.someId" `shouldBe` "Some.Module"+ describe "moduleNameFromId" $ do+ it "returns the module name of a fully qualified identifier" $ do+ moduleNameFromId "Some.Module.someId" `shouldBe` "Some.Module" describe "importList" $ do it "generates imports for a list of specs" $ do
hspec2.cabal view
@@ -1,5 +1,5 @@ name: hspec2-version: 0.5.0+version: 0.5.1 license: MIT license-file: LICENSE copyright: (c) 2011-2014 Simon Hengel,@@ -7,7 +7,7 @@ (c) 2011 Greg Weber maintainer: Simon Hengel <sol@typeful.net> build-type: Simple-cabal-version: >= 1.8+cabal-version: >= 1.10 category: Testing stability: experimental bug-reports: https://github.com/hspec/hspec/issues@@ -70,6 +70,7 @@ Test.Hspec.Runner.Tree Test.Hspec.Formatters.Internal Test.Hspec.Timer+ default-language: Haskell2010 executable hspec-discover ghc-options:@@ -85,6 +86,7 @@ base == 4.* , filepath , directory+ default-language: Haskell2010 test-suite spec type:@@ -128,11 +130,12 @@ , hspec-expectations , async - , hspec-meta == 1.12.0+ , hspec-meta == 1.12.1 , process , directory , stringbuilder , ghc-paths+ default-language: Haskell2010 test-suite example type:@@ -147,6 +150,7 @@ base == 4.* , hspec2 , QuickCheck+ default-language: Haskell2010 test-suite hspec-discover-spec type:@@ -166,3 +170,4 @@ , filepath , directory , hspec-meta+ default-language: Haskell2010