1

我正在尝试运行 hspec,但出现“无法为 Spec 文件加载接口”的错误。我从github尝试了类似的例子,得到了同样的错误。请建议我哪里出错了......(PS:我可以用堆栈运行它)。

来自 github 的示例(https://github.com/FranklinChen/twenty-four-days2015-of-hackage)我有以下文件:

-- src.HintExample.hs
module HintExample where
import SortWrapper (Sort)
import qualified Language.Haskell.Interpreter as I
import Language.Haskell.Interpreter (OptionVal((:=)))

-- | Dynamically load a 'Sort' implementation from a file.
-- src is needed to pick up our SortWrapper.
-- sort-plugins is a sample user plugins directory
loadSort :: I.MonadInterpreter m =>
        String  -- ^ module name
     -> String  -- ^ function name
     -> m Sort
loadSort moduleName functionName = do
I.set [I.searchPath := ["src", "sort-plugins"]]
I.loadModules [moduleName]
I.setImports [moduleName, "SortWrapper"]
I.interpret (moduleName ++ "." ++ functionName) (I.as :: Sort)

和测试文件为

-- test.HintExampleSpec.hs
module HintExampleSpec where
import SortWrapper (Sort(Sort))
import HintExample (loadSort)
import qualified Language.Haskell.Interpreter as I
import Test.Hspec (Spec, hspec, describe, it, shouldBe)
import Test.Hspec.QuickCheck (prop)

-- | Required for auto-discovery.
spec :: Spec
spec =
describe "hint" $ do
it "dynamically loads a correct polymorphic sort function" $ do
  Right (Sort ourSort) <-
    I.runInterpreter (loadSort "OurSorter" "ourSort")
  ourSort "ebcad" `shouldBe` "abcde"
  ourSort [1 :: Int, 5, 4, 3, 7] `shouldBe` [1, 3, 4, 5, 7]
it "dynamically loads a wrong (only head) sort function" $ do
  Right (Sort onlyHead) <-
    I.runInterpreter (loadSort "OurSorter" "onlyHead")
  onlyHead "ebcad" `shouldBe` "e"
  onlyHead [True, False] `shouldBe` [True]

main :: IO ()
main = hspec spec

测试目录中的规范文件(Spec.hs)为:

{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

运行方式:runhaskell test/Spec.hs

4

0 回答 0