Search code examples
unit-testinghaskellinternalsvisibleto

.NET's "InternalsVisibleTo" equivalent in Haskell


In .NET I can decorate my assembly with the following attribute:

[<assembly: InternalsVisibleTo("MyProject.Test")>]

Thanks to this, all the modules marked as "internal" are accessible from "MyProject.Test". I can use it e.g. for unit testing the functionality I don't want to expose in my library.

I'm wondering if there's something similar in the Haskell's world. Let's say I have a library with the following .cabal file:

library
  exposed-modules:  MyLibrary.API
  other-modules:    MyLibrary.Utils
  -- ...

test-suite mylib-test
  -- ...
  build-depends:  base,
                  hspec,
                  my-library

Is there a way to refer to "MyLibrary.Utils" from mylib-test test-suite?


Solution

  • You can make it part of an internal library, and depend on that from both the public library and the test suite. It will not be available outside the package.

    library utils
        exposed-modules: MyLibrary.Utils
    
    library
        exposed-modules: MyLibrary.API
        build-depends: utils
    
    test-suite mylib-test
        build-depends: base, hspec, my-library, utils
    

    Alternately, there's a strong convention of exposing the module anyway, but including Internal in its name and telling folks in the documentation that you break it you bought it.