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

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

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


[Skip to Readme]

Properties

Versions 1.0.0, 1.0.1, 1.0.2, 1.1.0, 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), th-orphans (>=0.13.4 && <0.13.11), 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-09-24T19:29:15Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for th-test-utils-1.1.0

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