haskell - hspec failing to import (private) code dependency despite CPP override -


let's have src file so:

{-# language cpp #-} module alphabet ( #ifdef test   alphabet #endif )   alphabet :: [char]   alphabet = "abcdefghijklmnopqrstuvwxyz" 

a .cabal file so:

name:                alphabet version:             0.1.0.0 library   build-depends:       base >=4.8 && <4.9, containers >=0.5 && <0.6, split >=0.2 && <0.3   hs-source-dirs:      src   exposed-modules:     alphabet   default-language:    haskell2010 test-suite alphabet-test   ghc-options:         -wall -werror   cpp-options:         -dtest   default-extensions:  overloadedstrings   type:                exitcode-stdio-1.0   main-is:             spec.hs   hs-source-dirs:      tests   build-depends:       alphabet, base >= 4.8 && < 4.9, containers >= 0.5 && <0.6, split >= 0.2 && < 0.3, hspec, quickcheck   default-language:    haskell2010 

a master test file so:

 {-# options_ghc -f -pgmf hspec-discover #-} 

and test file like:

module aphabetspec (spec)    import test.hspec   import alphabet (alphabet)    spec :: spec   spec =     describe "alphabet.alphabet" $       "returns alphabet" $         alphabet `shouldbe` "abcdefghijklmnopqrstuvwxyz"  running `cabal test`: cabal test preprocessing library alphabet-0.1.0.0... in-place registering alphabet-0.1.0.0... preprocessing test suite 'alphabet-test' alphabet-0.1.0.0... [1 of 1] compiling main             ( tests/alphabetspec.hs, dist/build/alphabet-test/alphabet-test-tmp/alphabetspec.o )  tests/alphabetspec.hs:4:27: module ‘alphabet’ not export ‘alphabet’ 

why cpp not working expected? how can fix it?

why cpp not working expected?

two-step building. since tests depend on library, gets build first. library doesn't have cpp options set, therefore alphabet doesn't exported.

when tests built, library compiled, , alphabet doesn't exported. it's separation of concerns.

how can fix it?

there several tricks work "hidden" (e.g. non-exported) functions. one, can put them .internal module. way, users want use hidden bits can rather easily, casual user doesn't have many tools @ hand.

another way handle drop dependency in test , instead add src directory tests:

 hs-source-dirs:      tests, src 

however, means have rebuild whole library tests, enable using cpp.

a third option not test alphabet, instead observable behaviour of exported functions depend on it. instead of testing alphabet, test filteralpha:

filteralpha :: string -> string filteralpha = filter (`elem` alphabet) 

you have test filteralpha anyway. if there many functions use alphabet, have test notice regression if accidentally change it.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -