Cabal测试为Haskell产生错误

时间:2014-09-12 09:49:41

标签: haskell cabal hspec

我的Main.hs文件接收命令行参数:

module Main (toLowerStr, Result(..), grade, main) where

...

grade :: [String] -> String -> String -> (Bool, Result, [Highlight])
    grade dictWords correctAnswer studentAnswer =
            ...

...

main :: IO ()
main = do
        args <- getArgs
        dict <- readFile $ args !! 0
        ...

除非我尝试测试,否则工作正常。我的测试文件是

module Testing where

import Main
import Test.Hspec

main :: IO ()
main = do
        dict <- readFile "dict.txt"
        let dictWords = map toLowerStr $ lines dict
        hspec $ do
        describe "Validate passing answers" $ do
                it "Match should be verified exactly" $ do
                        grade dictWords "house" "house"
                              `shouldBe` (True, Perfect, [])

然而,当我跑cabal test时,它仍然给了我

Preprocessing test suite 'tests' for grade-0.1.0.0...
[2 of 2] Compiling Testing          ( src/HSpecTests.hs, dist/build/tests/tests-tmp/Testing.o )
Linking dist/build/tests/tests ...
Running 1 test suites...
Test suite tests: RUNNING...
tests: Prelude.(!!): index too large

Test suite tests: FAIL

由于argsMain.main的调用,我很确定它失败了,因为可执行文件本身工作正常,我看不到!!在其他任何地方使用过。

如何让测试运行?

编辑Main.hs中使用的模式匹配:

main :: IO ()
main = do
        [filename, correctString, studentString] <- getArgs
        ...

现在错误

[1 of 2] Compiling Main             ( src/Main.hs, dist/build/tests/tests-tmp/Main.o )
Linking dist/build/tests/tests ...
Running 1 test suites...
Test suite tests: RUNNING...
tests: user error (Pattern match failure in do expression at src/Main.hs:141:9-48)
Test suite tests: FAIL

编辑2 :我的整个grade.cabal文件

-- Initial grade.cabal generated by cabal init.  For further documentation,
--  see http://haskell.org/cabal/users-guide/

name:                grade
version:             0.1.0.0
-- synopsis:            
-- description:         
license-file:        LICENSE
author:              Amos Ng <amosng@cmu.edu>
maintainer:          Amos Ng <amosng@cmu.edu>
-- copyright:           
category:            Language
build-type:          Simple
cabal-version:       >=1.8

executable grade
  main-is:             Main.hs
  -- other-modules:       
  build-depends:       base, split ==0.2.*
  hs-source-dirs:      src

test-suite tests
  ghc-options:         -Wall
  type: exitcode-stdio-1.0
  main-is:             HSpecTests.hs
  other-modules:       Main
  build-depends:       base, split ==0.2.*, hspec ==1.11.*
  hs-source-dirs:      src

1 个答案:

答案 0 :(得分:1)

GHC将始终使用名为Main的模块中名为main的函数作为入口点。由于HSpecTests.hsTesting模块,而不是Main模块,因此main完全被忽略,而是偏向main模块中的Main 。您应该将您的逻辑从Main模块中删除,将Main模块保留为命令行存根。然后,您需要将其从测试版本中排除,并将模块Testing更改为Main中的模块HSpecTests.hs