函数隐私和单元测试Haskell [英] Function privacy and unit testing Haskell
问题描述
如果你在模块中导出每个函数,以便单元测试可以访问它们,冒险让其他人调用不应该在公共API中的函数。
我想用 { - #LANGUAGE CPP# - }
,然后用 #ifdef
来包围出口:
{ - #LANGUAGE CPP# - }
模块SomeModule
#ifndef TESTING
(export1
,export2
)
#endif
其中
有没有更好的方法?
通常的约定是将你的模块分成公共和私有部分,即
module SomeModule.Internal其中
- ...导出所有私有方法
然后公共API
module SomeModule where(export1,export2)
import SomeModule.In ternal
然后您可以导入 SomeModule.Internal
在测试和其他关键访问内部实现的地方。
这个想法是,你的库的用户永远不会意外调用私有API,但如果知道他们在做什么(调试等),他们可以使用它。与强制隐藏私有API相比,这大大增加了您的库的可用性。
How do you deal with function visibility and unit testing in Haskell?
If you export every function in a module so that the unit tests have access to them, you risk other people calling functions that should not be in the public API.
I thought of using {-# LANGUAGE CPP #-}
and then surrounding the exports with an #ifdef
:
{-# LANGUAGE CPP #-}
module SomeModule
#ifndef TESTING
( export1
, export2
)
#endif
where
Is there a better way?
The usual convention is to split your module into public and private parts, i.e.
module SomeModule.Internal where
-- ... exports all private methods
and then the public API
module SomeModule where (export1, export2)
import SomeModule.Internal
Then you can import SomeModule.Internal
in tests and other places where its crucial to get access to the internal implementation.
The idea is that the users of your library never accidentally call the private API, but they can use it if the know what they are doing (debugging etc.). This greatly increases the usability of you library compared to forcibly hiding the private API.
这篇关于函数隐私和单元测试Haskell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!