Search code examples
haskelltemplate-haskell

Testing Template Haskell with mkName


I have a function that returns a dynamically-bound Type - in essence, ConT $ mkName "MyType". Of course, the actual function is significantly more complicated, enough so that I'd like to write tests for it, and preferably legible ones. But the following:

import Language.Haskell.TH
import MyTypeModule (MyType)

myFn :: Type    
myFn = ConT $ mkName "MyType"

test = ... $ do
  m <- runQ [t| MyType |]
  myFn `shouldBe` m

Will always fail, since m will resolve to ConT MyTypeModule.MyType rather than to ConT MyType.

Is there a nice way to transform the myFn type to something fully-qualified, or otherwise check that m and myFn are the same (in the current context)?


Solution

  • Wow long time without an answer, but never too late to post answers on the Internet!

    myFn = ConT ''MyType
    

    The ticks are enabled with the -XTemplateHaskell language extension, and are documented here:

    https://hackage.haskell.org/package/template-haskell-2.13.0.0/docs/Language-Haskell-TH-Syntax.html#t:Name