Question

Je suis en train d'utiliser HTF pour exécuter certaines affirmations de style 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]

Et quand je l'utilise ghc --make, il semble fonctionner correctement.

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

Mais quand je l'utilise cabale pour le construire, pas tous les tests qui échoue, l'échec.

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

Qu'est-ce qui ne va pas et comment puis-je résoudre ce problème?

Était-ce utile?

La solution

Ceci est un bogue dans certaines versions de GHC, liées à l'optimiseur suppression de lancer des exceptions IO dans certains cas. Il est très fréquent avec le code hunit, si les optimisations sont activées. Et ensembles cabale -O1 par défaut, ce qui permet le bogue.

Mise à niveau vers GHC 7 corrige ce problème (pas vraiment recommandé que les bibliothèques ont pris avec elle, IE une version de plate-forme haskell est faite pour GHC 7).

Vous pouvez aussi mettre -O0 dans les options du compilateur dans la strophe de fichier .cabal lié à votre exécutable de test. Voilà ce que je l'ai fait pour mon code de test, jusqu'à ce que je suis prêt à passer mon projet à GHC 7.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top