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

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

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.16 && <2.22), th-orphans (>=0.13.4 && <0.14), transformers (>=0.5.2 && <0.7) [details]
License BSD-3-Clause
Author Brandon Chinn <brandon@leapyear.io>
Maintainer Brandon Chinn <brandon@leapyear.io>
Revised Revision 3 made by brandonchinn178 at 2023-10-10T04:45:19Z
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 2022-11-19T22:13:06Z
Distributions LTSHaskell:1.2.1, NixOS:1.2.1, Stackage:1.2.1
Downloads 2196 total (33 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-11-19 [all 1 reports]

Readme for th-test-utils-1.2.1

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