th-test-utils: Utility functions for testing Template Haskell code

[ bsd3, library, testing ] [ Propose Tags ]

Utility functions for testing Template Haskell code, including functions for testing failures in the Q monad.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 1.0.0, 1.0.1, 1.0.2, 1.1.0, 1.1.1, 1.2.0, 1.2.1
Change log CHANGELOG.md
Dependencies base (>=4.9 && <5), template-haskell (>=2.11.1.0 && <2.17), transformers (>=0.5.2 && <0.5.7) [details]
License BSD-3-Clause
Author Brandon Chinn <brandon@leapyear.io>
Maintainer Brandon Chinn <brandon@leapyear.io>
Category Testing
Home page https://github.com/LeapYear/th-test-utils#readme
Bug tracker https://github.com/LeapYear/th-test-utils/issues
Source repo head: git clone https://github.com/LeapYear/th-test-utils
Uploaded by leapyear at 2020-05-18T18:37:53Z
Distributions LTSHaskell:1.2.1, NixOS:1.2.1, Stackage:1.2.1
Downloads 2031 total (27 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2020-05-18 [all 1 reports]

Readme for th-test-utils-1.0.2

[back to package description]

th-test-utils

CircleCI Hackage

This package contains utility functions for testing Template Haskell code.

Currently, this package only exposes a single function, tryQ (and derivatives), that allows testing whether a given Template Haskell expression fails.

Usage

-- e.g. $(qConcat ["hello", "world"]) generates "helloworld" at compile time
qConcat :: [String] -> Q Exp
qConcat [] = fail "Cannot concat empty list"
qConcat xs = ...

-- e.g. [numberify| one |] generates `1` at compile time
numberify :: QuasiQuoter
numberify = ...
-- example using tasty-hunit
main :: IO ()
main = defaultMain $ testGroup "my-project"
  [ testCase "qConcat 1" $
      $(tryQ $ qConcat ["hello", "world"]) @?= (Right "helloworld" :: Either String String)
  , testCase "qConcat 2" $
      $(tryQ $ qConcat [])                 @?= (Left "Cannot concat empty list" :: Either String String)
  , testCase "numberify 1" $
      $(tryQ $ quoteExp numberify "one")   @?= (Right 1 :: Either String Int)
  , testCase "numberify 2" $
      $(tryQ $ quoteExp numberify "foo")   @?= (Left "not a number" :: Either String Int)

  -- can also return error message as `Maybe String` or `String` (which errors
  -- if the function doesn't error)
  , testCase "numberify 3" $
      $(tryQErr $ quoteExp numberify "foo") @?= Just "not a number"
  , testCase "numberify 4" $
      $(tryQErr' $ quoteExp numberify "foo") @?= "not a number"
  ]