Search code examples
haskelltestinghunit

Proof of concept in Haskell HUnit Testing


I'm trying to create a dummy project in Haskell to test a library.

src/List/Boundary/List/List.hs:

module List.Boundary.List where

import List.Entity.List(List(Nil,Cons))

len :: List a -> Int
len Nil = 0
len (Cons _ xs) = 1 + len xs

hs-collections-cabal:

Cabal-version:      2.4
Name:               hs-collections
Version:            0.1.0.0
Author:             yago
Maintainer:         [email protected]

Library hs-collections-lib
  Exposed-modules: List.Boundary.List
  Other-modules: List.Controller.List,
               List.Entity.List
  Build-depends:    base ^>=4.17.2.0,
  Hs-source-dirs:   src
  Default-language: Haskell2010

Test-suite hs-collections-testsuite
  Type: exitcode-stdio-1.0
  Main-is: ListTest.hs
  Build-depends: base ^>=4.14,
               HUnit ^>=1.6,
               hs-collections-lib
  Hs-source-dirs: test
  Default-language: Haskell2010  

test/ListTest.hs:

module ListTest where

import Test.HUnit
import qualified System.Exit as Exit
 
test1 :: Test
test1 = TestCase (assertEqual "should return 0" 0 (len []))
 
tests :: Test
tests = TestList [TestLabel "test1" test1]
 
main :: IO ()
main = do
  result <- runTestTT tests
  if failures result > 0 then Exit.exitFailure else Exit.exitSuccess

But I'm getting the following error:

Resolving dependencies...
cabal: Could not resolve dependencies:
[__0] trying: hs-collections-0.1.0.0 (user goal)
[__1] rejecting: hs-collections:!test (constraint from config file, command
line flag, or user target requires opposite flag selection)
[__1] trying: hs-collections:*test
[__2] next goal: base (dependency of hs-collections)
[__2] rejecting: base-4.17.2.0/installed-4.17.2.0 (conflict: hs-collections
*test => base^>=4.14)
[__2] rejecting: base-4.21.0.0, base-4.20.0.1, base-4.20.0.0, base-4.19.2.0,
base-4.19.1.0, base-4.19.0.0, base-4.18.2.1, base-4.18.2.0, base-4.18.1.0,
base-4.18.0.0, base-4.17.2.1, base-4.17.2.0, base-4.17.1.0, base-4.17.0.0,
base-4.16.4.0, base-4.16.3.0, base-4.16.2.0, base-4.16.1.0, base-4.16.0.0,
base-4.15.1.0, base-4.15.0.0, base-4.14.3.0, base-4.14.2.0, base-4.14.1.0,
base-4.14.0.0, base-4.13.0.0, base-4.12.0.0, base-4.11.1.0, base-4.11.0.0,
base-4.10.1.0, base-4.10.0.0, base-4.9.1.0, base-4.9.0.0, base-4.8.2.0,
base-4.8.1.0, base-4.8.0.0, base-4.7.0.2, base-4.7.0.1, base-4.7.0.0,
base-4.6.0.1, base-4.6.0.0, base-4.5.1.0, base-4.5.0.0, base-4.4.1.0,
base-4.4.0.0, base-4.3.1.0, base-4.3.0.0, base-4.2.0.2, base-4.2.0.1,
base-4.2.0.0, base-4.1.0.0, base-4.0.0.0, base-3.0.3.2, base-3.0.3.1
(constraint from non-upgradeable package requires installed instance)
[__2] fail (backjumping, conflict set: base, hs-collections,
hs-collections:test)
After searching the rest of the dependency tree exhaustively, these were the
goals I've had most trouble fulfilling: base, hs-collections,
hs-collections:test

I don't know quite well what is going on. I'm trying to create a first template to investigate HUnit, but I'm stuck here.


Solution

  • The base ^>=4.14 constraint means that your test suite should work with a base package with a version >=4.14 but <4.15. But the base version to build your hs-collections-lib has ^>=4.17.2.0, so >=4.17.2.0 and <4.18. There is no way that you can find a version that satisfies both ranges at the same time.

    Harmonize the versions with:

    Test-suite hs-collections-testsuite
      Type: exitcode-stdio-1.0
      Main-is: ListTest.hs
      Build-depends: base ^>=4.17.2.0,
                   HUnit ^>=1.6,
                   hs-collections-lib
      Hs-source-dirs: test
      Default-language: Haskell2010