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

[ bsd3, library, testing ] [ Propose Tags ] [ Report a vulnerability ]

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


[Skip to Readme]

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, 1.2.2
Change log CHANGELOG.md
Dependencies base (>=4.9 && <5), template-haskell (>=2.16 && <2.24), th-orphans (>=0.13.4 && <0.14), transformers (>=0.5.2 && <0.7) [details]
License BSD-3-Clause
Author Brandon Chinn <brandonchinn178@gmail.com>
Maintainer Brandon Chinn <brandonchinn178@gmail.com>
Category Testing
Home page https://github.com/brandonchinn178/th-test-utils#readme
Bug tracker https://github.com/brandonchinn178/th-test-utils/issues
Source repo head: git clone https://github.com/brandonchinn178/th-test-utils
Uploaded by brandonchinn178 at 2024-12-21T01:58:48Z
Distributions LTSHaskell:1.2.1, NixOS:1.2.1
Downloads 2208 total (25 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-12-21 [all 1 reports]

Readme for th-test-utils-1.2.2

[back to package description]

th-test-utils

GitHub Actions codecov Hackage

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"
  ]