-
Notifications
You must be signed in to change notification settings - Fork 139
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
Method of visiting private members in unit test #2666
Comments
After some search, I find that access check is not invoked in explicit template instantiation, and there's already some repo/code available on github which exploit this behavior for non-invasive tests: https://github.com/martong/access_private This behavior is stated in the following paragraph from the C++11 standdard (14.7.2, paragraph 12) "The usual access checking rules do not apply to names used to specify explicit instantiations. [ Note: In |
The tool in https://github.com/martong/access_private is simply one header file, and the usage is non-invasive, i.e., no modification to the origional source file is required. I think if testing private functions is really necessary, this might be something we can introduce (or simply learn the idea & write our own) to our unit test procedure. What are your opinions @baixiaokuang @hongriTianqi ? The following is an example with googletest. PS: testee.h (the class under test)
unit test source file:
|
Private functions have been tested indirectly by testing public functions which call those private: |
Meet trouble when adding unit test on module_io/bessel_basis. Relevant issue #2614 .
Discussed in #2659
Originally posted by kirk0830 June 21, 2023
The writing and debugging unit test of module_io/bessel_basis are still in progress, while thanks to Zuxin we will have a brand new module (module_nao) later, therefore the unit test of bessel_basis may be not quite important if the new module is fully implemented. However, presently, during adding unit tests on private members, the use of
#define private public
will iteratively cause errors like:error: ‘struct std::__cxx11::basic_stringbuf<_CharT, _Traits, _Alloc>::__xfer_bufptr ’ redeclared with different access
446 | struct __xfer_bufptrs
| ^~~~~~
which is, obviously caused by imposing
#define private public
onto<sstream>
(for analysis with more details posted by other coder, see why cannot use #define private public anymore?). Got advice from Tianqi that removing unnecessary dependence of header files on other header files may finally solve this error, but this error pops again and again, fromglobal_function.h
toglobal_variable.h
, thenrealarray.h
,unitcell.h
,vector3.h
andmatrix3.h
.Actually there are also other methods for visiting private variables and functions, but will be more or less invasive, such as gtest/gtest_prod.h::FRIEND_TEST, or the following way:
#ifdef __UNITTEST
#define PRIVATE_SWTICH public
#else
#define PRIVATE_SWTICH private
#endif
and substitute private with PRIVATE_SWTICH.
The use of FRIEND_TEST also requires declaration in private scope of class to test. So these two methods are more or less invasive but has pros that makes the range of variables which will be set as public during unit test controllable. Suggestions provided by ChatGPT4 are pasted here:
Therefore comments and opinions are of need and will be appreciated about method visiting private variables and functions during unit test.
The text was updated successfully, but these errors were encountered: