How to test my haskell functions

6.3k views Asked by At

I just started with Haskell and tried to do write some tests first. Basically, I want to define some function and than call this function to check the behavior.

add :: Integer -> Integer -> Integer
add a b = a+b

-- Test my function 
add 2 3

If I load that little script in Hugs98, I get the following error:

Syntax error in declaration (unexpected `}', possibly due to bad layout)

If I remove the last line, load the script and then type in "add 2 3" in the hugs interpreter, it works just fine.

So the question is: How can I put calls of my functions in the same script as the function definition? I just want to load the script and be able to check if it does what I expect it to...I don't want to type them in manually all the time.

6

There are 6 answers

1
Dominic Mulligan On BEST ANSWER

Make a top level definition:

add :: Integer -> Integer -> Integer
add a b = a + b

test1 = add 2 3

Then call test1 in your Hugs session.

2
dave4420 On

Others have said how to solve your immediate problem, but for testing you should be using QuickCheck or some other automated testing library.

import Test.QuickCheck
prop_5 = add 2 3 == 5
prop_leftIdentity n = add 0 n == n

Then run quickCheck prop_5 and quickCheck prop_leftIdentity in your Hugs session. QuickCheck can do a lot more than this, but that will get you started.

(Here's a QuickCheck tutorial but it's out of date. Anyone know of one that covers QuickCheck 2?)

1
Chris On

the most beginner friendly way is probably the doctest module. Download it with "cabal install doctest", then put your code into a file "Add.hs" and run "doctest Add.hs" from the command line.

Your code should look like this, the formatting is important:

module Add where

-- | add adds two numbers
--
-- >>> add 2 3
-- 5
-- >>> add 5 0
-- 5
-- >>> add 0 0
-- 0
add :: Integer -> Integer -> Integer
add a b = a+b

HTH Chris

0
Dan Burton On

If you have ghc installed, then the runhaskell command will interpret and run the main function in your file.

add x y = x + y
main = print $ add 2 3

Then on the command line

> runhaskell Add.hs
5

Not sure, but hugs probably has a similar feature to the runhaskell command. Or, if you load the file into the hugs interpreter, you can simply run it by calling main.

0
LeleDumbo On

How can I put calls of my functions in the same script as the function definition? I just want to load the script and be able to check if it does what I expect it to...I don't want to type them in manually all the time.

In short, you can't. Wrap it in a function and call it instead. Your file serves as a valid Haskell module, and having "flying" expression is not a valid way to write it.

You seem to come from a scripting language background, but don't try treating Haskell as one of them.

0
Aiden Cullo On

I was trying to do the same thing and I just made a function that ran through all my test cases (using guards) and returned 1 if they all passed and threw an error if any failed.

test :: Num b => a->b
test x
      | sumALL [1]            /= 1 = error "test failed"
      | sumALL [0,1,2]      /= 3 = error "test failed"
      ...
      | otherwise = 1