Skip to content

Commit

Permalink
Use Catch2 version 3 if installed
Browse files Browse the repository at this point in the history
  • Loading branch information
mhekkel committed Feb 17, 2024
1 parent d94f6f4 commit 9c78131
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 42 deletions.
26 changes: 20 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# We're using the older version 2 of Catch2
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.13.9)

FetchContent_MakeAvailable(Catch2)
find_package(Catch2 QUIET)

if (NOT Catch2_FOUND)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.13.9)

FetchContent_MakeAvailable(Catch2)

set(Catch2_VERSION "2.13.9")
endif()

list(
APPEND
Expand All @@ -19,10 +26,17 @@ list(
reconstruction
validate-pdbx)


add_library(test-main OBJECT "${CMAKE_CURRENT_SOURCE_DIR}/test-main.cpp")

target_link_libraries(test-main cifpp::cifpp Catch2::Catch2)

if(${Catch2_VERSION} VERSION_GREATER_EQUAL "3.0.0")
target_compile_definitions(test-main PUBLIC CATCH22=0)
else()
target_compile_definitions(test-main PUBLIC CATCH22=1)
endif()


foreach(CIFPP_TEST IN LISTS CIFPP_tests)
set(CIFPP_TEST "${CIFPP_TEST}-test")
set(CIFPP_TEST_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/${CIFPP_TEST}.cpp")
Expand Down
3 changes: 2 additions & 1 deletion test/format-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <catch2/catch.hpp>

#include "test-main.hpp"

#include <stdexcept>

Expand Down
2 changes: 0 additions & 2 deletions test/model-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

#include "test-main.hpp"

#include <catch2/catch.hpp>

#include <stdexcept>

#include <cif++.hpp>
Expand Down
2 changes: 0 additions & 2 deletions test/reconstruction-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

#include <cif++.hpp>

#include <catch2/catch.hpp>

#include <iostream>
#include <fstream>

Expand Down
2 changes: 0 additions & 2 deletions test/rename-compound-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

#include <cif++.hpp>

#include <catch2/catch.hpp>

#include <iostream>
#include <fstream>

Expand Down
30 changes: 28 additions & 2 deletions test/spinner-test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
#include "cif++/utilities.hpp"
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 NKI/AVL, Netherlands Cancer Institute
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "test-main.hpp"

#include <catch2/catch.hpp>
#include "cif++/utilities.hpp"

#include <random>
#include <thread>
Expand Down
2 changes: 0 additions & 2 deletions test/sugar-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

#include "test-main.hpp"

#include <catch2/catch.hpp>

#include <stdexcept>

#include <cif++.hpp>
Expand Down
9 changes: 6 additions & 3 deletions test/test-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

#include <cif++.hpp>

#define CATCH_CONFIG_RUNNER
#include <catch2/catch.hpp>

std::filesystem::path gTestDir = std::filesystem::current_path();

int main(int argc, char *argv[])
{
Catch::Session session; // There must be exactly one instance

// Build a new parser on top of Catch2's
#if CATCH22
using namespace Catch::clara;
#else
// Build a new parser on top of Catch2's
using namespace Catch::Clara;
#endif

auto cli = session.cli() // Get Catch2's command line parser
| Opt(gTestDir, "data-dir") // bind variable to a new option, with a hint string
["-D"]["--data-dir"] // the option names it will respond to
Expand Down
34 changes: 34 additions & 0 deletions test/test-main.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 NKI/AVL, Netherlands Cancer Institute
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#if CATCH22
#include <catch2/catch.hpp>
#else
#include <catch2/catch_all.hpp>
#endif

#include <filesystem>

extern std::filesystem::path gTestDir;
2 changes: 0 additions & 2 deletions test/unit-3d-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

#include "test-main.hpp"

#include <catch2/catch.hpp>

#include <stdexcept>

#include <cif++.hpp>
Expand Down
65 changes: 47 additions & 18 deletions test/unit-v2-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

#include "test-main.hpp"

#include <catch2/catch.hpp>

#include <cif++.hpp>

#include "cif++/dictionary_parser.hpp"
Expand Down Expand Up @@ -746,27 +744,51 @@ _cat_2.desc
std::istream is_data(&data_buffer);
f.load(is_data);

auto &cat1 = f.front()["cat_1"];
auto &cat2 = f.front()["cat_2"];
SECTION("one")
{
auto &cat1 = f.front()["cat_1"];
auto &cat2 = f.front()["cat_2"];

REQUIRE(cat1.size() == 3);
REQUIRE(cat2.size() == 3);
REQUIRE(cat1.size() == 3);
REQUIRE(cat2.size() == 3);

cat1.erase(cif::key("id") == 1);
cat1.erase(cif::key("id") == 1);

REQUIRE(cat1.size() == 2);
REQUIRE(cat2.size() == 1);
REQUIRE(cat1.size() == 2);
REQUIRE(cat2.size() == 1);

// REQUIRE_THROWS_AS(cat2.emplace({
// { "id", 4 },
// { "parent_id", 4 },
// { "desc", "moet fout gaan" }
// }), std::exception);
// REQUIRE_THROWS_AS(cat2.emplace({
// { "id", 4 },
// { "parent_id", 4 },
// { "desc", "moet fout gaan" }
// }), std::exception);

REQUIRE_THROWS_AS(cat2.emplace({ { "id", "vijf" }, // <- invalid value
{ "parent_id", 2 },
{ "desc", "moet fout gaan" } }),
std::exception);
REQUIRE_THROWS_AS(cat2.emplace({ { "id", "vijf" }, // <- invalid value
{ "parent_id", 2 },
{ "desc", "moet fout gaan" } }),
std::exception);
}

// SECTION("two")
// {
// auto &cat1 = f.front()["cat_1"];
// auto &cat2 = f.front()["cat_2"];

// cat1.update_value(cif::all(), "id", [](std::string_view v) -> std::string
// {
// int vi;
// auto [ec, ptr] = std::from_chars(v.data(), v.data() + v.length(), vi);
// return std::to_string(vi + 1);
// });

// REQUIRE(cat1.find1<std::string>(cif::key("id") == 2, "name") == "Aap");
// REQUIRE(cat1.find1<std::string>(cif::key("id") == 3, "name") == "Noot");
// REQUIRE(cat1.find1<std::string>(cif::key("id") == 4, "name") == "Mies");

// REQUIRE(cat2.find1<int>(cif::key("id") == 1, "parent_id") == 2);
// REQUIRE(cat2.find1<int>(cif::key("id") == 2, "parent_id") == 2);
// REQUIRE(cat2.find1<int>(cif::key("id") == 3, "parent_id") == 3);
// }
}

// --------------------------------------------------------------------
Expand Down Expand Up @@ -3486,4 +3508,11 @@ TEST_CASE("pdb2cif_formula_weight")

fw = a.front()["entity"].find1<float>(cif::key("id") == 3, "formula_weight");
CHECK(fw == 18.015f);
}

// --------------------------------------------------------------------

TEST_CASE("update_values_with_provider")
{

}
2 changes: 0 additions & 2 deletions test/validate-pdbx-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

#include "test-main.hpp"

#include <catch2/catch.hpp>

#include <cif++.hpp>

#include <stdexcept>
Expand Down

0 comments on commit 9c78131

Please sign in to comment.