Skip to content

Commit

Permalink
Rework tests to conditionally use modules
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Jul 28, 2024
1 parent 43b8193 commit 763241b
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ endif()

option(SEMVER_BUILD_TESTS "Enable builds of tests" ${TOP_PROJECT})
option(SEMVER_INSTALL "Enable install target" ${TOP_PROJECT})
option(SEMVER_BUILD_MODULE "Build as C++20 module" OFF)
option(SEMVER_BUILD_MODULE "Build as C++20 module" ON)

if (SEMVER_BUILD_MODULE)
cmake_minimum_required(VERSION 3.28)
Expand Down
3 changes: 2 additions & 1 deletion include/semver/semver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace semver
}
};

class prerelease_descriptor {
SEMVER_EXPORT class prerelease_descriptor {
private:
std::vector<prerelease_part> m_parts;
std::string prerelease_str;
Expand Down Expand Up @@ -403,6 +403,7 @@ namespace semver
#ifdef __cpp_lib_format
#if __cpp_lib_format >= 201907L

SEMVER_EXPORT
template <class CharT>
struct std::formatter<semver::version, CharT> : std::formatter<std::string_view> {
template <class FormatContext>
Expand Down
12 changes: 12 additions & 0 deletions module/semver.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SOFTWARE.

module;

#ifndef SEMVER_IMPORT_STD
#include <ostream>
#include <string>
#include <regex>
Expand All @@ -37,11 +38,16 @@ module;
#include <string_view>
#endif
#endif
#endif

export module semver;

#define SEMVER_MODULE

#ifdef SEMVER_IMPORT_STD
import std;
#endif

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 5244)
Expand All @@ -62,4 +68,10 @@ export module semver;

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#if defined(__cpp_lib_format)
export namespace std {
using std::formatter;
}
#endif
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ catch_discover_tests(${PROJECT_NAME}-tests-17)
# add c++20 tests
add_executable(${PROJECT_NAME}-tests-20 ${TEST_SRC})
if(SEMVER_BUILD_MODULE)
target_sources(${PROJECT_NAME}-tests-20 PRIVATE module/module_tests.cpp)
set_target_properties(${PROJECT_NAME}-tests-20 PROPERTIES CXX_SCAN_FOR_MODULES ON)
set_target_properties(${PROJECT_NAME}-tests-20 PROPERTIES CXX_EXTENSIONS OFF)
target_compile_definitions(${PROJECT_NAME}-tests-20 PRIVATE SEMVER_TEST_MODULE)
target_compile_definitions(${PROJECT_NAME}-tests-20 PRIVATE SEMVER_IMPORT_STD)
endif()
set_target_properties(${PROJECT_NAME}-tests-20 PROPERTIES CXX_STANDARD 20)
target_link_libraries(${PROJECT_NAME}-tests-20 PRIVATE Catch2::Catch2WithMain ${PROJECT_NAME})
Expand Down
5 changes: 5 additions & 0 deletions test/compare.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include <catch2/catch_test_macros.hpp>

#ifdef SEMVER_TEST_MODULE
import semver;
#else
#include <semver/semver.hpp>
#endif

TEST_CASE("Test version compare, less than, by numbers", "[version][compare]") {
semver::version v = semver::version::parse("5.2.3");
Expand Down
5 changes: 5 additions & 0 deletions test/increment.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>

#ifdef SEMVER_TEST_MODULE
import semver;
#else
#include <semver/semver.hpp>
#endif

TEST_CASE("Test version incrementation", "[version][increment]") {
semver::version v = semver::version::parse("1.2.3-alpha.4+build.3");
Expand Down
49 changes: 0 additions & 49 deletions test/module/module_tests.cpp

This file was deleted.

5 changes: 5 additions & 0 deletions test/prerelease.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include <catch2/catch_test_macros.hpp>

#ifdef SEMVER_TEST_MODULE
import semver;
#else
#include <semver/semver.hpp>
#endif

TEST_CASE("Test invalid pre-releases", "[prerelease]") {
REQUIRE_THROWS_AS(semver::prerelease_descriptor::parse(".alpha"), semver::semver_exception);
Expand Down
7 changes: 6 additions & 1 deletion test/serialization.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include <catch2/catch_test_macros.hpp>
#include <semver/semver.hpp>
#include <sstream>

#ifdef SEMVER_TEST_MODULE
import semver;
#else
#include <semver/semver.hpp>
#endif

TEST_CASE("Test version std::ostream <<", "[version]") {
semver::version v = semver::version::parse("5.2.3");
std::stringstream ss;
Expand Down
5 changes: 5 additions & 0 deletions test/version.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include <catch2/catch_test_macros.hpp>

#ifdef SEMVER_TEST_MODULE
import semver;
#else
#include <semver/semver.hpp>
#endif

using namespace semver::literals;

Expand Down

0 comments on commit 763241b

Please sign in to comment.