使用测试套件对库进行条件编译

时间:2017-10-07 14:38:01

标签: haskell cabal haskell-stack

我正在尝试为haskell项目正确编译我的测试套件,该项目使用CPP扩展来有条件地导入不同的模块。一个模块将输出发送到网络,另一个模块发送到终端进行调试。

这很好用,但是当我尝试设置我的测试套件时,它似乎不尊重.cabal文件中设置的cpp-options,而是始终构建网络版本。

Cabal文件:

name:           server
version:        0.1.0.0
build-type:     Simple
cabal-version:  >= 1.10

library
  hs-source-dirs:
      src
  build-depends:
      base >=4.9 && <=4.11
    , lens
    , random
    , transformers
    , safe
    , uuid-types
    , QuickCheck
    , network
    , bytestring
    , binary
  exposed-modules:
      Betting
      Game
      Input.Network.Input
      Input.Terminal.Input
      Lenses
      Main
      Output.Network.Output
      Output.Terminal.InputMessages
      Output.Terminal.Output
      Output.Terminal.OutputMessages
      RunLocal
      RunNetwork
      Showdown
      Showdown.Best
      Showdown.Ord
      Showdown.Value
      TestStates
      Types
      Utilities.Card
      Utilities.Player
      Utilities.Showdown
      Utilities.Terminal.Output
      Utilities.Types
  other-modules:
      Paths_server
  default-language: Haskell2010

executable local-server
  main-is: Main.hs
  hs-source-dirs:
      src
  cpp-options: -DDEBUG
  build-depends:
      base >=4.9 && <=4.11
    , lens
    , random
    , transformers
    , safe
    , uuid-types
    , QuickCheck
    , server
  other-modules:
      Betting
      Game
      Input.Network.Input
      Input.Terminal.Input
      Lenses
      Output.Network.Output
      Output.Terminal.InputMessages
      Output.Terminal.Output
      Output.Terminal.OutputMessages
      RunLocal
      RunNetwork
      Showdown
      Showdown.Best
      Showdown.Ord
      Showdown.Value
      TestStates
      Types
      Utilities.Card
      Utilities.Player
      Utilities.Showdown
      Utilities.Terminal.Output
      Utilities.Types
  default-language: Haskell2010

executable networked-server
  main-is: Main.hs
  hs-source-dirs:
      src
  build-depends:
      base >=4.9 && <=4.11
    , lens
    , random
    , transformers
    , safe
    , uuid-types
    , QuickCheck
    , server
    , network
    , bytestring
    , binary
  other-modules:
      Betting
      Game
      Input.Network.Input
      Input.Terminal.Input
      Lenses
      Output.Network.Output
      Output.Terminal.InputMessages
      Output.Terminal.Output
      Output.Terminal.OutputMessages
      RunLocal
      RunNetwork
      Showdown
      Showdown.Best
      Showdown.Ord
      Showdown.Value
      TestStates
      Types
      Utilities.Card
      Utilities.Player
      Utilities.Showdown
      Utilities.Terminal.Output
      Utilities.Types
  default-language: Haskell2010

test-suite test-server
  type: exitcode-stdio-1.0
  main-is: Tests.hs
  hs-source-dirs:
      tests
  cpp-options: -DDEBUG
  build-depends:
      base >=4.9 && <=4.11
    , lens
    , random
    , transformers
    , safe
    , uuid-types
    , QuickCheck
    , server
    , poker-eval
  other-modules:
      BettingTests
      CardTests
      PlayerTests
      ShowdownTests
  default-language: Haskell2010

有条件地编译的模块之一的示例:

{-# LANGUAGE CPP #-}

module Utilities.Card where

#ifdef DEBUG
import Output.Terminal.Output (outputPlayerCards)
#else
import Output.Network.Output (outputPlayerCards)
#endif

dealCards :: GameStateT ()
dealCards = do
    updateCards =<< numPlayersT
    outputPlayerCards

在我的测试套件中,尽管有cpp-options:-DDEBUG设置,但总是会调用outputPlayerCards的网络版本。

但是,网络服务器和本地服务器已正确编译,并调用相应的函数。

我以前的方法只有一个可编译的网络或本地标记的可执行文件,但我读到这不是编写测试套件的正确方法,因为我没有链接aginst它,而是建立它直接将其源目录包含在.cabal文件中。

我正在使用堆栈来构建,如果需要可以包含该文件。

1 个答案:

答案 0 :(得分:1)

我会使用cabal标志:当设置为true时,编译库和测试套件的本地调试版本;设置为false时,编译库的网络版本但禁用测试套件。

您还应该将可执行文件的主文件放在与库不同的目录中,这是您提到的不链接库的问题背后的主要原因。