Hi,
In order to learn Haskell, my plan is to do some of the problems in the Project Euler.
So I created a Cabal project and I want to do unit tests to validate the problems with HUnit in order to have a structure to work with.
This is the file structure :
haskell
├── cabal.project.local
├── cabal.project.local~
├── project-euler.cabal
├── Setup.hs
├── src
│ └── Problem_1.hs
└── test
└── Tests.hs
- First question : as I want to do this for multiple languages, the root folder is named
haskell
. Is it an issue to have a cabal project that is named differently than the parent folder?
So I set up tests like this in the project-euler.cabal
file :
Test-Suite test
type: exitcode-stdio-1.0
ghc-options: -Wall
hs-source-dirs: test
main-is: Tests.hs
build-depends: base, HUnit
default-language: Haskell2010
And in my Test.hs file :
import Test.HUnit
test1 :: Test
test1 = TestCase (assertEqual "test" 0 1)
tests :: Test
tests = TestList [TestLabel "test1" test1]
main :: IO Counts
main = do
runTestTT tests
- Second question : if I run
cabal test
, it says :
Build profile: -w ghc-8.8.3 -O1
In order, the following will be built (use -v for more details):
- project-euler-0.1.0.0 (test:test) (ephemeral targets)
Preprocessing test suite 'test' for project-euler-0.1.0.0..
Building test suite 'test' for project-euler-0.1.0.0..
Running 1 test suites...
Test suite test: RUNNING...
Test suite test: PASS
Test suite logged to:
/home/josephhenry/git/project-euler/src/haskell/dist-newstyle/build/x86_64-linux/ghc-8.8.3/project-euler-0.1.0.0/t/test/test/project-euler-0.1.0.0-test.log
1 of 1 test suites (1 of 1 test cases) passed.
but the test1 should not pass, what is wrong here?
If I now run cabal run test
, it’s working fine :
Build profile: -w ghc-8.8.3 -O1
In order, the following will be built (use -v for more details):
- project-euler-0.1.0.0 (test:test) (additional components to build)
Preprocessing test suite 'test' for project-euler-0.1.0.0..
Building test suite 'test' for project-euler-0.1.0.0..
### Failure in: 0:test1
test/Tests.hs:4
test
expected: 0
but got: 1
Cases: 1 Tried: 1 Errors: 0 Failures: 1
So what is the difference here?
- Last question : In my
src
folder, I will have multiple.hs
files, one for each problem with one function to test each time like :
-- src/Problem_1.hs
problem_1 = sum [x | x <- init [0..1000], (x `mod` 3 == 0) || (x `mod` 5 == 0)]
Now how can I import it inside my Test.hs
file? Is it the best way to do this or should I put a test file for each of the problems?
Any suggestions is greatly appreciated!