Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ jobs:
${{ steps.strings.outputs.cmake_args }}
-D BuildExamples=ON
-D BuildInternalTests=ON
-DEnableMainHelperClasses=ON
-D EnableMainHelperClasses=ON
-D BuildExternalTests=ON
-D CMAKE_PREFIX_PATH=${{ github.workspace }}/installed

- &build-tests
Expand Down
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ target_include_directories(CppUnit2Gtest INTERFACE
$<INSTALL_INTERFACE:include>
)

# install
include(tests/GetGtest.cmake)
target_link_libraries(CppUnit2Gtest INTERFACE GTest::GTest)

# Install targets
install(TARGETS CppUnit2Gtest
Expand Down Expand Up @@ -83,5 +84,16 @@ create_header_alias(\${SOURCE_FILE} \${INSTALL_INCLUDE_DIR}/cppunit/extensions/H
create_header_alias(\${SOURCE_FILE} \${INSTALL_INCLUDE_DIR}/cppunit/config/SourcePrefix.h)
create_header_alias(\${SOURCE_FILE} \${INSTALL_INCLUDE_DIR}/cppunit/TestAssert.h)
")
if (EnableMainHelperClasses)
install(CODE "
set(INSTALL_INCLUDE_DIR \${CMAKE_INSTALL_PREFIX}/include)
set(SOURCE_FILE CppUnit2Gtest.hpp)
include(tests/CreateSymlink.cmake)
create_header_alias(\${SOURCE_FILE} \${INSTALL_INCLUDE_DIR}/cppunit/TestPath.h)
create_header_alias(\${SOURCE_FILE} \${INSTALL_INCLUDE_DIR}/cppunit/extensions/TestFactoryRegistry.h)
create_header_alias(\${SOURCE_FILE} \${INSTALL_INCLUDE_DIR}/cppunit/ui/text/TestRunner.h)
create_header_alias(\${SOURCE_FILE} \${INSTALL_INCLUDE_DIR}/cppunit/TestFixture.h)
")
endif()

# We don't install GTest so the user can have any they would like and/or install it later
5 changes: 3 additions & 2 deletions CppUnit2Gtest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <gtest/gtest.h>

#define CppUnit2Gtest

#define CPPUNIT_NS ::CppUnit

namespace CppUnit {
Expand Down Expand Up @@ -245,7 +247,7 @@ namespace to { namespace gtest {
#define CPPUNIT_ASSERT_THROW_MESSAGE(message, expression, expected) ASSERT_THROW(expression, expected) << message
#define CPPUNIT_ASSERT_DOUBLES_EQUAL(a,b, t) ASSERT_NEAR(a, b, t)
#define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(msg, a, b, t) ASSERT_NEAR(a, b, t) << msg
#define CPPUNIT_FAIL(message) FAIL(message)
#define CPPUNIT_FAIL(message) FAIL() << message
#define CPPUNIT_ASSERT_ASSERTION_PASS(e) ASSERT_NO_THROW(e)
#define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE(msg, e) ASSERT_NO_THROW(e) << msg
#define CPPUNIT_ASSERT_LESS(expected, actual) ASSERT_LT(actual, expected)
Expand Down Expand Up @@ -486,7 +488,6 @@ namespace TextUi {
// Has similar (enough) interface as: https://cppunit.sourceforge.net/doc/cvs/class_text_test_runner.html
}


} // namespace CppUnit

#endif // Cpp2Unit2Gtest_EnableMainHelperClasses
Expand Down
12 changes: 12 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ option(BuildUnityTests
OFF
)
option(MutationTesting "Experimental, for mutation testing" OFF)
option(BuildExternalTests "Build and add tests from external libs" OFF)

if (NOT (BuildExamples OR BuildInternalTests OR BuildUnityTests))
message(FATAL_ERROR "Asked to build tests but no tests were given")
Expand Down Expand Up @@ -128,6 +129,17 @@ else()
)
endif()

if (BuildExternalTests)
if(NOT EnableMainHelperClasses)
message(FATAL_ERROR "EnableMainHelperClasses must be set for external tests")
endif()
message(STATUS "Building external tests")
if (build_testing)
message(WARNING "Building external tests with internal package, not recommended")
endif()
add_subdirectory(external_tests)
endif()

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
# The gtest find tests function doesn't work because it relies on regex expressions
# We have tests that look like CppUnit so it won't add them.
Expand Down
4 changes: 4 additions & 0 deletions tests/GetGtest.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if (TARGET GTest::GTest)
return()
endif()

# Try to find GTest first
find_package(GTest QUIET)

Expand Down
24 changes: 24 additions & 0 deletions tests/external_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.20..3.28.3)

project(CppUnit2Gtest_ExternalTests
VERSION 0.0.1
DESCRIPTION "Runs repo tests using CppUnit2Gtest instead of cppunit"
)

include(FetchContent)
include(../GetGtest.cmake)

if (NOT TARGET CppUnit2Gtest::CppUnit2Gtest)
# find_package on our library
find_package(CppUnit2Gtest REQUIRED)
endif()

# Alias it to have CMake understand
if (NOT TARGET cppunit)
add_library(cppunit ALIAS CppUnit2Gtest::CppUnit2Gtest)
endif()

# Tests
enable_testing()
include(Martchus-cpp-utilities.cmake)

32 changes: 32 additions & 0 deletions tests/external_tests/Martchus-cpp-utilities.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

set(BUILD_TESTING ON)
set(EXCLUDE_TESTS_FROM_ALL OFF)

if (WIN32)
# Attempts to reduce features to allow it to work on CI
set(USE_ICONV OFF)
set(USE_BOOST_PROCESS OFF)
set(USE_NATIVE_FILE_BUFFER ON)
set(FORCE_BOOST_IOSTREAMS_FOR_NATIVE_FILE_BUFFER OFF)
# Either boost or gcc is needed so it doesn't work on CI
if ("$ENV{CI}")
message(STATUS "Cannot build cpp-utilites without boost or gnu, skipping test")
return()
endif()
endif()

# pull the code we want to test
FetchContent_Declare(
cpputils
GIT_REPOSITORY https://github.com/OlekRaymond/cpp-utilities.git
GIT_TAG cppunit-removal
)
# Currently using a fork with small changes.
# TODO: update to use source repo and release tags

# Make it available
# This also includes the build for the lib which I don't think we need
FetchContent_MakeAvailable(cpputils)

# Use ctest to run
# (nothing required)
19 changes: 19 additions & 0 deletions tests/external_tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# External tests

These tests are on external projects and follow roughly these steps for drop in replacement tests:

- Build CppUnit2Gtest
+ Requires `-DEnableMainLikeClasses=ON -DInstallAsCppUnit=ON`
- Install CppUnit2Gtest
+ To prefx path "P"
- Build 3rd Party tests
+ Telling CMake to look in "P" for CppUnit
- Run tests

Or for repos where we have changed main:
- Build CppUnit2Gtest
- Install CppUnit2Gtest
- Build 3rd Party tests
- Run tests