簡體   English   中英

Haskell-pipes:如何使用drawAll來測試具有MonadSafe約束的生產者?

[英]Haskell-pipes: how to use drawAll to test a producer with a MonadSafe constraint?

我有一個生產者,給定一個路徑,它遍歷文件系統並產生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

如何使用toListM的toListM:

...
import qualified Pipes.Prelude as P
...

files1 :: (MonadIO m, MonadSafe m) => Producer FilePath m ()
files1 = find "." (glob "*.hs" <> regular)

test1 = do
  got <- runSafeT $ runEffect $ P.toListM files1
  shouldBe got ["a.hs", "b.hs", "c.hs"]

-- using `allFiles`:

test2 = do
  prod <- allFiles "."
  got <- runSafeT $ runEffect $ P.toListM prod
  shouldBe got ["a.hs", "b.hs"]

要編寫您的shouldReturnP函數,請shouldReturnP開始:

shouldReturnP1 prod expected = do
  let _ = expected :: [FilePath]
  got <- runSafeT $ P.toListM prod
  shouldBe got expected

並讓ghc告訴您類型是什么,即:

shouldReturnP1
  :: (Eq a, Show a) => Producer a (SafeT IO) () -> [a] -> IO ()

您可以使用以下方法進行測試:

testP1 = shouldReturnP1 files1 ["a.hs", "b.hs", "c.hs"]

對於IO操作版本,請輸入:

shouldReturnP2 action expected = do
  let _ = expected :: [FilePath]
  prod <- action
  paths <- runSafeT $ runEffect $ P.toListM prod
  paths `shouldBe` expected

ghc告訴您類型是:

shouldReturnP2
  :: IO (Producer FilePath (SafeT IO) ()) -> [FilePath] -> IO ()

和一個測試:

testP2 = shouldReturnP2 (allfiles ".") ["a1.hs"]

更新

根據注釋中有關將doesFileExist檢查放入管道的討論:

allfiles2 :: MonadSafe m => FilePath -> Producer FilePath m ()
allfiles2 path = do
  exists <- liftIO $ doesFileExist path
  if exists
    then each [path] >-> P.filter (".hs" `isSuffixOf`)
    else find path (glob "*.hs" <> regular)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM