首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用cabal、HTF和HUnit断言的奇怪之处

使用cabal、HTF和HUnit断言的奇怪之处
EN

Stack Overflow用户
提问于 2011-01-16 12:04:25
回答 1查看 263关注 0票数 3

所以我尝试使用HTF来运行一些HUnit风格的断言

代码语言:javascript
复制
% 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时,它似乎工作正常。

代码语言:javascript
复制
% 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构建它时,并不是所有应该失败的测试都会失败。

代码语言:javascript
复制
% 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)

哪里出了问题,我该如何修复它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-16 15:49:46

这是某些GHC版本中的一个错误,与优化器在某些情况下删除抛出IO异常有关。如果启用了优化,这在HUnit代码中非常常见。而cabal在默认情况下设置了-O1,这会启用该错误。

升级到GHC 7可以解决这个问题(在图书馆跟上它之前,并不推荐这样做,IE是为GHC 7制作的haskell平台版本)。

您还可以将-O0放在与测试可执行文件相关的.cabal文件节的编译器选项中。这就是我对我的测试代码所做的,直到我准备将我的项目转移到GHC7。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4703722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档