Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Serge Nikulin <serge@safeai.ai>
  • Loading branch information
Serge Nikulin committed Sep 27, 2021
1 parent 9269c03 commit 31a3d10
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 7 deletions.
38 changes: 38 additions & 0 deletions ament_cmake_gen_version_h/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
cmake_minimum_required(VERSION 3.5)

project(ament_cmake_gen_version_h)
include(CTest)

find_package(ament_cmake_core REQUIRED)

ament_package(CONFIG_EXTRAS "ament_cmake_gen_version_h-extras.cmake")

# Default to C11
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 11)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(BUILD_TESTING)
# Simulate pre-installed package
set(ament_cmake_gen_version_h_DIR ${CMAKE_SOURCE_DIR}/cmake)
include (cmake/ament_cmake_gen_version_h.cmake)
find_package(ament_cmake_gtest REQUIRED)

ament_cmake_gen_version_h(NO_INSTALL TRUE)
ament_cmake_gen_version_h(NO_INSTALL TRUE VERSION_FILE_NAME "version1.h")
ament_cmake_gen_version_h(NO_INSTALL TRUE INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include VERSION_FILE_NAME "version2.h")
ament_cmake_gen_version_h(
NO_INSTALL TRUE
INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include
VERSION_FILE_NAME "version_custom.h"
VERSION_MAJOR 1
VERSION_MINOR 2
VERSION_PATCH 3
)

ament_add_gtest(test_${PROJECT_NAME}
test/test_version_custom.cpp
test/test_version_h.cpp
test/test_version1_h.cpp
test/test_version2_h.cpp
)

endif()


install(
DIRECTORY cmake
DESTINATION share/${PROJECT_NAME}
Expand Down
39 changes: 32 additions & 7 deletions ament_cmake_gen_version_h/cmake/ament_cmake_gen_version_h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,28 @@
# :type VERSION_FILE_NAME: string
# :default value VERSION_FILE_NAME: version.h

# :param VERSION_MAJOR: override VERSION_MAJOR
# :type VERSION_MAJOR: string
# :default value VERSION_MAJOR: VERSION_MAJOR from the package.xml file

# :param VERSION_MINOR: file name of the generated header file
# :type VERSION_MINOR: string
# :default value VERSION_MINOR: VERSION_MINOR from the package.xml file

# :param VERSION_PATCH: file name of the generated header file
# :type VERSION_PATCH: string
# :default value VERSION_PATCH: VERSION_PATCH from the package.xml file

################################################################################
function(ament_cmake_gen_version_h)
cmake_parse_arguments(
ARG # prefix of all variables
"NO_INSTALL" # list of names of the boolean arguments (only defined ones will be true)
"INCLUDE_DIR;VERSION_FILE_NAME" # list of names of mono-valued arguments
"INCLUDE_DIR;VERSION_FILE_NAME;VERSION_MAJOR;VERSION_MINOR;VERSION_PATCH" # list of mono arguments
"" # list of names of multi-valued arguments (output variables are lists)
${ARGN} # arguments of the function to parse, here we take the all original ones
)

# Find myself so I can use my own cmake instalation folder `ament_cmake_gen_version_h_DIR`
# I can't rely on a previous `find_package` call because it could be a direct call
find_package(ament_cmake_gen_version_h REQUIRED)
set(TEMPLATE_FILE "${ament_cmake_gen_version_h_DIR}/version.h.in")
if(NOT EXISTS "${TEMPLATE_FILE}")
message(FATAL_ERROR "Can't find ${TEMPLATE_FILE}. Reinstall ament_cmake_gen_version_h package.")
Expand Down Expand Up @@ -76,9 +85,25 @@ function(ament_cmake_gen_version_h)

# parse version information from the version string
string(REGEX MATCH "([0-9]+)\.([0-9]+)\.([0-9]+)" "" dummy ${VERSION_STR})
set(VERSION_MAJOR ${CMAKE_MATCH_1})
set(VERSION_MINOR ${CMAKE_MATCH_2})
set(VERSION_PATCH ${CMAKE_MATCH_3})
if (ARG_VERSION_MAJOR)
set(VERSION_MAJOR ${ARG_VERSION_MAJOR})
else()
set(VERSION_MAJOR ${CMAKE_MATCH_1})
endif()

if (ARG_VERSION_MINOR)
set(VERSION_MINOR ${ARG_VERSION_MINOR})
else()
set(VERSION_MINOR ${CMAKE_MATCH_2})
endif()

if (ARG_VERSION_PATCH)
set(VERSION_PATCH ${ARG_VERSION_PATCH})
else()
set(VERSION_PATCH ${CMAKE_MATCH_3})
endif()

set(VERSION_STR ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})

# Check if the version file exist
if(EXISTS "${VERSION_FILE_NAME}")
Expand Down
6 changes: 6 additions & 0 deletions ament_cmake_gen_version_h/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
<author>Serge Nikulin</author>

<buildtool_depend>ament_cmake_core</buildtool_depend>
<buildtool_depend>ament_package</buildtool_depend>

<buildtool_export_depend>ament_cmake_core</buildtool_export_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_cmake_gmoc</test_depend>


<export>
<build_type>ament_cmake</build_type>
Expand Down
2 changes: 2 additions & 0 deletions ament_cmake_gen_version_h/test/test_version1_h.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Test the presense of the following header file
#include <ament_cmake_gen_version_h/version1.h>
2 changes: 2 additions & 0 deletions ament_cmake_gen_version_h/test/test_version2_h.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Test the presense of the following header file
#include <ament_cmake_gen_version_h/version2.h>
17 changes: 17 additions & 0 deletions ament_cmake_gen_version_h/test/test_version_custom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Test the presense of the following header file
#include <ament_cmake_gen_version_h/version_custom.h>
#include <gtest/gtest.h>

TEST(test_ament_cmake_gen_version_h, version_custom)
{
EXPECT_TRUE(AMENT_CMAKE_GEN_VERSION_H_VERSION_GTE(0, 0, 0));
EXPECT_TRUE(AMENT_CMAKE_GEN_VERSION_H_VERSION_GTE(0, 1, 2));
EXPECT_TRUE(AMENT_CMAKE_GEN_VERSION_H_VERSION_GTE(1, 2, 3));
EXPECT_FALSE(AMENT_CMAKE_GEN_VERSION_H_VERSION_GTE(1, 2, 4));
EXPECT_FALSE(AMENT_CMAKE_GEN_VERSION_H_VERSION_GTE(1, 3, 2));
EXPECT_FALSE(AMENT_CMAKE_GEN_VERSION_H_VERSION_GTE(2, 1, 2));
EXPECT_EQ(AMENT_CMAKE_GEN_VERSION_H_VERSION_MAJOR, 1);
EXPECT_EQ(AMENT_CMAKE_GEN_VERSION_H_VERSION_MINOR, 2);
EXPECT_EQ(AMENT_CMAKE_GEN_VERSION_H_VERSION_PATCH, 3);
EXPECT_STREQ(AMENT_CMAKE_GEN_VERSION_H_VERSION_STR, "1.2.3");
}
2 changes: 2 additions & 0 deletions ament_cmake_gen_version_h/test/test_version_h.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Test the presense of the following header file
#include <ament_cmake_gen_version_h/version.h>

0 comments on commit 31a3d10

Please sign in to comment.