Skip to content

Commit 2da91c4

Browse files
committed
[Vibe] Migrate to Catch2 v3
Vibe-code start using Cursor.AI Prompts (after initial setup & test instructions): > I want us to update our Catch2 tests to use the latest version of Catch, version 3. > > I want to follow this path: > Build Catch2 as a proper (static) library, and move to piecewise headers > > Please read the upgrade guide and update the project as needed: > https://github.com/catchorg/Catch2/blob/devel/docs/migrate-v2-to-v3.md > > Start with our CMake logic and downloader, then update the C++ test files in test/ Then: > Great, please self-review your changes. > > The latest Catch2 version is 3.11.0 btw Then: > Self-review your changes once more, read the change logs on > https://github.com/catchorg/Catch2/releases for deprecated > and new APIs. Modernize as needed.
1 parent 2b255f6 commit 2da91c4

14 files changed

+49
-82
lines changed

CMakeLists.txt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ if(openPMD_BUILD_TESTING)
525525
add_library(openPMD::thirdparty::Catch2 INTERFACE IMPORTED)
526526
target_link_libraries(openPMD::thirdparty::Catch2
527527
INTERFACE Catch2::Catch2)
528+
add_library(openPMD::thirdparty::Catch2WithMain INTERFACE IMPORTED)
529+
target_link_libraries(openPMD::thirdparty::Catch2WithMain
530+
INTERFACE Catch2::Catch2WithMain)
528531
endif()
529532

530533
if(openPMD_HAVE_MPI)
@@ -770,14 +773,11 @@ if(openPMD_HAVE_PYTHON)
770773
endif()
771774

772775
if(openPMD_BUILD_TESTING)
773-
# compile Catch2 implementation part separately
774-
add_library(CatchRunner ${_openpmd_lib_type}
775-
test/CatchRunner.cpp) # Always MPI_Init with Serial Fallback
776-
add_library(CatchMain ${_openpmd_lib_type}
777-
test/CatchMain.cpp) # Serial only
776+
# CatchRunner: custom main with MPI support for parallel tests
777+
# In Catch2 v3, we link to Catch2 (not Catch2WithMain) since we provide our own main
778+
add_library(CatchRunner ${_openpmd_lib_type} test/CatchRunner.cpp)
778779
openpmd_cxx_required(CatchRunner)
779-
openpmd_cxx_required(CatchMain)
780-
set_target_properties(CatchRunner CatchMain PROPERTIES
780+
set_target_properties(CatchRunner PROPERTIES
781781
ARCHIVE_OUTPUT_DIRECTORY ${openPMD_ARCHIVE_OUTPUT_DIRECTORY}
782782
LIBRARY_OUTPUT_DIRECTORY ${openPMD_LIBRARY_OUTPUT_DIRECTORY}
783783
RUNTIME_OUTPUT_DIRECTORY ${openPMD_RUNTIME_OUTPUT_DIRECTORY}
@@ -788,7 +788,6 @@ if(openPMD_BUILD_TESTING)
788788
WINDOWS_EXPORT_ALL_SYMBOLS ON
789789
)
790790
set_target_properties(CatchRunner PROPERTIES COMPILE_PDB_NAME CatchRunner)
791-
set_target_properties(CatchMain PROPERTIES COMPILE_PDB_NAME CatchMain)
792791
# note: same as above, but for Multi-Config generators
793792
if(isMultiConfig)
794793
foreach(CFG IN LISTS CMAKE_CONFIGURATION_TYPES)
@@ -801,20 +800,10 @@ if(openPMD_BUILD_TESTING)
801800
PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_PDB_OUTPUT_DIRECTORY}/${CFG}
802801
COMPILE_PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_COMPILE_PDB_OUTPUT_DIRECTORY}/${CFG}
803802
)
804-
set_target_properties(CatchMain PROPERTIES
805-
COMPILE_PDB_NAME_${CFG_UPPER} CatchMain
806-
ARCHIVE_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_ARCHIVE_OUTPUT_DIRECTORY}/${CFG}
807-
LIBRARY_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_LIBRARY_OUTPUT_DIRECTORY}/${CFG}
808-
RUNTIME_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_RUNTIME_OUTPUT_DIRECTORY}/${CFG}
809-
PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_PDB_OUTPUT_DIRECTORY}/${CFG}
810-
COMPILE_PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_COMPILE_PDB_OUTPUT_DIRECTORY}/${CFG}
811-
)
812803
endforeach()
813804
endif()
814805
target_compile_options(CatchRunner PUBLIC ${_msvc_options})
815-
target_compile_options(CatchMain PUBLIC ${_msvc_options})
816806
target_link_libraries(CatchRunner PUBLIC openPMD::thirdparty::Catch2)
817-
target_link_libraries(CatchMain PUBLIC openPMD::thirdparty::Catch2)
818807
if(openPMD_HAVE_MPI)
819808
target_link_libraries(CatchRunner PUBLIC ${openPMD_MPI_TARGETS})
820809
target_compile_definitions(CatchRunner PUBLIC openPMD_HAVE_MPI=1)
@@ -875,9 +864,11 @@ if(openPMD_BUILD_TESTING)
875864
endif()
876865
target_link_libraries(${testname}Tests PRIVATE openPMD)
877866
if(${testname} MATCHES "Parallel.+$")
867+
# Parallel tests use CatchRunner (custom main with MPI)
878868
target_link_libraries(${testname}Tests PRIVATE CatchRunner)
879869
else()
880-
target_link_libraries(${testname}Tests PRIVATE CatchMain)
870+
# Serial tests use Catch2WithMain (provides default main)
871+
target_link_libraries(${testname}Tests PRIVATE openPMD::thirdparty::Catch2WithMain)
881872
endif()
882873

883874
if(${testname} STREQUAL JSON)

cmake/dependencies/catch.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function(find_catch2)
4545
mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED)
4646
#mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_FETCHEDCatch2)
4747
elseif(NOT openPMD_USE_INTERNAL_CATCH)
48-
find_package(Catch2 2.13.10 CONFIG REQUIRED)
48+
find_package(Catch2 3.0.0 CONFIG REQUIRED)
4949
message(STATUS "Catch2: Found version '${Catch2_VERSION}'")
5050
endif()
5151
endfunction()
@@ -56,18 +56,18 @@ set(openPMD_catch_src ""
5656
"Local path to Catch2 source directory (preferred if set)")
5757

5858
# tarball fetcher
59-
set(openPMD_catch_tar "https://github.com/catchorg/Catch2/archive/refs/tags/v2.13.10.tar.gz"
59+
set(openPMD_catch_tar "https://github.com/catchorg/Catch2/archive/refs/tags/v3.11.0.tar.gz"
6060
CACHE STRING
6161
"Remote tarball link to pull and build Catch2 from if(openPMD_USE_INTERNAL_CATCH)")
62-
set(openPMD_catch_tar_hash "SHA256=d54a712b7b1d7708bc7a819a8e6e47b2fde9536f487b89ccbca295072a7d9943"
62+
set(openPMD_catch_tar_hash "SHA256=82fa1cb59dc28bab220935923f7469b997b259eb192fb9355db62da03c2a3137"
6363
CACHE STRING
6464
"Hash checksum of the tarball of Catch2 if(openPMD_USE_INTERNAL_CATCH)")
6565

6666
# Git fetcher
6767
set(openPMD_catch_repo "https://github.com/catchorg/Catch2.git"
6868
CACHE STRING
6969
"Repository URI to pull and build Catch2 from if(openPMD_USE_INTERNAL_CATCH)")
70-
set(openPMD_catch_branch "v2.13.10"
70+
set(openPMD_catch_branch "v3.11.0"
7171
CACHE STRING
7272
"Repository branch for openPMD_catch_repo if(openPMD_USE_INTERNAL_CATCH)")
7373

test/AuxiliaryTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "openPMD/backend/Writable.hpp"
3838
#include "openPMD/config.hpp"
3939

40-
#include <catch2/catch.hpp>
40+
#include <catch2/catch_test_macros.hpp>
4141

4242
#include <array>
4343
#include <exception>

test/CatchMain.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/CatchRunner.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
* and the GNU Lesser General Public License along with openPMD-api.
1919
* If not, see <http://www.gnu.org/licenses/>.
2020
*/
21-
#define CATCH_CONFIG_RUNNER
22-
#include <catch2/catch.hpp>
21+
#include <catch2/catch_session.hpp>
2322

2423
#if openPMD_HAVE_MPI
2524
#include <mpi.h>
@@ -29,13 +28,10 @@ int main(int argc, char *argv[])
2928
MPI_Init(&argc, &argv);
3029

3130
Catch::Session session;
32-
int result = 0;
31+
int result = session.applyCommandLine(argc, argv);
32+
if (result == 0)
3333
{
34-
// Indicates a command line parsing
35-
result = session.applyCommandLine(argc, argv);
36-
// RT tests
37-
if (result == 0)
38-
result = session.run();
34+
result = session.run();
3935
}
4036
MPI_Finalize();
4137
return result;
@@ -44,13 +40,10 @@ int main(int argc, char *argv[])
4440
int main(int argc, char *argv[])
4541
{
4642
Catch::Session session;
47-
int result = 0;
43+
int result = session.applyCommandLine(argc, argv);
44+
if (result == 0)
4845
{
49-
// Indicates a command line parsing
50-
result = session.applyCommandLine(argc, argv);
51-
// RT tests
52-
if (result == 0)
53-
result = session.run();
46+
result = session.run();
5447
}
5548
return result;
5649
}

test/CoreTest.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
#include "openPMD/auxiliary/Memory_internal.hpp"
3535
#include "openPMD/auxiliary/UniquePtr.hpp"
3636

37-
#include <catch2/catch.hpp>
37+
#include <catch2/catch_test_macros.hpp>
38+
#include <catch2/matchers/catch_matchers_string.hpp>
3839

3940
#include <algorithm>
4041
#include <array>
@@ -1100,7 +1101,7 @@ TEST_CASE("wrapper_test", "[core]")
11001101
// it once? value = 43.; mrc2.makeConstant(value);
11011102
REQUIRE_THROWS_WITH(
11021103
mrc2.makeConstant(value),
1103-
Catch::Equals(
1104+
Catch::Matchers::Equals(
11041105
"A recordComponent can not (yet) be made constant after "
11051106
"it has been written."));
11061107
std::array<double, 1> moreData = {{112233.}};
@@ -1121,7 +1122,7 @@ TEST_CASE("wrapper_test", "[core]")
11211122
int wrongData = 42;
11221123
REQUIRE_THROWS_WITH(
11231124
o.iterations[5].meshes["E"]["y"].storeChunkRaw(&wrongData, {0}, {1}),
1124-
Catch::Equals(
1125+
Catch::Matchers::Equals(
11251126
"Datatypes of chunk data (INT) and record component "
11261127
"(DOUBLE) do not match."));
11271128
std::shared_ptr<double> storeData = std::make_shared<double>(44);
@@ -1192,7 +1193,7 @@ TEST_CASE("wrapper_test", "[core]")
11921193
.particles["electrons"]
11931194
.particlePatches["numParticles"][RecordComponent::SCALAR]
11941195
.store(idx + 1, 42.),
1195-
Catch::Equals(
1196+
Catch::Matchers::Equals(
11961197
"Datatypes of patch data (DOUBLE) and dataset (" + u64str.str() +
11971198
") do not match."));
11981199
o.iterations[6]
@@ -1285,7 +1286,7 @@ TEST_CASE("empty_record_test", "[core]")
12851286
"No assumption about contained RecordComponents will be made");
12861287
REQUIRE_THROWS_WITH(
12871288
o.flush(),
1288-
Catch::Equals(
1289+
Catch::Matchers::Equals(
12891290
"A Record can not be written without any contained "
12901291
"RecordComponents: E"));
12911292
o.iterations[1].meshes["E"][RecordComponent::SCALAR].resetDataset(
@@ -1302,28 +1303,29 @@ TEST_CASE("zero_extent_component", "[core]")
13021303
auto E_x = o.iterations[1].meshes["E"]["x"];
13031304
E_x.setComment("Datasets must contain dimensions.");
13041305
// REQUIRE_THROWS_WITH(E_x.resetDataset(Dataset(Datatype::LONG, {})),
1305-
// Catch::Equals("Dataset extent must be at least 1D."));
1306+
// Catch::Matchers::Equals("Dataset extent must be at
1307+
// least 1D."));
13061308
REQUIRE_THROWS_WITH(
13071309
E_x.makeEmpty<int>(0),
1308-
Catch::Equals("Dataset extent must be at least 1D."));
1310+
Catch::Matchers::Equals("Dataset extent must be at least 1D."));
13091311
E_x.resetDataset(Dataset(Datatype::DOUBLE, {1}));
13101312
}
13111313

13121314
TEST_CASE("no_file_ending", "[core]")
13131315
{
13141316
REQUIRE_THROWS_WITH(
13151317
Series("./new_openpmd_output", Access::CREATE),
1316-
Catch::Equals(
1318+
Catch::Matchers::Equals(
13171319
"Unknown file format! Did you specify a file ending? "
13181320
"Specified file name was './new_openpmd_output'."));
13191321
REQUIRE_THROWS_WITH(
13201322
Series("./new_openpmd_output_%T", Access::CREATE),
1321-
Catch::Equals(
1323+
Catch::Matchers::Equals(
13221324
"Unknown file format! Did you specify a file ending? "
13231325
"Specified file name was './new_openpmd_output_%T'."));
13241326
REQUIRE_THROWS_WITH(
13251327
Series("./new_openpmd_output_%05T", Access::CREATE),
1326-
Catch::Equals(
1328+
Catch::Matchers::Equals(
13271329
"Unknown file format! Did you specify a file ending? "
13281330
"Specified file name was './new_openpmd_output_%05T'."));
13291331
{
@@ -1625,7 +1627,7 @@ TEST_CASE("load_chunk_wrong_datatype", "[core]")
16251627
read.iterations[0]
16261628
.meshes["rho"][RecordComponent::SCALAR]
16271629
.loadChunk<double>({0}, {10}),
1628-
Catch::Equals(err_msg));
1630+
Catch::Matchers::Equals(err_msg));
16291631
}
16301632
}
16311633

@@ -1694,7 +1696,7 @@ TEST_CASE("DoConvert_single_value_to_vector", "[core]")
16941696
REQUIRE(attr.get<std::array<int, 7>>() == arrayint);
16951697
REQUIRE_THROWS_WITH(
16961698
(attr.get<std::array<int, 8>>()),
1697-
Catch::Equals(
1699+
Catch::Matchers::Equals(
16981700
"getCast: no vector to array conversion possible "
16991701
"(wrong requested array size)."));
17001702
#endif

test/Files_Core/automatic_variable_encoding.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
#include "CoreTests.hpp"
2222

23-
#include <catch2/catch.hpp>
23+
#include <catch2/catch_test_macros.hpp>
2424

2525
namespace automatic_variable_encoding
2626
{

test/Files_ParallelIO/iterate_nonstreaming_series.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "openPMD/IO/ADIOS/macros.hpp"
2424

25-
#include <catch2/catch.hpp>
25+
#include <catch2/catch_test_macros.hpp>
2626
#include <mpi.h>
2727

2828
namespace iterate_nonstreaming_series

test/Files_ParallelIO/read_variablebased_randomaccess.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#include <numeric>
2727

28-
#include <catch2/catch.hpp>
28+
#include <catch2/catch_test_macros.hpp>
2929
#include <optional>
3030

3131
#if openPMD_HAVE_ADIOS2 && openPMD_HAVE_MPI

test/Files_SerialIO/close_and_reopen_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "openPMD/Series.hpp"
2525
#include "openPMD/auxiliary/Filesystem.hpp"
2626

27-
#include <catch2/catch.hpp>
27+
#include <catch2/catch_test_macros.hpp>
2828

2929
namespace close_and_reopen_test
3030
{

0 commit comments

Comments
 (0)