Link-Time Dependency Injection for Unit Tests

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Link-Time Dependency Injection for Unit Tests

Joshua Baergen
Hello,

I'm attempting to implement link-time dependency injection. In brief, what this means is that, for a library A that depends on B, we would normally have:

  target_link_libraries(A B)

However, when building unittests for A (done in a subdirectory), we instead want:

  target_link_libraries(A MockB)

I've spent a bunch of time looking through CMake documentation and I'm not sure how to express this without resorting to manually specifying the link line when A is in use. Could someone give me some guidance?

Thanks!
Josh

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Link-Time Dependency Injection for Unit Tests

Eric Noulard
Hi Joshua,


2018-03-15 4:18 GMT+01:00 Joshua Baergen <[hidden email]>:
Hello,

I'm attempting to implement link-time dependency injection. In brief, what this means is that, for a library A that depends on B, we would normally have:

  target_link_libraries(A B)

However, when building unittests for A (done in a subdirectory), we instead want:

  target_link_libraries(A MockB)

I've spent a bunch of time looking through CMake documentation and I'm not sure how to express this without resorting to manually specifying the link line when A is in use. Could someone give me some guidance?


Since you want link-time choice I bet you should have a look to some kind of generator expression 

genex may be used in target_link_libraries.

for example if you can set some property on (all) your unit test target
(I personnally do that globally by defining my own mytest_add_executable macro)

set_target_properties(<YourTestTarget> PROPERTIES UNITTEST 1)

then may be you can use some genex which check whether an executable if a unitest:

set(GenEx_IsTest $<STREQUAL:$<TARGET_PROPERTY:UNITTEST>,1>)

and then use some other genex using the previous one in target_link_libraries

like here for automatic instantiation of source:
I lack time to craft the complete example but I think it's doable with genex.

It seems that others already played along this line:

--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Link-Time Dependency Injection for Unit Tests

Joshua Baergen
Thanks Eric! That worked perfectly.

I had looked at generator expressions yesterday, but didn't understand at which point during makefile generation they were evaluated, and had falsely assumed that they were evaluated too early. Happy to learn new tricks! :)

Josh

On Thu, Mar 15, 2018 at 2:56 AM Eric Noulard <[hidden email]> wrote:
Hi Joshua,


2018-03-15 4:18 GMT+01:00 Joshua Baergen <[hidden email]>:
Hello,

I'm attempting to implement link-time dependency injection. In brief, what this means is that, for a library A that depends on B, we would normally have:

  target_link_libraries(A B)

However, when building unittests for A (done in a subdirectory), we instead want:

  target_link_libraries(A MockB)

I've spent a bunch of time looking through CMake documentation and I'm not sure how to express this without resorting to manually specifying the link line when A is in use. Could someone give me some guidance?


Since you want link-time choice I bet you should have a look to some kind of generator expression 

genex may be used in target_link_libraries.

for example if you can set some property on (all) your unit test target
(I personnally do that globally by defining my own mytest_add_executable macro)

set_target_properties(<YourTestTarget> PROPERTIES UNITTEST 1)

then may be you can use some genex which check whether an executable if a unitest:

set(GenEx_IsTest $<STREQUAL:$<TARGET_PROPERTY:UNITTEST>,1>)

and then use some other genex using the previous one in target_link_libraries

like here for automatic instantiation of source:
I lack time to craft the complete example but I think it's doable with genex.

It seems that others already played along this line:

--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Link-Time Dependency Injection for Unit Tests

Eric Noulard


2018-03-15 14:52 GMT+01:00 Joshua Baergen <[hidden email]>:
Thanks Eric! That worked perfectly.

I had looked at generator expressions yesterday, but didn't understand at which point during makefile generation they were evaluated, and had falsely assumed that they were evaluated too early. Happy to learn new tricks! :)

You're welcome.

--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake