Skip to content

CXX-2745 refactor test directory structure by ABI namespace #1403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 17 additions & 13 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -161,31 +161,35 @@ IncludeCategories:
- Regex: 'mongocxx/v1/.*\.hh' # v1 internal headers
Priority: 27
- Regex: 'bsoncxx/private/.*' # private headers
Priority: 60
Priority: 70
- Regex: 'mongocxx/private/.*' # private headers
Priority: 61
Priority: 71
- Regex: 'bsoncxx/config/prelude\.(hpp|hh)' # v_noabi preludes
Priority: 62
Priority: 72
- Regex: 'mongocxx/config/prelude\.(hpp|hh)' # v_noabi preludes
Priority: 63
Priority: 73
- Regex: 'bsoncxx/test/(v.*|private)' # library test headers
Priority: 30
- Regex: 'mongocxx/test/(v.*|private)' # library test headers
Priority: 31
- Regex: 'bsoncxx/test/.*' # test headers
Priority: 70
Priority: 84
- Regex: 'mongocxx/test/.*' # test headers
Priority: 71
Priority: 85
- Regex: '<[[:alnum:]_.]+>' # system headers
Priority: 30
- Regex: 'bsoncxx/.*(-|\/)fwd\.(hpp|hh)' # all remaining forward headers
Priority: 40
- Regex: 'bsoncxx/.*(-|\/)fwd\.(hpp|hh)' # all remaining forward headers
Priority: 50
- Regex: 'mongocxx/.*(-|\/)fwd\.(hpp|hh)' # all remaining forward headers
Priority: 41
Priority: 51
- Regex: 'bsoncxx/.*\.hpp' # all remaining public headers
Priority: 50
Priority: 60
- Regex: 'mongocxx/.*\.hpp' # all remaining public headers
Priority: 51
Priority: 61
- Regex: 'bsoncxx/.*\.hh' # all remaining internal headers
Priority: 52
Priority: 62
- Regex: 'mongocxx/.*\.hh' # all remaining internal headers
Priority: 53
Priority: 63
- Regex: '.*' # all other headers (third party)
Priority: 90
IncludeIsMainRegex: '([-_](test|unittest))?$'
Expand Down
4 changes: 2 additions & 2 deletions .evergreen/config_generator/components/macro_guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def tasks():
Compile.call(
build_type='Debug',
compiler=compiler,
vars={'COMPILE_MACRO_GUARD_TESTS': 'ON'},
)
vars={'COMPILE_MACRO_GUARD_TESTS': 'ON', 'ENABLE_TESTS': 'ON'},
),
],
)
)
Expand Down
3 changes: 3 additions & 0 deletions .evergreen/generated_configs/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14187,6 +14187,7 @@ tasks:
- func: compile
vars:
COMPILE_MACRO_GUARD_TESTS: "ON"
ENABLE_TESTS: "ON"
build_type: Debug
- name: macro-guards-rhel80-clang
run_on: rhel80-large
Expand All @@ -14198,6 +14199,7 @@ tasks:
- func: compile
vars:
COMPILE_MACRO_GUARD_TESTS: "ON"
ENABLE_TESTS: "ON"
build_type: Debug
cc_compiler: clang
cxx_compiler: clang++
Expand All @@ -14211,6 +14213,7 @@ tasks:
- func: compile
vars:
COMPILE_MACRO_GUARD_TESTS: "ON"
ENABLE_TESTS: "ON"
build_type: Debug
cc_compiler: gcc
cxx_compiler: g++
Expand Down
3 changes: 3 additions & 0 deletions .evergreen/scripts/cmake-compat-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ echo "Importing C++ Driver via add_subdirectory()..."
cmake_minimum_required(VERSION ${CMAKE_MAJOR_VERSION:?}.${CMAKE_MINOR_VERSION:?})
project(cmake-compat)

set(ENABLE_EXAMPLES OFF)
set(ENABLE_TESTS OFF)

add_subdirectory(mongoc)
add_subdirectory(mongo-cxx-driver)

Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ Changes prior to 3.9.0 are documented as [release notes on GitHub](https://githu

## 4.2.0 [Unreleased]

<!-- Will contain entries for the next minor release. -->
### Fixed

- CMake option `ENABLE_TESTS` (`OFF` by default) is no longer overwritten by the auto-downloaded C Driver (`ON` by default) given [`CMAKE_REQUIRE_FIND_PACKAGE_<PackageName>`](https://cmake.org/cmake/help/latest/variable/CMAKE_REQUIRE_FIND_PACKAGE_PackageName.html).

### Changed

- CMake 3.16.0 or newer is required when `ENABLE_TESTS=ON` for compatibility with the updated Catch2 library version (3.7.0 -> 3.8.1).

## 4.1.0

Expand Down
2 changes: 1 addition & 1 deletion cmake/FetchCatch2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function(fetch_catch2)
EP_Catch2

GIT_REPOSITORY https://github.com/catchorg/Catch2
GIT_TAG v3.7.0
GIT_TAG v3.8.1
GIT_SHALLOW TRUE
LOG_DOWNLOAD ON

Expand Down
4 changes: 4 additions & 0 deletions cmake/FetchMongoC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function(fetch_mongoc)
set(OLD_CACHE_BUILD_VERSION $CACHE{BUILD_VERSION})
unset(BUILD_VERSION CACHE)
endif()
set(OLD_ENABLE_TESTS $CACHE{ENABLE_TESTS})

# Disable unnecessary targets and potential conflicts with C++ Driver options.
set(ENABLE_TESTS OFF)
Expand All @@ -42,6 +43,9 @@ function(fetch_mongoc)
if(DEFINED OLD_CACHE_BUILD_VERSION)
set(BUILD_VERSION ${OLD_CACHE_BUILD_VERSION} CACHE STRING "Library version (for both bsoncxx and mongocxx)")
endif()

# Restore prior value of ENABLE_TESTS cache variable after possibly being overwritten by C Driver configuration.
set_property(CACHE ENABLE_TESTS PROPERTY VALUE "${OLD_ENABLE_TESTS}")
endif()

message(STATUS "Downloading and configuring MongoDB C Driver ${MONGOC_DOWNLOAD_VERSION}... done.")
Expand Down
85 changes: 43 additions & 42 deletions src/bsoncxx/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,49 @@ endif()

target_compile_definitions(bsoncxx_testing PUBLIC BSONCXX_TESTING)

add_executable(test_bson
array.cpp
bson_b_date.cpp
bson_builder.cpp
bson_get_values.cpp
bson_serialization.cpp
bson_types.cpp
bson_util_itoa.cpp
bson_validate.cpp
bson_value.cpp
set(bsoncxx_test_sources_private
private/make_unique.test.cpp
private/bson_version.cpp
)

set(bsoncxx_test_sources_v_noabi
v_noabi/array.cpp
v_noabi/bson_b_date.cpp
v_noabi/bson_builder.cpp
v_noabi/bson_get_values.cpp
v_noabi/bson_serialization.cpp
v_noabi/bson_types.cpp
v_noabi/bson_util_itoa.cpp
v_noabi/bson_validate.cpp
v_noabi/bson_value.cpp
v_noabi/json.cpp
v_noabi/oid.cpp
v_noabi/vector.cpp
v_noabi/view_or_value.cpp
)

set(bsoncxx_test_sources_v1
v1/stdx/optional.test.cpp
v1/stdx/string_view.test.cpp
v1/stdx/type_traits.test.cpp
)

set(bsoncxx_test_sources
${bsoncxx_test_sources_private}
${bsoncxx_test_sources_v_noabi}
${bsoncxx_test_sources_v1}
catch.cpp
exception_guard.cpp
json.cpp
oid.cpp
vector.cpp
version.cpp
optional.test.cpp
view_or_value.cpp
make_unique.test.cpp
string_view.test.cpp
type_traits.test.cpp
)

file(GLOB_RECURSE bsoncxx_test_headers
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
CONFIGURE_DEPENDS
"*.hh"
)

add_executable(test_bson
${bsoncxx_test_sources}
)

# Common target properties for test executables.
Expand Down Expand Up @@ -140,28 +162,7 @@ endif()

set_dist_list(src_bsoncxx_test_DIST
CMakeLists.txt
array.cpp
bson_b_date.cpp
bson_builder.cpp
bson_get_values.cpp
bson_serialization.cpp
bson_types.cpp
bson_util_itoa.cpp
bson_validate.cpp
bson_value.cpp
catch.cpp
catch.hh
exception_guard.cpp
exception_guard.hh
json.cpp
oid.cpp
vector.cpp
optional.test.cpp
${bsoncxx_test_sources}
${bsoncxx_test_headers}
test_macro_guards.cpp.in
to_string.hh
view_or_value.cpp
version.cpp
make_unique.test.cpp
string_view.test.cpp
type_traits.test.cpp
)
70 changes: 10 additions & 60 deletions src/bsoncxx/test/catch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,24 @@

#include <bsoncxx/v1/detail/macros.hpp>

#include <bsoncxx/test/v1/stdx/optional.hh>
#include <bsoncxx/test/v1/stdx/string_view.hh>
#include <bsoncxx/test/v_noabi/to_string.hh>

#include <bsoncxx/document/view_or_value.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/oid.hpp>
#include <bsoncxx/stdx/operators.hpp>
#include <bsoncxx/stdx/optional.hpp>

#include <bsoncxx/test/to_string.hh>
#include <bsoncxx/test/stringify.hh>
#include <bsoncxx/test/system_error.hh>

#include <catch2/catch_test_macros.hpp> // TEST_CASE, SECTION, CHECK, etc.
#include <catch2/catch_tostring.hpp> // Catch::StringMaker

#define THROWS_WITH_CODE_IMPL(_assertion, _expr, _code) \
if (1) { \
try { \
(void)(_expr); \
INFO("expected an exception to be thrown: " #_expr); \
_assertion(false); \
} catch (Catch::TestFailureException const&) { \
throw; /* Propagate Catch exceptions. */ \
} catch (Catch::TestSkipException const&) { \
throw; /* Propagate Catch exceptions. */ \
} catch (std::system_error const& ex) { \
using std::make_error_code; \
(void)ex; /* Avoid unused variable warnings. */ \
_assertion(ex.code() == (_code)); \
} catch (...) { \
/* Reuse `*_THROWS_AS` to handle the unexpected exception type. */ \
BSONCXX_PRIVATE_CONCAT(_assertion, _THROWS_AS)(throw, std::system_error); \
} \
} else \
((void)0)

#define CHECK_THROWS_WITH_CODE(_expr, _code) THROWS_WITH_CODE_IMPL(CHECK, _expr, _code)
#define REQUIRE_THROWS_WITH_CODE(_expr, _code) THROWS_WITH_CODE_IMPL(REQUIRE, _expr, _code)

namespace Catch {

template <>
struct StringMaker<std::error_condition> {
static std::string convert(std::error_condition const& value) {
std::string res;

res += value.category().name();
res += ':';
res += Catch::StringMaker<int>::convert(value.value());

return res;
}
};

template <>
struct StringMaker<bsoncxx::oid> {
static std::string convert(bsoncxx::oid const& value) {
Expand All @@ -83,14 +51,14 @@ struct StringMaker<bsoncxx::document::view> {
template <>
struct StringMaker<bsoncxx::document::view_or_value> {
static std::string convert(bsoncxx::document::view_or_value const& value) {
return StringMaker<bsoncxx::document::view>::convert(value.view());
return bsoncxx::test::stringify(value.view());
}
};

template <>
struct StringMaker<bsoncxx::document::value> {
static std::string convert(bsoncxx::document::value const& value) {
return StringMaker<bsoncxx::document::view>::convert(value.view());
return bsoncxx::test::stringify(value.view());
}
};

Expand All @@ -105,32 +73,14 @@ struct StringMaker<bsoncxx::types::bson_value::view> {
template <>
struct StringMaker<bsoncxx::types::bson_value::value> {
static std::string convert(bsoncxx::types::bson_value::value const& value) {
return StringMaker<bsoncxx::types::bson_value::view>::convert(value.view());
return bsoncxx::test::stringify(value.view());
}
};

template <>
struct StringMaker<bsoncxx::types::bson_value::view_or_value> {
static std::string convert(bsoncxx::types::bson_value::view_or_value const& value) {
return StringMaker<bsoncxx::types::bson_value::view>::convert(value.view());
}
};

template <typename T>
struct StringMaker<bsoncxx::stdx::optional<T>> {
static std::string convert(bsoncxx::stdx::optional<T> const& value) {
if (value) {
return StringMaker<T>::convert(value.value());
}

return "{nullopt}";
}
};

template <>
struct StringMaker<bsoncxx::stdx::optional<bsoncxx::stdx::nullopt_t>> {
static std::string convert(bsoncxx::stdx::optional<bsoncxx::stdx::nullopt_t> const&) {
return "{nullopt}";
return bsoncxx::test::stringify(value.view());
}
};

Expand Down
33 changes: 33 additions & 0 deletions src/bsoncxx/test/stringify.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2009-present MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <string>
#include <type_traits>

#include <catch2/catch_tostring.hpp>

namespace bsoncxx {
namespace test {

// Equivalent to `Catch::Detail::stringify`.
template <typename Param>
std::string stringify(Param const& param) {
using T = typename std::remove_cv<typename std::remove_reference<Param>::type>::type;
return Catch::StringMaker<T>::convert(param);
}

} // namespace test
} // namespace bsoncxx
Loading