Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function privacy and unit testing Haskell

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?

like image 218
Ralph Avatar asked Jan 17 '13 12:01

Ralph


1 Answers

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.

like image 168
shang Avatar answered Oct 08 '22 16:10

shang