设置Haskell项目并运行测试

时间:2016-08-13 03:06:59

标签: haskell cabal

我正在尝试使用可用于处理The Haskell Road to Logic, Maths, and Programming的测试来设置Haskell项目(库)。我想要有三个部分:

  • 本书附带的代码,位于子目录
  • 我在书中为练习写的代码;每章一个文件
  • 我为测试编写的代码;每章一个文件

我尝试过项目设置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))

2 个答案:

答案 0 :(得分:2)

所有变化的差异:

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
    

    (将书的模块放在模块名称空间的顶层可能更容易。)

  3. 修复此编译错误:

    src/Book/IAR.hs:131:7:
        No instance for (Foldable t3) arising from a use of ‘foldr’
    
  4. 只需将{-# LANGUAGE NoMonomorphismRestriction #-}添加到Book/IAR.hs的顶部(它应该是第一行。)

    1. 修复此编译错误:

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

      f (n+1) = True : f n更改为f 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语句:

      import Test.HUnit
      

答案 1 :(得分:0)

Cabal有 此处描述了developing packages link和cabal文件内容结构。

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

在测试可执行文件应该包含的时候,库不应该包含main,这就是你在cabal文件中用main-is说明测试可执行文件的原因。

希望这有帮助! (我没有查看github源代码,只是错误消息。)