简体   繁体   English

与cabal,HTF和HUnit断言的奇怪之处

[英]Weirdness with cabal, HTF, and HUnit assertions

So I'm trying to use HTF to run some HUnit-style assertions 所以我试图用HTF来运行一些HUnit风格的断言

% 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]

And when I use ghc --make , it seems to work correctly. 当我使用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)

But when I use cabal to build it, not all the tests that should fail, fail. 但是当我使用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)

What's going wrong and how can I fix it? 出了什么问题,我该如何解决?

This is a bug in certain versions of GHC, related to the optimizer removing throwing IO exceptions in some cases. 这是某些GHC版本中的错误,与优化程序在某些情况下删除抛出IO异常有关。 It is very common with HUnit code, if optimizations are enabled. 如果启用了优化,则在HUnit代码中非常常见。 And cabal sets -O1 by default, which enables the bug. 并且cabal默认设置为-O1 ,这样可以实现错误。

Upgrading to GHC 7 fixes it (not really recommended until libraries have caught up with it, IE a haskell platform release is made for GHC 7). 升级到GHC 7修复它(在图书馆赶上它之前不是真的推荐,IE为GHC 7制作了一个haskell平台版本)。

You can also put -O0 in the compiler options in the .cabal file stanza related to your test executable. 您还可以将-O0放在与测试可执行文件相关的.cabal文件节中的编译器选项中。 This is what I've done for my test code, until I am ready to move my project to GHC 7. 这是我为我的测试代码所做的,直到我准备将我的项目转移到GHC 7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM