.NET在Haskell中的“InternalsVisibleTo”等效项

在 .NET 中,我可以使用以下属性装饰我的程序集:

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

因此,所有标记为“内部”的模块都可以从“MyProject.Test”访问。我可以使用它来对我不想在我的库中公开的功能进行单元测试。

我想知道 Haskell 的世界是否有类似的东西。假设我有一个包含以下 .cabal 文件的库:

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

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

有没有办法从 mylib-test 测试套件中引用“MyLibrary.Utils”?

回答

您可以将其作为内部库的一部分,并依赖于来自公共库和测试套件的内容。它不会在包外提供。

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

或者,无论如何都存在公开模块的强烈约定,但包括Internal在其名称中并在文档中告诉人们您破坏了它是您购买的。

  • @AlojzyLeszcz Be the change you want to see in the world. That's why I use metric exclusively despite living in the US. (That said it's not clear to me that internal libraries are the obviously superior choice. There is something interesting about being given the power to tinker with internals when the situation calls for it -- and there's also something interesting about never having to deal with complaints from folks that depended on internals and got upset when they changed.)

以上是.NET在Haskell中的“InternalsVisibleTo”等效项的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>