module Test.Hspec.Dirstream
(
testFiles
, testFilesIO
, testFilesErr
, testFilesPredicate
, F.extension
, Test.Hspec.Dirstream.hasExtension
) where
import Data.DirStream
import Data.Text (Text)
import qualified Filesystem.Path.CurrentOS as F
import Pipes
import qualified Pipes.Prelude as P
import Pipes.Safe
import System.FilePath
import Test.Hspec
import Test.Hspec.Core.Spec
deriving instance MonadCatch (SpecM a)
deriving instance MonadThrow (SpecM a)
deriving instance MonadMask (SpecM a)
deriving instance MonadIO (SpecM a)
hasExtension :: Text -> F.FilePath -> Bool
hasExtension = flip F.hasExtension
mapS :: (a -> SpecM () ()) -> Proxy () a y' y (SafeT (SpecM ())) r
mapS = P.mapM_ . (lift .)
paths :: MonadSafe m => String -> (F.FilePath -> Bool) -> Producer String m ()
paths dir p = every (childOf path) >-> P.filter p >-> P.map F.encodeString
where path = F.decodeString dir
testFiles :: (Eq a, Show a)
=> FilePath
-> (F.FilePath -> Bool)
-> (String -> Either a String)
-> SpecWith ()
testFiles dir p f = runSafeT $ runEffect $ paths dir p >-> mapS (testFile f)
testFilesIO :: FilePath -> (F.FilePath -> Bool) -> (String -> IO String) -> SpecWith ()
testFilesIO dir p f = runSafeT $ runEffect $ paths dir p >-> mapS (testFileIO f)
testFilesPredicate :: (Show a, Eq a)
=> FilePath
-> (F.FilePath -> Bool)
-> (String -> a)
-> (a -> Bool)
-> SpecWith ()
testFilesPredicate dir p f pr = runSafeT $ runEffect $ paths dir p >-> mapS (testFilePredicate f pr)
testFileIO :: (String -> IO String) -> String -> SpecWith ()
testFileIO fun f = it f $ do
sample <- readFile f
expected <- readFile (replaceExtension f ".out")
fun sample >>= (`shouldBe` expected)
testFilesErr :: (Show b, Eq b) => FilePath -> (F.FilePath -> Bool) -> (String -> Either String b) -> SpecWith ()
testFilesErr dir p f = runSafeT $ runEffect $ paths dir p >-> mapS (testFileErr f)
testFilePredicate :: (Eq a, Show a) => (String -> a) -> (a -> Bool) -> String -> SpecWith ()
testFilePredicate fun p f = it f $ do
sample <- readFile f
fun sample `shouldSatisfy` p
testFileErr :: (Eq b, Show b) => (String -> Either String b) -> String -> SpecWith ()
testFileErr fun f = it f $ do
sample <- readFile f
expected <- readFile (replaceExtension f ".out")
fun sample `shouldBe` Left expected
testFile :: (Eq a, Show a) => (String -> Either a String) -> String -> SpecWith ()
testFile fun f = it f $ do
sample <- readFile f
expected <- readFile (replaceExtension f ".out")
fun sample `shouldBe` Right expected