th-test-utils

Utility functions for testing Template Haskell code

https://github.com/LeapYear/th-test-utils#readme

Version on this page:1.2.1@rev:2
LTS Haskell 22.39:1.2.1@rev:3
Stackage Nightly 2024-10-31:1.2.1@rev:3
Latest on Hackage:1.2.1@rev:3

See all snapshots th-test-utils appears in

BSD-3-Clause licensed and maintained by Brandon Chinn
This version can be pinned in stack with:th-test-utils-1.2.1@sha256:2171efd6873bff73abe6f2c93151ab102e08fa1ed157a437b0c7946736f05d6a,2251

Module documentation for 1.2.1

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

Changes

v1.2.1

  • Support GHC 9.4

v1.2.0

  • Drop support for GHC < 8.10

v1.1.1

  • Support GHC 9.2

v1.1.0

  • Rewrite with runTestQ, allowing for both recoverable Q actions and mocked Q actions in IO.

    The previous tryQ' function can be reimplemented as:

    tryQ' :: Q a -> Q (Either String a)
    tryQ' = tryTestQ unmockedState
    

    with the other helpers defined as before, using tryQ'.

v1.0.2

  • Support GHC 8.10

v1.0.1

  • Support GHC 8.8

v1.0.0

Initial release:

  • Add tryQ, tryQErr, tryQErr' functions