-1

I am writing a unit test library and I need to log the name of the test function during the assertion, like as follows:

struct my_test_case : public unit_test::test {
    void some_test()
    {
        assert_test(false, "test failed.");
    }
};

When I run the test case, I want to produce an output like:

ASSERTION FAILED (&my_test_case::some_test()): test failed.

I know there are some ways to solve this issue:

  1. Give __FUNCTION__ to assert_true()

  2. Define a macro like ASSERT(a, b) that expands to assert_true(a, b, __FUNCTION__)

  3. Define a macro like TEST to cache the __FUNCTION__ in the test function

struct my_test_case : public unit_test::test { void some_test() { TEST assert_test(false, "test failed."); } };

But these are error-prone and ugly solutions. Are there any other solutions to this problem?

Benji Mizrahi
  • 2,154
  • 2
  • 23
  • 38
  • Once i can take some time i will fix the formating... – Benji Mizrahi Sep 30 '15 at 13:56
  • 2
    You know [Boost Test](http://www.boost.org/doc/libs/release/libs/test/)? It does what you want, and more. Boost is a quasi-standard expansion library for C++. – DevSolar Sep 30 '15 at 14:06
  • 2
    In what way is (2) ugly and error prone? The end user doesn't see anything? Is it that you don't want to use a macro at all? In short, please be more specific about what is wrong with your 3 solutions. In order to have an actual, unique answer, and not a "list of every solution in answers, please", we need better criteria. – Yakk - Adam Nevraumont Sep 30 '15 at 14:08

1 Answers1

0

something like (2) or a variant thereof will probably be the only choice:

__FUNCTION__ is not really a function or run time variable, but only exists at compile time; so, no matter what you do, when you need its value at runtime, you will need to locally save the value.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • I agree that this question is not written clearly ,and the title and the explanation do not match at all. I am going to write a better question and let you know. – Benji Mizrahi Oct 01 '15 at 06:11
  • Why is `__FUNCTION__` not a run time variable? In this link (https://gcc.gnu.org/onlinedocs/gcc/Function-Names.html) it is as if a local static variable `static const char __func__[] = "function-name";` – Benji Mizrahi Oct 01 '15 at 06:26
  • I re-phrased my question: http://stackoverflow.com/questions/32880969/getting-a-function-name-func-from-a-class-t-and-a-pointer-to-member-functi/32882070#32882070 – Benji Mizrahi Oct 01 '15 at 07:55