Unit Testing C++ Code in an Unnamed Namespace
Asked Answered
M

2

19

I am working on a procedural C/C++ project. The public interface consists of 4 functions, each with fairly complex tasks. There are helper functions declared in the same cpp file, in an unnamed namespace. The test framework being used is GTest.

However, some of these helper functions are becoming complex enough to require their own unit testing. Normally, I would refactor these helpers into their own testable units, but the project requirements state everything needs to be in the one cpp, and only the specified functions can be publicly visible.

Is there a way I can unit test the helper functions while minimizing coupling, and following the project requirements as closely as possible?

A possible solution I had was to use a macro to turn the namespace into a named one for testing, and unnamed for production. However, that seemed a bit messier than I would like.

Marvismarwin answered 9/2, 2013 at 7:15 Comment(0)
E
18

Both definitions and declarations in an anonymous namespace are only visible within the same translation unit.

There are two approaches you can take to unit test these private functions.

You can #include the entire .cpp file being tested in your _test.cpp file. (#includeing .cpp files is not a good way to reuse code - you should not do this in production code!)

Perhaps a better approach is to move the private code into a foo::internal namespace, where foo is the namespace your project normally uses, and put the private declarations in a -internal.h file. Your production .cpp files and your tests are allowed to include this internal header, but your clients are not. This way, you can fully test your internal implementation without leaking it to your clients.

Ethiopic answered 9/2, 2013 at 9:11 Comment(3)
For the second method, moving objects to named namespace means it will keep reference-able runtime identity, and it seems voiding the reason of using anonymous namespace - completely prohibiting any access from out of the compilation unit… Also it seems allowing accidental name clash…Robber
Really like your first idea. Wondering if someone will shoot me for doing it.Guanajuato
@Guanajuato if you were working with me, I would :D (metaphorically of course...)Grammalogue
S
-1

Johnsyweb is perfect, but we don't need foo::internal, and only foo is ok.

Seniority answered 16/8, 2023 at 12:50 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewLeanora

© 2022 - 2024 — McMap. All rights reserved.