Skip to content

Commit d07b7fe

Browse files
authored
Small cleanup for mock tests. (dmlc#10085)
1 parent 7a61216 commit d07b7fe

File tree

5 files changed

+33
-50
lines changed

5 files changed

+33
-50
lines changed

tests/cpp/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ if(PLUGIN_SYCL)
2929
${xgboost_SOURCE_DIR}/rabit/include)
3030

3131
target_compile_definitions(plugin_sycl_test PUBLIC -DXGBOOST_USE_SYCL=1)
32-
3332
target_link_libraries(plugin_sycl_test PUBLIC -fsycl)
33+
target_link_libraries(plugin_sycl_test PRIVATE ${GTEST_LIBRARIES})
3434

3535
set_target_properties(plugin_sycl_test PROPERTIES
36-
COMPILE_FLAGS -fsycl
37-
CXX_STANDARD 17
38-
CXX_STANDARD_REQUIRED ON
39-
POSITION_INDEPENDENT_CODE ON)
36+
COMPILE_FLAGS -fsycl
37+
CXX_STANDARD 17
38+
CXX_STANDARD_REQUIRED ON
39+
POSITION_INDEPENDENT_CODE ON)
4040
if(USE_OPENMP)
4141
find_package(OpenMP REQUIRED)
4242
set_target_properties(plugin_sycl_test PROPERTIES

tests/cpp/data/test_metainfo.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
// Copyright 2016-2021 by Contributors
1+
/**
2+
* Copyright 2016-2024, XGBoost contributors
3+
*/
24
#include "test_metainfo.h"
35

6+
#include <gmock/gmock.h>
47
#include <dmlc/io.h>
58
#include <xgboost/data.h>
69

@@ -9,7 +12,7 @@
912

1013
#include "../../../src/common/version.h"
1114
#include "../filesystem.h" // dmlc::TemporaryDirectory
12-
#include "../helpers.h"
15+
#include "../helpers.h" // for GMockTHrow
1316
#include "xgboost/base.h"
1417

1518
namespace xgboost {
@@ -46,6 +49,8 @@ TEST(MetaInfo, GetSet) {
4649

4750
TEST(MetaInfo, GetSetFeature) {
4851
xgboost::MetaInfo info;
52+
ASSERT_THAT([&] { info.SetFeatureInfo("", nullptr, 0); },
53+
GMockThrow("Unknown feature info name"));
4954
EXPECT_THROW(info.SetFeatureInfo("", nullptr, 0), dmlc::Error);
5055
EXPECT_THROW(info.SetFeatureInfo("foo", nullptr, 0), dmlc::Error);
5156
EXPECT_NO_THROW(info.SetFeatureInfo("feature_name", nullptr, 0));
@@ -86,7 +91,8 @@ void VerifyGetSetFeatureColumnSplit() {
8691
std::transform(types.cbegin(), types.cend(), c_types.begin(),
8792
[](auto const &str) { return str.c_str(); });
8893
info.num_col_ = kCols;
89-
EXPECT_THROW(info.SetFeatureInfo(u8"feature_type", c_types.data(), c_types.size()), dmlc::Error);
94+
ASSERT_THAT([&] { info.SetFeatureInfo(u8"feature_type", c_types.data(), c_types.size()); },
95+
GMockThrow("Length of feature_type must be equal to number of columns"));
9096
info.num_col_ = kCols * world_size;
9197
EXPECT_NO_THROW(info.SetFeatureInfo(u8"feature_type", c_types.data(), c_types.size()));
9298
std::vector<std::string> expected_type_names{u8"float", u8"c", u8"float",
@@ -103,7 +109,8 @@ void VerifyGetSetFeatureColumnSplit() {
103109
std::transform(names.cbegin(), names.cend(), c_names.begin(),
104110
[](auto const &str) { return str.c_str(); });
105111
info.num_col_ = kCols;
106-
EXPECT_THROW(info.SetFeatureInfo(u8"feature_name", c_names.data(), c_names.size()), dmlc::Error);
112+
ASSERT_THAT([&] { info.SetFeatureInfo(u8"feature_name", c_names.data(), c_names.size()); },
113+
GMockThrow("Length of feature_name must be equal to number of columns"));
107114
info.num_col_ = kCols * world_size;
108115
EXPECT_NO_THROW(info.SetFeatureInfo(u8"feature_name", c_names.data(), c_names.size()));
109116
std::vector<std::string> expected_names{u8"0.feature0", u8"0.feature1", u8"1.feature0",

tests/cpp/helpers.h

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
2-
* Copyright 2016-2024 by XGBoost contributors
2+
* Copyright 2016-2024, XGBoost contributors
33
*/
44
#pragma once
55

6+
#include <gmock/gmock.h>
67
#include <gtest/gtest.h>
78
#include <sys/stat.h>
89
#include <sys/types.h>
@@ -12,7 +13,7 @@
1213
#include <xgboost/learner.h> // for LearnerModelParam
1314
#include <xgboost/model.h> // for Configurable
1415

15-
#include <cstdint> // std::int32_t
16+
#include <cstdint> // std::int32_t
1617
#include <cstdio>
1718
#include <fstream>
1819
#include <iostream>
@@ -573,30 +574,7 @@ class DeclareUnifiedDistributedTest(MetricTest) : public BaseMGPUTest{};
573574

574575
inline DeviceOrd FstCU() { return DeviceOrd::CUDA(0); }
575576

576-
/**
577-
* @brief poor man's gmock for message matching.
578-
*
579-
* @tparam Error The type of expected execption.
580-
*
581-
* @param submsg A substring of the actual error message.
582-
* @param fn The function that throws Error
583-
*/
584-
template <typename Error, typename Fn>
585-
void ExpectThrow(std::string submsg, Fn&& fn) {
586-
try {
587-
fn();
588-
} catch (Error const& exc) {
589-
auto actual = std::string{exc.what()};
590-
ASSERT_NE(actual.find(submsg), std::string::npos)
591-
<< "Expecting substring `" << submsg << "` from the error message."
592-
<< " Got:\n"
593-
<< actual << "\n";
594-
return;
595-
} catch (std::exception const& exc) {
596-
auto actual = exc.what();
597-
ASSERT_TRUE(false) << "An unexpected type of exception is thrown. what:" << actual;
598-
return;
599-
}
600-
ASSERT_TRUE(false) << "No exception is thrown";
577+
inline auto GMockThrow(StringView msg) {
578+
return ::testing::ThrowsMessage<dmlc::Error>(::testing::HasSubstr(msg));
601579
}
602580
} // namespace xgboost

tests/cpp/plugin/federated/test_federated_comm.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022-2023, XGBoost contributors
2+
* Copyright 2022-2024, XGBoost contributors
33
*/
44
#include <gmock/gmock.h>
55
#include <gtest/gtest.h>
@@ -9,7 +9,7 @@
99

1010
#include "../../../../plugin/federated/federated_comm.h"
1111
#include "../../collective/test_worker.h" // for SocketTest
12-
#include "../../helpers.h" // for ExpectThrow
12+
#include "../../helpers.h" // for GMockThrow
1313
#include "test_worker.h" // for TestFederated
1414
#include "xgboost/json.h" // for Json
1515

@@ -20,19 +20,19 @@ class FederatedCommTest : public SocketTest {};
2020

2121
TEST_F(FederatedCommTest, ThrowOnWorldSizeTooSmall) {
2222
auto construct = [] { FederatedComm comm{"localhost", 0, 0, 0}; };
23-
ASSERT_THAT(construct,
24-
::testing::ThrowsMessage<dmlc::Error>(::testing::HasSubstr("Invalid world size")));
23+
ASSERT_THAT(construct, GMockThrow("Invalid world size"));
2524
}
2625

2726
TEST_F(FederatedCommTest, ThrowOnRankTooSmall) {
2827
auto construct = [] { FederatedComm comm{"localhost", 0, 1, -1}; };
29-
ASSERT_THAT(construct,
30-
::testing::ThrowsMessage<dmlc::Error>(::testing::HasSubstr("Invalid worker rank.")));
28+
ASSERT_THAT(construct, GMockThrow("Invalid worker rank."));
3129
}
3230

3331
TEST_F(FederatedCommTest, ThrowOnRankTooBig) {
34-
auto construct = [] { FederatedComm comm{"localhost", 0, 1, 1}; };
35-
ExpectThrow<dmlc::Error>("Invalid worker rank.", construct);
32+
auto construct = [] {
33+
FederatedComm comm{"localhost", 0, 1, 1};
34+
};
35+
ASSERT_THAT(construct, GMockThrow("Invalid worker rank."));
3636
}
3737

3838
TEST_F(FederatedCommTest, ThrowOnWorldSizeNotInteger) {
@@ -43,7 +43,7 @@ TEST_F(FederatedCommTest, ThrowOnWorldSizeNotInteger) {
4343
config["federated_rank"] = Integer(0);
4444
FederatedComm comm{DefaultRetry(), std::chrono::seconds{DefaultTimeoutSec()}, "", config};
4545
};
46-
ExpectThrow<dmlc::Error>("got: `String`", construct);
46+
ASSERT_THAT(construct, GMockThrow("got: `String`"));
4747
}
4848

4949
TEST_F(FederatedCommTest, ThrowOnRankNotInteger) {
@@ -54,7 +54,7 @@ TEST_F(FederatedCommTest, ThrowOnRankNotInteger) {
5454
config["federated_rank"] = std::string("0");
5555
FederatedComm comm(DefaultRetry(), std::chrono::seconds{DefaultTimeoutSec()}, "", config);
5656
};
57-
ExpectThrow<dmlc::Error>("got: `String`", construct);
57+
ASSERT_THAT(construct, GMockThrow("got: `String`"));
5858
}
5959

6060
TEST_F(FederatedCommTest, GetWorldSizeAndRank) {

tests/cpp/test_learner.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2017-2023, XGBoost contributors
2+
* Copyright 2017-2024, XGBoost contributors
33
*/
44
#include <gtest/gtest.h>
55
#include <gmock/gmock.h>
@@ -82,9 +82,7 @@ TEST(Learner, ParameterValidation) {
8282

8383
// whitespace
8484
learner->SetParam("tree method", "exact");
85-
EXPECT_THAT([&] { learner->Configure(); },
86-
::testing::ThrowsMessage<dmlc::Error>(
87-
::testing::HasSubstr(R"("tree method" contains whitespace)")));
85+
ASSERT_THAT([&] { learner->Configure(); }, GMockThrow(R"("tree method" contains whitespace)"));
8886
}
8987

9088
TEST(Learner, CheckGroup) {

0 commit comments

Comments
 (0)