Skip to content

Commit 079af6c

Browse files
authored
CXX-2745 refactor test directory structure by ABI namespace (#1403)
* test: upgrade Catch2 from 3.7.0 to 3.8.1 * test: restructure test files by ABI namespace * test: add Catch2 string maker for std::error_condition * test: add equivalent to Catch::Detail::stringify * test: extract StringMaker specializations into separate headers * evg: avoid building example and test targets during CMake compat tasks * test: add StringMaker test cases for string_view and optional<T>
1 parent 39c9926 commit 079af6c

File tree

98 files changed

+546
-395
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+546
-395
lines changed

.clang-format

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,31 +161,35 @@ IncludeCategories:
161161
- Regex: 'mongocxx/v1/.*\.hh' # v1 internal headers
162162
Priority: 27
163163
- Regex: 'bsoncxx/private/.*' # private headers
164-
Priority: 60
164+
Priority: 70
165165
- Regex: 'mongocxx/private/.*' # private headers
166-
Priority: 61
166+
Priority: 71
167167
- Regex: 'bsoncxx/config/prelude\.(hpp|hh)' # v_noabi preludes
168-
Priority: 62
168+
Priority: 72
169169
- Regex: 'mongocxx/config/prelude\.(hpp|hh)' # v_noabi preludes
170-
Priority: 63
170+
Priority: 73
171+
- Regex: 'bsoncxx/test/(v.*|private)' # library test headers
172+
Priority: 30
173+
- Regex: 'mongocxx/test/(v.*|private)' # library test headers
174+
Priority: 31
171175
- Regex: 'bsoncxx/test/.*' # test headers
172-
Priority: 70
176+
Priority: 84
173177
- Regex: 'mongocxx/test/.*' # test headers
174-
Priority: 71
178+
Priority: 85
175179
- Regex: '<[[:alnum:]_.]+>' # system headers
176-
Priority: 30
177-
- Regex: 'bsoncxx/.*(-|\/)fwd\.(hpp|hh)' # all remaining forward headers
178180
Priority: 40
181+
- Regex: 'bsoncxx/.*(-|\/)fwd\.(hpp|hh)' # all remaining forward headers
182+
Priority: 50
179183
- Regex: 'mongocxx/.*(-|\/)fwd\.(hpp|hh)' # all remaining forward headers
180-
Priority: 41
184+
Priority: 51
181185
- Regex: 'bsoncxx/.*\.hpp' # all remaining public headers
182-
Priority: 50
186+
Priority: 60
183187
- Regex: 'mongocxx/.*\.hpp' # all remaining public headers
184-
Priority: 51
188+
Priority: 61
185189
- Regex: 'bsoncxx/.*\.hh' # all remaining internal headers
186-
Priority: 52
190+
Priority: 62
187191
- Regex: 'mongocxx/.*\.hh' # all remaining internal headers
188-
Priority: 53
192+
Priority: 63
189193
- Regex: '.*' # all other headers (third party)
190194
Priority: 90
191195
IncludeIsMainRegex: '([-_](test|unittest))?$'

.evergreen/scripts/cmake-compat-check.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ echo "Importing C++ Driver via add_subdirectory()..."
104104
cmake_minimum_required(VERSION ${CMAKE_MAJOR_VERSION:?}.${CMAKE_MINOR_VERSION:?})
105105
project(cmake-compat)
106106
107+
set(ENABLE_EXAMPLES OFF)
108+
set(ENABLE_TESTS OFF)
109+
107110
add_subdirectory(mongoc)
108111
add_subdirectory(mongo-cxx-driver)
109112

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Changes prior to 3.9.0 are documented as [release notes on GitHub](https://githu
1313

1414
- CMake option `ENABLE_TESTS` (`OFF` by default) is no longer overwritten by the auto-downloaded C Driver (`ON` by default) during CMake configuration.
1515

16+
### Changed
17+
18+
- 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).
19+
1620
## 4.1.0
1721

1822
### Fixed

cmake/FetchCatch2.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function(fetch_catch2)
1212
EP_Catch2
1313

1414
GIT_REPOSITORY https://github.com/catchorg/Catch2
15-
GIT_TAG v3.7.0
15+
GIT_TAG v3.8.1
1616
GIT_SHALLOW TRUE
1717
LOG_DOWNLOAD ON
1818

src/bsoncxx/test/CMakeLists.txt

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,49 @@ endif()
2626

2727
target_compile_definitions(bsoncxx_testing PUBLIC BSONCXX_TESTING)
2828

29-
add_executable(test_bson
30-
array.cpp
31-
bson_b_date.cpp
32-
bson_builder.cpp
33-
bson_get_values.cpp
34-
bson_serialization.cpp
35-
bson_types.cpp
36-
bson_util_itoa.cpp
37-
bson_validate.cpp
38-
bson_value.cpp
29+
set(bsoncxx_test_sources_private
30+
private/make_unique.test.cpp
31+
private/bson_version.cpp
32+
)
33+
34+
set(bsoncxx_test_sources_v_noabi
35+
v_noabi/array.cpp
36+
v_noabi/bson_b_date.cpp
37+
v_noabi/bson_builder.cpp
38+
v_noabi/bson_get_values.cpp
39+
v_noabi/bson_serialization.cpp
40+
v_noabi/bson_types.cpp
41+
v_noabi/bson_util_itoa.cpp
42+
v_noabi/bson_validate.cpp
43+
v_noabi/bson_value.cpp
44+
v_noabi/json.cpp
45+
v_noabi/oid.cpp
46+
v_noabi/vector.cpp
47+
v_noabi/view_or_value.cpp
48+
)
49+
50+
set(bsoncxx_test_sources_v1
51+
v1/stdx/optional.test.cpp
52+
v1/stdx/string_view.test.cpp
53+
v1/stdx/type_traits.test.cpp
54+
)
55+
56+
set(bsoncxx_test_sources
57+
${bsoncxx_test_sources_private}
58+
${bsoncxx_test_sources_v_noabi}
59+
${bsoncxx_test_sources_v1}
3960
catch.cpp
4061
exception_guard.cpp
41-
json.cpp
42-
oid.cpp
43-
vector.cpp
44-
version.cpp
45-
optional.test.cpp
46-
view_or_value.cpp
47-
make_unique.test.cpp
48-
string_view.test.cpp
49-
type_traits.test.cpp
62+
)
63+
64+
file(GLOB_RECURSE bsoncxx_test_headers
65+
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
66+
CONFIGURE_DEPENDS
67+
"*.hh"
68+
)
69+
70+
add_executable(test_bson
71+
${bsoncxx_test_sources}
5072
)
5173

5274
# Common target properties for test executables.
@@ -140,28 +162,7 @@ endif()
140162

141163
set_dist_list(src_bsoncxx_test_DIST
142164
CMakeLists.txt
143-
array.cpp
144-
bson_b_date.cpp
145-
bson_builder.cpp
146-
bson_get_values.cpp
147-
bson_serialization.cpp
148-
bson_types.cpp
149-
bson_util_itoa.cpp
150-
bson_validate.cpp
151-
bson_value.cpp
152-
catch.cpp
153-
catch.hh
154-
exception_guard.cpp
155-
exception_guard.hh
156-
json.cpp
157-
oid.cpp
158-
vector.cpp
159-
optional.test.cpp
165+
${bsoncxx_test_sources}
166+
${bsoncxx_test_headers}
160167
test_macro_guards.cpp.in
161-
to_string.hh
162-
view_or_value.cpp
163-
version.cpp
164-
make_unique.test.cpp
165-
string_view.test.cpp
166-
type_traits.test.cpp
167168
)

src/bsoncxx/test/catch.hh

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,24 @@
1616

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

19+
#include <bsoncxx/test/v1/stdx/optional.hh>
20+
#include <bsoncxx/test/v1/stdx/string_view.hh>
21+
#include <bsoncxx/test/v_noabi/to_string.hh>
22+
1923
#include <bsoncxx/document/view_or_value.hpp>
2024
#include <bsoncxx/json.hpp>
2125
#include <bsoncxx/oid.hpp>
2226
#include <bsoncxx/stdx/operators.hpp>
2327
#include <bsoncxx/stdx/optional.hpp>
2428

25-
#include <bsoncxx/test/to_string.hh>
29+
#include <bsoncxx/test/stringify.hh>
30+
#include <bsoncxx/test/system_error.hh>
2631

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

30-
#define THROWS_WITH_CODE_IMPL(_assertion, _expr, _code) \
31-
if (1) { \
32-
try { \
33-
(void)(_expr); \
34-
INFO("expected an exception to be thrown: " #_expr); \
35-
_assertion(false); \
36-
} catch (Catch::TestFailureException const&) { \
37-
throw; /* Propagate Catch exceptions. */ \
38-
} catch (Catch::TestSkipException const&) { \
39-
throw; /* Propagate Catch exceptions. */ \
40-
} catch (std::system_error const& ex) { \
41-
using std::make_error_code; \
42-
(void)ex; /* Avoid unused variable warnings. */ \
43-
_assertion(ex.code() == (_code)); \
44-
} catch (...) { \
45-
/* Reuse `*_THROWS_AS` to handle the unexpected exception type. */ \
46-
BSONCXX_PRIVATE_CONCAT(_assertion, _THROWS_AS)(throw, std::system_error); \
47-
} \
48-
} else \
49-
((void)0)
50-
51-
#define CHECK_THROWS_WITH_CODE(_expr, _code) THROWS_WITH_CODE_IMPL(CHECK, _expr, _code)
52-
#define REQUIRE_THROWS_WITH_CODE(_expr, _code) THROWS_WITH_CODE_IMPL(REQUIRE, _expr, _code)
53-
5435
namespace Catch {
5536

56-
template <>
57-
struct StringMaker<std::error_condition> {
58-
static std::string convert(std::error_condition const& value) {
59-
std::string res;
60-
61-
res += value.category().name();
62-
res += ':';
63-
res += Catch::StringMaker<int>::convert(value.value());
64-
65-
return res;
66-
}
67-
};
68-
6937
template <>
7038
struct StringMaker<bsoncxx::oid> {
7139
static std::string convert(bsoncxx::oid const& value) {
@@ -83,14 +51,14 @@ struct StringMaker<bsoncxx::document::view> {
8351
template <>
8452
struct StringMaker<bsoncxx::document::view_or_value> {
8553
static std::string convert(bsoncxx::document::view_or_value const& value) {
86-
return StringMaker<bsoncxx::document::view>::convert(value.view());
54+
return bsoncxx::test::stringify(value.view());
8755
}
8856
};
8957

9058
template <>
9159
struct StringMaker<bsoncxx::document::value> {
9260
static std::string convert(bsoncxx::document::value const& value) {
93-
return StringMaker<bsoncxx::document::view>::convert(value.view());
61+
return bsoncxx::test::stringify(value.view());
9462
}
9563
};
9664

@@ -105,32 +73,14 @@ struct StringMaker<bsoncxx::types::bson_value::view> {
10573
template <>
10674
struct StringMaker<bsoncxx::types::bson_value::value> {
10775
static std::string convert(bsoncxx::types::bson_value::value const& value) {
108-
return StringMaker<bsoncxx::types::bson_value::view>::convert(value.view());
76+
return bsoncxx::test::stringify(value.view());
10977
}
11078
};
11179

11280
template <>
11381
struct StringMaker<bsoncxx::types::bson_value::view_or_value> {
11482
static std::string convert(bsoncxx::types::bson_value::view_or_value const& value) {
115-
return StringMaker<bsoncxx::types::bson_value::view>::convert(value.view());
116-
}
117-
};
118-
119-
template <typename T>
120-
struct StringMaker<bsoncxx::stdx::optional<T>> {
121-
static std::string convert(bsoncxx::stdx::optional<T> const& value) {
122-
if (value) {
123-
return StringMaker<T>::convert(value.value());
124-
}
125-
126-
return "{nullopt}";
127-
}
128-
};
129-
130-
template <>
131-
struct StringMaker<bsoncxx::stdx::optional<bsoncxx::stdx::nullopt_t>> {
132-
static std::string convert(bsoncxx::stdx::optional<bsoncxx::stdx::nullopt_t> const&) {
133-
return "{nullopt}";
83+
return bsoncxx::test::stringify(value.view());
13484
}
13585
};
13686

src/bsoncxx/test/stringify.hh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2009-present MongoDB, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <string>
18+
#include <type_traits>
19+
20+
#include <catch2/catch_tostring.hpp>
21+
22+
namespace bsoncxx {
23+
namespace test {
24+
25+
// Equivalent to `Catch::Detail::stringify`.
26+
template <typename Param>
27+
std::string stringify(Param const& param) {
28+
using T = typename std::remove_cv<typename std::remove_reference<Param>::type>::type;
29+
return Catch::StringMaker<T>::convert(param);
30+
}
31+
32+
} // namespace test
33+
} // namespace bsoncxx

src/bsoncxx/test/system_error.hh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2009-present MongoDB, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <bsoncxx/v1/detail/macros.hpp>
18+
19+
#include <system_error>
20+
21+
#include <bsoncxx/test/stringify.hh>
22+
23+
#include <catch2/catch_test_macros.hpp>
24+
#include <catch2/catch_tostring.hpp>
25+
26+
#define THROWS_WITH_CODE_IMPL(_assertion, _expr, _code) \
27+
if (1) { \
28+
try { \
29+
(void)(_expr); \
30+
INFO("expected an exception to be thrown: " #_expr); \
31+
_assertion(false); \
32+
} catch (Catch::TestFailureException const&) { \
33+
throw; /* Propagate Catch exceptions. */ \
34+
} catch (Catch::TestSkipException const&) { \
35+
throw; /* Propagate Catch exceptions. */ \
36+
} catch (std::system_error const& ex) { \
37+
using std::make_error_code; \
38+
(void)ex; /* Avoid unused variable warnings. */ \
39+
_assertion(ex.code() == (_code)); \
40+
} catch (...) { \
41+
/* Reuse `*_THROWS_AS` to handle the unexpected exception type. */ \
42+
BSONCXX_PRIVATE_CONCAT(_assertion, _THROWS_AS)(throw, std::system_error); \
43+
} \
44+
} else \
45+
((void)0)
46+
47+
#define CHECK_THROWS_WITH_CODE(_expr, _code) THROWS_WITH_CODE_IMPL(CHECK, _expr, _code)
48+
#define REQUIRE_THROWS_WITH_CODE(_expr, _code) THROWS_WITH_CODE_IMPL(REQUIRE, _expr, _code)
49+
50+
template <>
51+
struct Catch::StringMaker<std::error_category> {
52+
static std::string convert(std::error_category const& value) {
53+
return value.name();
54+
}
55+
};
56+
57+
template <>
58+
struct Catch::StringMaker<std::error_condition> {
59+
static std::string convert(std::error_condition const& value) {
60+
std::string res;
61+
62+
res += value.category().name();
63+
res += ':';
64+
res += bsoncxx::test::stringify(value.value());
65+
66+
return res;
67+
}
68+
};

0 commit comments

Comments
 (0)
close