设置 Haskell 个项目和 运行 个测试

Set up Haskell Project and run tests

我正在尝试建立一个 Haskell 项目(库),其中包含我可以用来完成 The Haskell Road to Logic, Maths, and Programming 的测试。我想要三个部分:

我已尝试设置项目 here,但收到以下阴谋集团错误:

Resolving dependencies...
Configuring haskell-road-0.1.0.0...
Building haskell-road-0.1.0.0...
Failed to install haskell-road-0.1.0.0
Build log ( /Users/stuart/.cabal/logs/haskell-road-0.1.0.0.log ):
cabal: Entering directory '.'
Configuring haskell-road-0.1.0.0...
Building haskell-road-0.1.0.0...
Preprocessing library haskell-road-0.1.0.0...

src/Chapter1.hs:1:1:
    File name does not match module name:
    Saw: ‘Main’
    Expected: ‘Chapter1’
cabal: Leaving directory '.'
cabal: Error: some packages failed to install:
haskell-road-0.1.0.0 failed during the building phase. The exception was:
ExitFailure 1

我希望能够 运行 $ cabal test 并进行所有测试 运行,并让导入路径正常工作。任何帮助表示赞赏。我认为测试结构可能存在问题,但我很难找到有关实际设置的权威指南。

编辑:更多细节

src/
  Chapter1.hs
  Book/
    GS.hs
    etc....
test/
  Chapter1Test.hs
  MainTestSuite.hs
  TestHelper.hs

haskell-book.hs:

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

-- The name of the package.
name:                haskell-road

-- The package version.  See the Haskell package versioning policy (PVP)
-- for standards guiding when and how versions should be incremented.
-- https://wiki.haskell.org/Package_versioning_policy
-- PVP summary:      +-+------- breaking API changes
--                   | | +----- non-breaking API additions
--                   | | | +--- code changes with no API change
version:             0.1.0.0

-- A short (one-line) description of the package.
-- synopsis:

-- A longer description of the package.
-- description:

-- The license under which the package is released.
license:             MIT

-- The file containing the license text.
license-file:        LICENSE

-- The package author(s).
author:              Stuart Terrett

-- An email address to which users can send suggestions, bug reports, and
-- patches.
maintainer:          shterrett@gmail.com

-- A copyright notice.
-- copyright:

-- category:

build-type:          Simple

-- Extra files to be distributed with the package, such as examples or a
-- README.
extra-source-files:  ChangeLog.md

-- Constraint on the version of Cabal needed to build this package.
cabal-version:       >=1.10


library
  -- Modules exported by the library.
  exposed-modules:     Chapter1, Book.COR, Book.DB, Book.FAIS, Book.FCT, Book.GS, Book.Hierarchy, Book.IAR, Book.Nats, Book.POL, Book.Polynomials, Book.PowerSeries, Book.Query, Book.REL, Book.SetEq, Book.SetOrd, Book.STAL, Book.TAMO, Book.TUOLP, Book.WWN

  -- Modules included in this library but not exported.
  -- other-modules:

  -- LANGUAGE extensions used by modules in this package.
  other-extensions:    FlexibleInstances

  -- Other library packages from which modules are imported.
  build-depends:       base, random >=1.1 && <1.2, HUnit >=1.3 && <1.4

  -- Directories containing source files.
  hs-source-dirs:      src

  -- Base language which the package is written in.
  default-language:    Haskell2010


test-suite haskell-road-tests
  type:              exitcode-stdio-1.0
  hs-source-dirs:    tests, src
  main-is:           MainTestSuite.hs
  build-depends:     base,
                     HUnit,
                     QuickCheck,
                     test-framework,
                     test-framework-hunit,
                     test-framework-quickcheck2

MainTestSuite.hs

import Chapter1Test

exitProperly :: IO Counts -> IO ()
exitProperly m = do
  counts <- m
  exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess

allTests::[Test]
allTests = [Chapter1Test.itRuns]

main :: IO ()
main = exitProperly (runTestTT (TestList allTests))

Cabaldeveloping packages link 那里描述了 cabal 文件内容结构。

通过查看错误消息,您的 haskell library 源文件 Chapter1 似乎以 module Main where 开头。如错误消息所述,它应该包含 module Chapter1 where

库不应该包含 main 而测试可执行文件应该,这就是为什么你在 cabal 文件中声明测试可执行文件带有 main-is

希望对您有所帮助! (我没有查看 github 来源,只查看错误消息。)

所有变化的差异:

http://lpaste.net/5997592404872396800

您需要进行的具体更改:

  1. 在 Chapter1.hs 中确保 module Chapter1 出现在导入语句之前:

    module Chapter1 where
    import ...
    
  2. 在每个 Book 模块中您需要添加前缀 Book. 每个模块语句,例如在 Book/COR.hs:

    change:  module COR
        to:  module Book.COR
    

    此外,任何导入语句也需要 Book. 前缀,即 Book/STAL.hs:

    change:  import DB
        to:  import Book.DB
    

    (将本书的模块留在模块名称 space 的顶层可能更容易。)

  3. 要修复此编译错误:

    src/Book/IAR.hs:131:7:
        No instance for (Foldable t3) arising from a use of ‘foldr’
    

只需将 {-# LANGUAGE NoMonomorphismRestriction #-} 添加到 Book/IAR.hs 的顶部(它应该是第一行。)

  1. 要修复此编译错误:

    src/Book/FAIS.hs:14:4: Parse error in pattern: n + 1
    

    更改:f (n+1) = True : f nf n = True : f (n-1)

    这称为 n+k pattern,有关它的更多信息(以及它被弃用的原因)可在此处获得:What are "n+k patterns" and why are they banned from Haskell 2010?

  2. test-suite 部分你有:

    hs-source-dirs: tests, src
    

    要使用src 目录中的代码,您测试应该依赖haskell-road 库而不是编译源代码。也就是说,在 test-suite 部分使用这些行:

    hs-source-dirs: tests
    build-depends: base, haskell-road, HUnit, ...
    
  3. 文件test/Chapter1Test.hs需要一个模块语句:

    module Chapter1Test where
    

    并修复此 import 语句:

    -import TestHelper.testCase
    +import TestHelper (testCase)
    
  4. 文件 test/MainTestSuite.hs 需要这些导入语句:

    import System.Exit
    import Test.HUnit
    
  5. 文件 test/testHelper.hs 需要重命名为 test/TestHelper.hs 并且还需要这个导入语句:

    import Test.HUnit