我有一个生产者,给定一个路径,它遍历文件系统产生 Haskell 文件的路径。它建立在管道文件之上:
import Pipes
import Pipes.Files
import Pipes.Safe
import qualified Pipes.Prelude as P
import Data.Monoid ((<>))
import Data.List (isSuffixOf)
import System.Directory (doesFileExist)
-- | Starting from a path, generate a sequence of paths corresponding
-- to Haskell files. The fileystem is traversed depth-first.
allFiles :: (MonadIO m, MonadSafe m) => FilePath -> IO (Producer FilePath m ())
allFiles path = do
isFile <- doesFileExist path
if isFile then return $ each [path] >-> P.filter (".hs" `isSuffixOf`)
else return $ find path (glob "*.hs" <> regular)
现在我想用 Hspec 对其进行测试,但我发现很难将生产者转换为列表。MonadSafe m
如果不是因为那个导致很多类型错误的约束,它会更简单。这是我写的:
import Pipes
import Pipes.Safe
import Pipes.Parse
import Test.Hspec
shouldReturnP :: (MonadIO m, MonadSafe m)
=> IO (Producer FilePath m ()) -> [FilePath] -> Expectation
shouldReturnP action res = do
prod <- action
let paths = runSafeT $ evalStateT drawAll prod
paths `shouldBe` res
这是应该如何使用的:
spec :: Spec
spec = do
describe "allFiles" $
it "traverses the filesystem depth-first returning only hs files" $
allFiles ("test" </> "tree") `shouldReturnP`
[ "test" </> "tree" </> "a.hs"
, "test" </> "tree" </> "sub" </> "b.hs"
, "test" </> "tree" </> "sub" </> "c.hs"
, "test" </> "tree" </> "sub2" </> "a.hs"
, "test" </> "tree" </> "sub2" </> "e.hs"
]
编译测试会出现以下错误:
test/Spec.hs:57:47:
Couldn't match type ‘m’ with ‘Pipes.Safe.SafeT []’
‘m’ is a rigid type variable bound by
the type signature for
shouldReturnP :: (MonadIO m, MonadSafe m) =>
IO (Producer FilePath m ()) -> [FilePath] -> Expectation
at test/Spec.hs:53:18
Expected type: Producer FilePath (Pipes.Safe.SafeT []) ()
Actual type: Producer FilePath m ()
Relevant bindings include
prod :: Producer FilePath m () (bound at test/Spec.hs:56:5)
action :: IO (Producer FilePath m ())
(bound at test/Spec.hs:55:15)
shouldReturnP :: IO (Producer FilePath m ())
-> [FilePath] -> Expectation
(bound at test/Spec.hs:55:1)
In the second argument of ‘evalStateT’, namely ‘prod’
In the second argument of ‘($)’, namely ‘evalStateT drawAll prod’
test/Spec.hs:58:22:
Couldn't match type ‘Char’ with ‘[Char]’
Expected type: [[FilePath]]
Actual type: [FilePath]
In the second argument of ‘shouldBe’, namely ‘res’
In a stmt of a 'do' block: paths res