Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:02:22+00:00 2026-06-01T13:02:22+00:00

I have a bunch of functions like: method1 , method2 , method3 . For

  • 0

I have a bunch of functions like: method1, method2, method3. For all of them there are HUnit test functions like: testMethod1, testMethod2, testMethod3.

testMethod1 = TestCase $
  assertEqual "testmethod1" ...

testMethod2 = TestCase $
  assertEqual "testmethod2" ...

testMethod3 = TestCase $
  assertEqual "testmethod3" ...

I would like to avoid redundant copying of function’s name as prefix of error
message and call it something like that:

testMethod1 = TestCase $
  assertEqual_ ...

How can it be achieved (any “magic” trick is appreciated)?

So actually question is how can function name be taken inside of it’s definition?


Update.

It’s not actually clear from original question, that I wanna handle that type of situation too:

tProcess = TestCase $ do
  assertEqual "tProcess" testResult $ someTest
  assertEqual "tProcess" anotherTestResult $ anotherTest
  assertEqual "tProcess" resultAgain $ testAgain

Finally I want to write something like that:

tProcess = TestCase $ do
  assertEqual_ testResult $ someTest
  assertEqual_ anotherTestResult $ anotherTest
  assertEqual_ resultAgain $ testAgain
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:02:23+00:00Added an answer on June 1, 2026 at 1:02 pm

    You can’t do this directly (i.e. so that your test case starts with testMethodN = ...), but you can use Template Haskell to get this:

    testCase "testMethod1" [| do
        assertEqual_ a b
        assertEqual_ c d
     |]
    

    This involves writing testCase :: String -> Q Exp -> Q [Dec], a function to turn the name of the test case and a quoted expression into a list of declarations. For instance:

    {-# LANGUAGE TemplateHaskell #-}
        
    import Data.Char
    import Control.Applicative
    import Control.Monad
    import Language.Haskell.TH
    import Data.Generics
    
    assertEqual :: (Eq a) => String -> a -> a -> IO ()
    assertEqual s a b = when (a /= b) . putStrLn $ "Test " ++ s ++ " failed!"
    
    assertEqual_ :: (Eq a) => a -> a -> IO ()
    assertEqual_ = error "assertEqual_ used outside of testCase"
    
    testCase :: String -> Q Exp -> Q [Dec]
    testCase name expr = do
        let lowerName = map toLower name
        e' <- [| assertEqual lowerName |]
        pure <$> valD
            (varP (mkName name))
            (normalB (everywhere (mkT (replaceAssertEqual_ e')) <$> expr))
            []
      where
        replaceAssertEqual_ e' (VarE n) | n == 'assertEqual_ = e'
        replaceAssertEqual_ _ e = e
    

    The basic idea here is to generate a definition of the name given, and replace every occurrence of the variable assertEqual_ in the quoted expression with assertEqual lowerName. Thanks to Template Haskell’s Scrap Your Boilerplate support, we don’t need to traverse the entire AST, just specify a transformation for each Exp node.

    Note that assertEqual_ must be a bound identifier with the correct type, since the quoted expression is typechecked before being passed on to testCase. Additionally, testCase must be defined in a separate module than the one it’s used in, due to GHC’s stage restriction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of functions with signatures like: FUNCTION func1 (par1 IN VARCHAR2,
My case is as follows : I have a bunch of functions and declarations
I have a bunch of generated functions from a supplier's tool are required to
I have a bunch of overloaded functions that operate on certain data types such
I have an mxml file with a bunch of functions in the script tag,
I have a bunch of functions (methods of a class actually) and I'm writing
Suppose I have a bunch of Clojure data structures, all of the same type
I have a bunch of javascript functions which depend on some server-side constants (such
So I want to have a bunch of functions that receive a state and
I have a simple Rails scaffolded application, and would like to add a bunch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.