-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Don't use define private public
in test files
#913
Comments
I only added |
(And I like the idea of using a more recent version of Catch. I tried an early 2.0 version, but the performance of the tests was worse.) |
As far as I know, there is no way of doing this with no changes to the header. There are two ways these things are usually solved:
#ifndef UNDER_TEST
#define INTERNAL_BLOCK private
#else
#define INTERNAL_BLOCK public
#endif
class foo {
public:
foo() { ... }
INTERNAL_BLOCK:
bool helper_1(){ ... }
int helper_2() { ... }
...
}; A common argument is also that if a private method needs to be made accessible to be tested, then the class is too complex and should be breaked apart (because a class should be defined by its external api, not implementation details), but I admit that I don't particularly subscribe to that philosophy. |
With the recent Those would remain unexposed by the |
This still would not allow a unit test to check the value of a private member variable. Though such a check may be expressed in terms of the public API in some cases, refactoring the code for the edge cases would be a lot of work. |
#define keyword is an undefined behavior in C++ std. And in gcc, access may change managed function name. I think using friend is a better way. |
Fixes: error nlohmann#913: invalid multibyte character sequence
Fixes: error nlohmann#913: invalid multibyte character sequence
Fixes: error nlohmann#913: invalid multibyte character sequence
Fixes: error nlohmann#913: invalid multibyte character sequence
Bug Report
I wanted to check whether Catch 2.1.0 fixed all perf. regression from Catch Classic on a real world test suite, but using
#define private public
in test files causes compilation errors because of ODR violations.One example:
This is caused by Catch no longer including
<sstream>
everywhere, so an attempt to redefineprivate
topublic
also hits<sstream>
header. It can be temporarily fixed by just force including<sstream>
before including thejson.hpp
header, but it should be noted that doing#define private public
and having it affect any part of the standard libraryin any TU using the standard library is UB.Note: There are also several test files that rely on transitive stdlib includes from Catch and will not compile with newer version.
OS & Compiler: g++ 5.4 under WSL
The text was updated successfully, but these errors were encountered: