mercredi 30 septembre 2015

Getting the function name (__FUNCTION__) from a class name and a pointer to member function

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?

Aucun commentaire:

Enregistrer un commentaire