Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional (auto-generated) test case names #802

Open
xparq opened this issue Sep 2, 2023 · 1 comment
Open

Optional (auto-generated) test case names #802

xparq opened this issue Sep 2, 2023 · 1 comment

Comments

@xparq
Copy link

xparq commented Sep 2, 2023

Description

Lowering the barrier to writing test cases even further (especially for the initial stages of (or small) projects, which is exactly where writing tests is often "postponed", even though simple, quick early smoke testing could save a lot of wasted effort) conflicts with the need for coming up with nice, unique, descriptive case names (especially when they are already thematically grouped in some test runner file), which could be a hindrance.

It would be nice to start with no name, and then add one later, as things (like terminology) take shape (and if the test case actually turns out to be kept at all, which is often not what happens).

E.g. for compilers, which support __COUNTER__ (i.e. all?), this is what I've done:

#define STRINGIFY_2(a) #a
#define STRINGIFY(a) STRINGIFY_2(a)
#define CONCAT_2(a, b) a##b
#define CONCAT(a, b) CONCAT_2(a, b)
#define FIRST(x, ...) x

#ifndef STD_CONFORMANT_PREPROCESSOR
#error Conformant C++ preprocessor is required for optional test case names!
#endif
#define CASE(...) DOCTEST_TEST_CASE( FIRST(__VA_ARGS__ __VA_OPT__(,) STRINGIFY(CONCAT(test_case_,__COUNTER__))) )

(Failure reports still include line numbers, so identifying nameless cases is not really a problem, especially if you have IDE support for locating the offending lines. And if you land there, you might then actually add a name, if you feel like. The point is you are not forced to.)

(BTW, that "conformant checking" placeholder in the code is not for __COUNTER__ -- that's still not standardized, unfortunately, but for __VA_OPT__, which is standardized, but the default (i.e. legacy) MSVC preprocessor merrily ignores even with -std:c++infinity. (-Zc:preprocessor enables their conformant version, in case anyone was wondering.))

@mitchgrout
Copy link

Unfortunately __VA_OPT__ forces you to C20 / C++20, so that would likely be a no-go. One option would be to add a macro that introduces a unique name into scope for you, like...

#define DOCTEST_TO_STRING0(x) #x
#define DOCTEST_TO_STRING(x) DOCTEST_TO_STRING0(x)

#ifdef __COUNTER__
  #define DOCTEST_UNIQUE_NAME() DOCTEST_TO_STRING(__COUNTER__)
#else
  #define DOCTEST_UNIQUE_NAME() DOCTEST_TO_STRING(__LINE__)
#endif /* __COUNTER__ */
#define UNIQUE_NAME DOCTEST_UNIQUE_NAME

...with its usage being along the lines of...

SCENARIO(UNIQUE_NAME()) { /* Hacking... */ }
SCENARIO(UNIQUE_NAME() * doctest::may_fail) /* Hacking, but it might be a bit flaky for now... */ }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants