Skip to content

Commit d8bc982

Browse files
committed
Add the cmake macro to generate tests
It uses CTest and boost.test. There is also an example in ExampleModule1.
1 parent dca8bb5 commit d8bc982

3 files changed

Lines changed: 86 additions & 5 deletions

File tree

Examples/ExampleModule1/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,15 @@ O2_GENERATE_EXECUTABLE(
3232
MODULE_LIBRARY_NAME ${LIBRARY_NAME}
3333
BUCKET_NAME ${BUCKET_NAME}
3434
)
35+
36+
37+
38+
set(TEST_SRCS
39+
test/testExampleModule1.cxx
40+
)
41+
42+
O2_GENERATE_TESTS(
43+
MODULE_LIBRARY_NAME ${LIBRARY_NAME}
44+
BUCKET_NAME ${BUCKET_NAME}
45+
TEST_SRCS ${TEST_SRCS}
46+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
///
2+
/// \author Barthelemy von Haller
3+
///
4+
5+
#include "../include/ExampleModule1/Foo.h"
6+
7+
#define BOOST_TEST_MODULE MO test
8+
#define BOOST_TEST_MAIN
9+
#define BOOST_TEST_DYN_LINK
10+
#include <boost/test/unit_test.hpp>
11+
#include <assert.h>
12+
#include "ExampleModule1/Foo.h"
13+
14+
15+
namespace AliceO2 {
16+
namespace Examples {
17+
namespace ExampleModule1 {
18+
19+
BOOST_AUTO_TEST_CASE(testFoo)
20+
{
21+
Foo foo;
22+
foo.greet();
23+
}
24+
25+
} /* namespace ExampleModule1 */
26+
} /* namespace Examples */
27+
} /* namespace AliceO2 */

cmake/O2Utils.cmake

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,12 @@ endmacro(O2_GENERATE_LIBRARY)
296296
# arg BUCKET_NAME
297297
# arg SOURCES
298298
# arg MODULE_LIBRARY_NAME - Name of the library of the module this executable belongs to. Optional.
299+
# arg INSTALL - True to install (default), false otherwise. Optional.
299300
function(O2_GENERATE_EXECUTABLE)
300301

301302
cmake_parse_arguments(
302303
PARSED_ARGS
303-
"" # bool args
304+
"INSTALL" # bool args
304305
"EXE_NAME;BUCKET_NAME;MODULE_LIBRARY_NAME" # mono-valued arguments
305306
"SOURCES" # multi-valued arguments
306307
${ARGN} # arguments
@@ -320,14 +321,55 @@ function(O2_GENERATE_EXECUTABLE)
320321
MODULE_LIBRARY_NAME ${PARSED_ARGS_MODULE_LIBRARY_NAME}
321322
)
322323

323-
############### install the executable #################
324-
install(TARGETS ${PARSED_ARGS_EXE_NAME} DESTINATION bin)
324+
if (NOT ${PARSED_ARGS_INSTALL} OR ${PARSED_ARGS_INSTALL})
325+
############### install the executable #################
326+
install(TARGETS ${PARSED_ARGS_EXE_NAME} DESTINATION bin)
325327

326-
############### install the library ###################
327-
install(TARGETS ${PARSED_ARGS_MODULE_LIBRARY_NAME} DESTINATION lib)
328+
############### install the library ###################
329+
install(TARGETS ${PARSED_ARGS_MODULE_LIBRARY_NAME} DESTINATION lib)
330+
endif ()
328331

329332
endfunction(O2_GENERATE_EXECUTABLE)
330333

334+
335+
#------------------------------------------------------------------------------
336+
# O2_GENERATE_TESTS
337+
# Generate tests for all source files listed in TEST_SRCS
338+
# arg BUCKET_NAME
339+
# arg TEST_SRCS
340+
# arg MODULE_LIBRARY_NAME - Name of the library of the module this executable belongs to.
341+
function(O2_GENERATE_TESTS)
342+
cmake_parse_arguments(
343+
PARSED_ARGS
344+
"" # bool args
345+
"BUCKET_NAME;MODULE_LIBRARY_NAME" # mono-valued arguments
346+
"TEST_SRCS" # multi-valued arguments
347+
${ARGN} # arguments
348+
)
349+
350+
CHECK_VARIABLE(PARSED_ARGS_BUCKET_NAME "You must provide a bucket name")
351+
CHECK_VARIABLE(PARSED_ARGS_TEST_SRCS "You must provide the list of sources")
352+
CHECK_VARIABLE(PARSED_ARGS_MODULE_LIBRARY_NAME "You must provide the module library name this executable belongs to")
353+
354+
foreach (test ${PARSED_ARGS_TEST_SRCS})
355+
string(REGEX REPLACE ".*/" "" test_name ${test})
356+
string(REGEX REPLACE "\\..*" "" test_name ${test_name})
357+
358+
message(STATUS "Generate test ${test_name}")
359+
360+
O2_GENERATE_EXECUTABLE(
361+
EXE_NAME ${test_name}
362+
SOURCES ${test}
363+
MODULE_LIBRARY_NAME ${PARSED_ARGS_MODULE_LIBRARY_NAME}
364+
BUCKET_NAME ${PARSED_ARGS_BUCKET_NAME}
365+
INSTALL FALSE
366+
)
367+
target_link_libraries(${test_name} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
368+
add_test(NAME ${test_name} COMMAND ${test_name})
369+
endforeach ()
370+
endfunction()
371+
372+
331373
#------------------------------------------------------------------------------
332374
# CHECK_VARIABLE
333375
macro(CHECK_VARIABLE VARIABLE_NAME ERROR_MESSAGE)

0 commit comments

Comments
 (0)