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]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees

Candidates

Versions [RSS] 1.0.0, 1.0.1, 1.0.2, 1.1.0, 1.1.1
Change log CHANGELOG.md
Dependencies base (>=4.9 && <5), template-haskell (>=2.11.1.0 && <2.19), th-orphans (>=0.13.4 && <0.14), transformers (>=0.5.2 && <0.6) [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 2021-11-11T21:29:39Z
Distributions LTSHaskell:1.1.1, NixOS:1.1.1, Stackage:1.1.1
Downloads 1557 total (9 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2021-11-11 [all 1 reports]

Readme for th-test-utils-1.1.1

[back to package description]

th-test-utils

CircleCI Hackage codecov

This package implements tryTestQ and related helpers in order to better test Template Haskell code. It supports returning the actual error message that recover doesn't currently return as well as mocking out Q actions, so that you can run Template Haskell code at runtime.

Usage

-- e.g. $(showInfo "Bool") generates a string corresponding
-- to the reify `Info` for `Bool`.
showInfo :: String -> Q Exp
showInfo s = do
  mName <- lookupTypeName s
  case mName of
    Nothing -> fail $ "Unknown type: " ++ s
    Just name -> do
      info <- reify name
      lift $ show info
-- example using tasty-hunit
main :: IO ()
main = defaultMain $ testGroup "my-project"
  [ testCase "showInfo unmocked" $(do
      result1 <- tryTestQ unmockedState $ showInfo "Bool"
      runIO $ isRight result1 @? ("Unexpected error: " ++ show result1)

      result2 <- tryTestQ unmockedState $ showInfo "Foo"
      runIO $ result2 @?= Left "Unknown type: Foo"

      [| return () |]
    )

  , testCase "showInfo mocked success" $ do
      let state = QState
            { mode = MockQ
            , knownNames = [("Bool", ''Bool)]
            , reifyInfo = $(loadNames [''Bool])
            }

      let result1 = tryTestQ state $ showInfo "Bool"
      isRight result1 @? ("Unexpected error: " ++ show result1)

      let result2 = tryTestQ state $ showInfo "Foo"
      result2 @?= Left "Unknown type: Foo"
  ]