phate / jlm

GNU Lesser General Public License v2.1
49 stars 13 forks source link

Do we want a shorthand for registering tests? #454

Open haved opened 4 months ago

haved commented 4 months ago

When registering tests individually, it currently looks like this:

static int
TestPair()
{
  // Actual test in here
  return 0;
}

JLM_UNIT_TEST_REGISTER("jlm/util/TestHashSet-TestPair", TestPair)

This leads to a lot of repeated "jlm/util/TestHashSet" and "TestPair", and clang format likes to put blank lines between the test and register macro.

I was wondering if we would like to add macros to enable something like


// Once per file
JLM_UNIT_TEST_FILE("jlm/util/TestHashSet")

JLM_UNIT_TEST(TestPair)
{
  // Actual test in here
  return 0;
}

JLM_UNIT_TEST(TestUniquePointer)
{
  // More test here
  return 0;
}

A possible implementation we could add to the bottom of test-registry.hpp would be:

#define JLM_UNIT_TEST_FILE(filename)                    \
  static const std::string & GetJlmUnitTestFileName()   \
  {                                                     \
    static std::string JlmUnitTestFileName{ filename }; \
    return JlmUnitTestFileName;                         \
  }

#define JLM_UNIT_TEST(name)                                          \
  static int name();                                                 \
  JLM_UNIT_TEST_REGISTER(GetJlmUnitTestFileName() + "-" #name, name) \
  static int name()

To avoid static constructor ordering conflicts, the filename must be placed in a function (saying static const std::string X = filename; as a global variable does not work).

phate commented 4 months ago

I am generally in favor of this suggestion. However, @caleridas planned to change the unit test setup up to google test. I am not sure whether this change will help or be detrimental for this. How far along are you with it @caleridas ? Should we do this?

haved commented 4 months ago

Ah, in that case Google tests probably has its own short syntax

caleridas commented 4 months ago

I had started converting to googletest but it is unfinished. I can pick it up to standardize the testing infrastructure, and it uses a syntax like this (plus a couple other useful tools).