th-test-utils

Utility functions for testing Template Haskell code

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

Version on this page:1.0.1@rev:1
LTS Haskell 22.19:1.2.1@rev:3
Stackage Nightly 2024-05-02: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.0.1@sha256:362d763aedad78882b4f48e4e2e98401e3f28547646467496bc31849c3e59374,2166

Module documentation for 1.0.1

th-test-utils

CircleCI Hackage

This package contains utility functions for testing Template Haskell code.

Currently, this package only exposes a single function, tryQ (and derivatives), that allows testing whether a given Template Haskell expression fails.

Usage

-- e.g. $(qConcat ["hello", "world"]) generates "helloworld" at compile time
qConcat :: [String] -> Q Exp
qConcat [] = fail "Cannot concat empty list"
qConcat xs = ...

-- e.g. [numberify| one |] generates `1` at compile time
numberify :: QuasiQuoter
numberify = ...
-- example using tasty-hunit
main :: IO ()
main = defaultMain $ testGroup "my-project"
  [ testCase "qConcat 1" $
      $(tryQ $ qConcat ["hello", "world"]) @?= (Right "helloworld" :: Either String String)
  , testCase "qConcat 2" $
      $(tryQ $ qConcat [])                 @?= (Left "Cannot concat empty list" :: Either String String)
  , testCase "numberify 1" $
      $(tryQ $ quoteExp numberify "one")   @?= (Right 1 :: Either String Int)
  , testCase "numberify 2" $
      $(tryQ $ quoteExp numberify "foo")   @?= (Left "not a number" :: Either String Int)

  -- can also return error message as `Maybe String` or `String` (which errors
  -- if the function doesn't error)
  , testCase "numberify 3" $
      $(tryQErr $ quoteExp numberify "foo") @?= Just "not a number"
  , testCase "numberify 4" $
      $(tryQErr' $ quoteExp numberify "foo") @?= "not a number"
  ]

Changes

Upcoming

1.0.1

  • Support GHC 8.8

1.0.0

Initial release:

  • Add tryQ, tryQErr, tryQErr' functions