简体   繁体   English

如何测试我的 Haskell 函数

[英]How to test my haskell functions

I just started with Haskell and tried to do write some tests first.我刚开始使用 Haskell,并尝试先编写一些测试。 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:如果我在 Hugs98 中加载那个小脚本,我会收到以下错误:

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.如果我删除最后一行,加载脚本,然后在拥抱解释器中输入“add 2 3”,它工作得很好。

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.我只想加载脚本并能够检查它是否符合我的预期......我不想一直手动输入它们。

Others have said how to solve your immediate problem, but for testing you should be using QuickCheck or some other automated testing library .其他人已经说过如何解决您当前的问题,但是对于测试,您应该使用QuickCheck其他一些自动化测试库

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.然后在您的 Hugs 会话中运行quickCheck prop_5quickCheck prop_leftIdentity QuickCheck can do a lot more than this, but that will get you started. QuickCheck 可以做的远不止这些,但这会让您开始。

(Here's a QuickCheck tutorial but it's out of date. Anyone know of one that covers QuickCheck 2?) (这是一个QuickCheck 教程,但它已经过时了。有人知道涵盖 QuickCheck 2 的 教程吗?)

the most beginner friendly way is probably the doctest module.对初学者最友好的方式可能是doctest模块。 Download it with "cabal install doctest", then put your code into a file "Add.hs" and run "doctest Add.hs" from the command line.使用“cabal install doctest”下载它,然后将您的代码放入文件“Add.hs”并从命令行运行“doctest Add.hs”。

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 HTH克里斯

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.然后在您的 Hugs 会话中调用 test1。

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.您的文件用作有效的 Haskell 模块,并且具有“飞行”表达式并不是编写它的有效方式。

You seem to come from a scripting language background, but don't try treating Haskell as one of them.您似乎来自脚本语言背景,但不要尝试将 Haskell 视为其中之一。

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.我试图做同样的事情,我只是做了一个函数,它运行了我所有的测试用例(使用守卫),如果它们都通过则返回 1,如果有任何失败则抛出错误。

test :: Num b => a->b测试 :: Num b => a->b
test x测试 x
| | sumALL [1] /= 1 = error "test failed" sumALL [1] /= 1 = 错误“测试失败”
| | sumALL [0,1,2] /= 3 = error "test failed" sumALL [0,1,2] /= 3 = 错误“测试失败”
... ...
| | otherwise = 1否则 = 1

If you have ghc installed, then the runhaskell command will interpret and run the main function in your file.如果您安装了 ghc,那么runhaskell命令将解释并运行您文件中的main函数。

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.不确定,但拥抱可能具有与runhaskell命令类似的功能。 Or, if you load the file into the hugs interpreter, you can simply run it by calling main .或者,如果将文件加载到 Hugs 解释器中,则只需调用main即可运行它。

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

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