因此,我试图使用HTF来运行一些饥饿式的断言

% cat tests/TestDemo.hs
{-# OPTIONS_GHC -Wall -F -pgmF htfpp #-}
module Main where
import Test.Framework
import Test.HUnit.Base ((@?=))
import System.Environment (getArgs)

-- just run some tests
main :: IO ()
main = getArgs >>= flip runTestWithArgs Main.allHTFTests

-- all these tests should fail
test_fail_int1 :: Assertion
test_fail_int1 = (0::Int) @?= (1::Int)

test_fail_bool1 :: Assertion
test_fail_bool1 = True @?= False

test_fail_string1 :: Assertion
test_fail_string1 = "0" @?= "1"

test_fail_int2 :: Assertion
test_fail_int2 = [0::Int] @?= [1::Int]

test_fail_string2 :: Assertion
test_fail_string2 = "true" @?= "false"

test_fail_bool2 :: Assertion
test_fail_bool2 = [True] @?= [False]

当我使用 ghc --make, ,似乎正常工作。

% ghc --make tests/TestDemo.hs
[1 of 1] Compiling Main             ( tests/TestDemo.hs, tests/TestDemo.o )
Linking tests/TestDemo ...
% tests/TestDemoA
...
* Tests:    6
* Passed:   0
* Failures: 6
* Errors:   0

Failures:
  * Main:fail_int1 (tests/TestDemo.hs:9)
  * Main:fail_bool1 (tests/TestDemo.hs:12)
  * Main:fail_string1 (tests/TestDemo.hs:15)
  * Main:fail_int2 (tests/TestDemo.hs:19)
  * Main:fail_string2 (tests/TestDemo.hs:22)
  * Main:fail_bool2 (tests/TestDemo.hs:25)

但是,当我使用Cabal构建它时,并非所有应该失败的测试失败。

% cat Demo.cabal
...
executable test-demo
  build-depends: base >= 4, HUnit, HTF
  main-is: TestDemo.hs
  hs-source-dirs: tests
% cabal configure
Resolving dependencies...
Configuring Demo-0.0.0...
% cabal build
Preprocessing executables for Demo-0.0.0...
Building Demo-0.0.0...
[1 of 1] Compiling Main             ( tests/TestDemo.hs, dist/build/test-demo/test-demo-tmp/Main.o )
Linking dist/build/test-demo/test-demo ...
% dist/build/test-demo/test-demo
...
* Tests:    6
* Passed:   3
* Failures: 3
* Errors:   0

Failures:
  * Main:fail_int2 (tests/TestDemo.hs:23)
  * Main:fail_string2 (tests/TestDemo.hs:26)
  * Main:fail_bool2 (tests/TestDemo.hs:29)

怎么了,我该如何解决?

有帮助吗?

解决方案

在某些版本的GHC中,这是一个错误,与在某些情况下删除投掷IO例外的优化器有关。如果启用了优化,则非常普遍。和阴谋集 -O1 默认情况下,可以启用错误。

升级到GHC 7修复了它(直到图书馆赶上它之前,并不是真正建议使用它,即为GHC 7制作了Haskell平台版本)。

你也可以放 -O0 在与测试可执行文件相关的.cabal文件stanza中的编译器选项中。这是我为测试代码所做的,直到我准备将项目移至GHC 7。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top