Skip to content

Commit cd801ca

Browse files
committed
fix: address clang-tidy warnings in unit tests
1 parent 37c3b1a commit cd801ca

6 files changed

Lines changed: 238 additions & 199 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,15 @@ endif()
271271

272272
if(SDBUSCPP_CLANG_TIDY)
273273
message(STATUS "Building with static analysis")
274-
#set(CLANG_TIDY "/home/one/CLion2/clion-2024.3.5/bin/clang/linux/x64/bin/clang-tidy")
275274
find_program(CLANG_TIDY NAMES clang-tidy)
276275
if(NOT CLANG_TIDY)
277276
message(WARNING "clang-tidy not found")
278277
else()
279278
message(STATUS "clang-tidy found: ${CLANG_TIDY}")
279+
# TODO: Check what happens if the targets don't exist
280280
set_target_properties(sdbus-c++-objlib PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY}")
281281
set_target_properties(sdbus-c++ PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY}")
282-
#set_target_properties(sdbus-c++-unit-tests PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY}")
282+
set_target_properties(sdbus-c++-unit-tests PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY}")
283283
#set_target_properties(sdbus-c++-integration-tests PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY}")
284284
set_target_properties(sdbus-c++-stress-tests PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY}")
285285
endif()

tests/unittests/Connection_test.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@
2626
*/
2727

2828
#include "Connection.h"
29+
#include "sdbus-c++/Error.h"
2930
#include "sdbus-c++/Types.h"
3031
#include "unittests/mocks/SdBusMock.h"
3132

32-
#include <gtest/gtest.h>
33+
#include <gtest/gtest.h> // IWYU pragma: export
34+
#include <cerrno>
35+
#include <memory>
36+
#include <utility>
37+
38+
// NOLINTBEGIN(cppcoreguidelines-non-private-member-variables-in-classes,misc-non-private-member-variables-in-classes)
39+
// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
3340

3441
using ::testing::_;
3542
using ::testing::DoAll;
@@ -194,30 +201,33 @@ template<> std::unique_ptr<Connection> AConnectionNameRequest<Connection::remote
194201
return std::make_unique<Connection>(std::unique_ptr<NiceMock<SdBusMock>>(sdBusIntfMock_), Connection::remote_system_bus, "some host");
195202
}
196203

197-
typedef ::testing::Types< Connection::default_bus_t
198-
, Connection::system_bus_t
199-
, Connection::session_bus_t
200-
, Connection::custom_session_bus_t
201-
, Connection::remote_system_bus_t
202-
, Connection::pseudo_bus_t
203-
> BusTypeTags;
204+
using BusTypeTags = ::testing::Types< Connection::default_bus_t
205+
, Connection::system_bus_t
206+
, Connection::session_bus_t
207+
, Connection::custom_session_bus_t
208+
, Connection::remote_system_bus_t
209+
, Connection::pseudo_bus_t
210+
>;
204211

205212
TYPED_TEST_SUITE(AConnectionNameRequest, BusTypeTags);
206-
}
213+
} // namespace
207214

208215
TYPED_TEST(AConnectionNameRequest, DoesNotThrowOnSuccess)
209216
{
210217
EXPECT_CALL(*this->sdBusIntfMock_, sd_bus_request_name(_, _, _)).WillOnce(Return(1));
211-
sdbus::ConnectionName name{"org.sdbuscpp.somename"};
218+
const sdbus::ConnectionName name{"org.sdbuscpp.somename"};
212219

213220
this->con_->requestName(name);
214221
}
215222

216223
TYPED_TEST(AConnectionNameRequest, ThrowsOnFail)
217224
{
218-
sdbus::ConnectionName name{"org.sdbuscpp.somename"};
225+
const sdbus::ConnectionName name{"org.sdbuscpp.somename"};
219226

220227
EXPECT_CALL(*this->sdBusIntfMock_, sd_bus_request_name(_, _, _)).WillOnce(Return(-1));
221228

222229
ASSERT_THROW(this->con_->requestName(name), sdbus::Error);
223230
}
231+
232+
// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
233+
// NOLINTEND(cppcoreguidelines-non-private-member-variables-in-classes,misc-non-private-member-variables-in-classes)

tests/unittests/Message_test.cpp

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,25 @@
2424
* along with sdbus-c++. If not, see <http://www.gnu.org/licenses/>.
2525
*/
2626

27+
#include <sdbus-c++/Error.h>
28+
#include <sdbus-c++/Message.h>
2729
#include <sdbus-c++/Types.h>
28-
#include "MessageUtils.h"
30+
#include <sdbus-c++/TypeTraits.h>
2931
#include <gtest/gtest.h>
3032
#include <gmock/gmock.h>
33+
#include <array>
3134
#include <cstdint>
3235
#include <list>
36+
#include <map>
37+
#include <span>
38+
#include <string>
39+
#include <string_view>
40+
#include <variant>
41+
#include <vector>
3342

3443
using ::testing::Eq;
3544
using ::testing::StrEq;
3645
using ::testing::Gt;
37-
using ::testing::DoubleEq;
3846
using ::testing::IsNull;
3947
using ::testing::SizeIs;
4048
using ::testing::ElementsAre;
@@ -48,15 +56,15 @@ namespace
4856
msg >> str;
4957
return str;
5058
}
51-
}
59+
} // namespace
5260

5361
namespace sdbus {
5462

55-
template <typename _ElementType>
56-
sdbus::Message& operator<<(sdbus::Message& msg, const std::list<_ElementType>& items)
63+
template <typename ElementType>
64+
sdbus::Message& operator<<(sdbus::Message& msg, const std::list<ElementType>& items)
5765
{
5866
// TODO: This can also be simplified on the basis of a callback (see dictionary...)
59-
msg.openContainer<_ElementType>();
67+
msg.openContainer<ElementType>();
6068

6169
for (const auto& item : items)
6270
msg << item;
@@ -66,15 +74,15 @@ namespace sdbus {
6674
return msg;
6775
}
6876

69-
template <typename _ElementType>
70-
sdbus::Message& operator>>(sdbus::Message& msg, std::list<_ElementType>& items)
77+
template <typename ElementType>
78+
sdbus::Message& operator>>(sdbus::Message& msg, std::list<ElementType>& items)
7179
{
72-
if(!msg.enterContainer<_ElementType>())
80+
if(!msg.enterContainer<ElementType>())
7381
return msg;
7482

7583
while (true)
7684
{
77-
_ElementType elem;
85+
ElementType elem;
7886
if (msg >> elem)
7987
items.emplace_back(std::move(elem));
8088
else
@@ -88,15 +96,15 @@ namespace sdbus {
8896
return msg;
8997
}
9098

91-
}
99+
} // namespace sdbus
92100

93-
template <typename _Element, typename _Allocator>
94-
struct sdbus::signature_of<std::list<_Element, _Allocator>>
95-
: sdbus::signature_of<std::vector<_Element, _Allocator>>
101+
template <typename Element, typename Allocator>
102+
struct sdbus::signature_of<std::list<Element, Allocator>>
103+
: sdbus::signature_of<std::vector<Element, Allocator>>
96104
{};
97105

98106
namespace my {
99-
enum class Enum
107+
enum class Enum : std::uint8_t
100108
{
101109
Value1,
102110
Value2,
@@ -105,42 +113,42 @@ namespace my {
105113

106114
struct Struct
107115
{
108-
int i;
116+
int i{};
109117
std::string s;
110118
std::list<double> l;
111-
Enum e;
119+
Enum e{};
112120

113121
friend bool operator==(const Struct& lhs, const Struct& rhs) = default;
114122
};
115123

116124
struct RelaxedStruct
117125
{
118-
int i;
126+
int i{};
119127
std::string s;
120128
std::list<double> l;
121-
Enum e;
129+
Enum e{};
122130

123131
friend bool operator==(const RelaxedStruct& lhs, const RelaxedStruct& rhs) = default;
124132
};
125133

126134
struct NestedStruct
127135
{
128-
int i;
136+
int i{};
129137
std::string s;
130-
Enum e;
138+
Enum e{};
131139
Struct x;
132140

133141
friend bool operator==(const NestedStruct& lhs, const NestedStruct& rhs) = default;
134142
};
135-
}
143+
} // namespace my
136144

137-
SDBUSCPP_REGISTER_STRUCT(my::Struct, i, s, l, e);
145+
SDBUSCPP_REGISTER_STRUCT(my::Struct, i, s, l, e); // NOLINT(readability-identifier-length)
138146

139147
SDBUSCPP_ENABLE_RELAXED_DICT2STRUCT_DESERIALIZATION(my::RelaxedStruct);
140-
SDBUSCPP_REGISTER_STRUCT(my::RelaxedStruct, i, s, l, e);
148+
SDBUSCPP_REGISTER_STRUCT(my::RelaxedStruct, i, s, l, e); // NOLINT(readability-identifier-length)
141149

142150
SDBUSCPP_ENABLE_NESTED_STRUCT2DICT_SERIALIZATION(my::NestedStruct);
143-
SDBUSCPP_REGISTER_STRUCT(my::NestedStruct, i, s, e, x);
151+
SDBUSCPP_REGISTER_STRUCT(my::NestedStruct, i, s, e, x); // NOLINT(readability-identifier-length)
144152

145153
/*-------------------------------------*/
146154
/* -- TEST CASES -- */
@@ -153,7 +161,7 @@ TEST(AMessage, CanBeDefaultConstructed)
153161

154162
TEST(AMessage, IsInvalidAfterDefaultConstructed)
155163
{
156-
sdbus::PlainMessage msg;
164+
const sdbus::PlainMessage msg;
157165

158166
ASSERT_FALSE(msg.isValid());
159167
}
@@ -218,7 +226,7 @@ TEST(AMessage, CanCarryASimpleInteger)
218226
msg << dataWritten;
219227
msg.seal();
220228

221-
int dataRead;
229+
int dataRead{};
222230
msg >> dataRead;
223231

224232
ASSERT_THAT(dataRead, Eq(dataWritten));
@@ -258,7 +266,7 @@ TEST(AMessage, CanCarryAVariant)
258266
{
259267
auto msg = sdbus::createPlainMessage();
260268

261-
const auto dataWritten = sdbus::Variant((double)3.14);
269+
const auto dataWritten = sdbus::Variant(3.14);
262270

263271
msg << dataWritten;
264272
msg.seal();
@@ -273,7 +281,7 @@ TEST(AMessage, CanCarryACollectionOfEmbeddedVariants)
273281
{
274282
auto msg = sdbus::createPlainMessage();
275283

276-
std::vector<sdbus::Variant> value{sdbus::Variant{"hello"s}, sdbus::Variant{(double)3.14}};
284+
std::vector<sdbus::Variant> value{sdbus::Variant{"hello"s}, sdbus::Variant{3.14}};
277285
const auto dataWritten = sdbus::Variant{value};
278286

279287
msg << dataWritten;
@@ -320,12 +328,12 @@ TEST(AMessage, CanCarryDBusArrayOfTrivialTypesGivenAsStdArray)
320328
{
321329
auto msg = sdbus::createPlainMessage();
322330

323-
const std::array<int, 3> dataWritten{3545342, 43643532, 324325};
331+
const std::array dataWritten{3545342, 43643532, 324325};
324332

325333
msg << dataWritten;
326334
msg.seal();
327335

328-
std::array<int, 3> dataRead;
336+
std::array<int, 3> dataRead{};
329337
msg >> dataRead;
330338

331339
ASSERT_THAT(dataRead, Eq(dataWritten));
@@ -351,13 +359,13 @@ TEST(AMessage, CanCarryDBusArrayOfTrivialTypesGivenAsStdSpan)
351359
{
352360
auto msg = sdbus::createPlainMessage();
353361

354-
const std::array<int, 3> sourceArray{3545342, 43643532, 324325};
362+
const std::array sourceArray{3545342, 43643532, 324325};
355363
const std::span dataWritten{sourceArray};
356364

357365
msg << dataWritten;
358366
msg.seal();
359367

360-
std::array<int, 3> destinationArray;
368+
std::array<int, 3> destinationArray{};
361369
std::span dataRead{destinationArray};
362370
msg >> dataRead;
363371

@@ -386,8 +394,8 @@ TEST(AMessage, CanCarryAnEnumValue)
386394
{
387395
auto msg = sdbus::createPlainMessage();
388396

389-
enum class EnumA : int16_t {X = 5} aWritten{EnumA::X};
390-
enum EnumB {Y = 11} bWritten{EnumB::Y};
397+
const enum class EnumA : int16_t {X = 5} aWritten{EnumA::X}; // NOLINT(performance-enum-size)
398+
const enum EnumB {Y = 11} bWritten{EnumB::Y}; // NOLINT(performance-enum-size)
391399

392400
msg << aWritten << bWritten;
393401
msg.seal();
@@ -409,7 +417,7 @@ TEST(AMessage, ThrowsWhenDestinationStdArrayIsTooSmallDuringDeserialization)
409417
msg << dataWritten;
410418
msg.seal();
411419

412-
std::array<int, 3> dataRead;
420+
std::array<int, 3> dataRead{};
413421
ASSERT_THROW(msg >> dataRead, sdbus::Error);
414422
}
415423

@@ -423,7 +431,7 @@ TEST(AMessage, ThrowsWhenDestinationStdSpanIsTooSmallDuringDeserialization)
423431
msg << dataWritten;
424432
msg.seal();
425433

426-
std::array<int, 2> destinationArray;
434+
std::array<int, 2> destinationArray{};
427435
std::span dataRead{destinationArray};
428436
ASSERT_THROW(msg >> dataRead, sdbus::Error);
429437
}
@@ -433,7 +441,7 @@ TEST(AMessage, CanCarryADictionary)
433441
{
434442
auto msg = sdbus::createPlainMessage();
435443

436-
std::map<int, std::string> dataWritten{{1, "one"}, {2, "two"}};
444+
const std::map<int, std::string> dataWritten{{1, "one"}, {2, "two"}};
437445

438446
msg << dataWritten;
439447
msg.seal();
@@ -468,7 +476,7 @@ TEST(AMessage, CanCarryAComplexType)
468476
>
469477
>;
470478

471-
ComplexType dataWritten = { {1, {{{5, {{sdbus::ObjectPath{"/some/object"}, true, 45, {{6, "hello"}, {7, "world"}}}}}}, sdbus::Signature{"av"}, 3.14}}};
479+
const ComplexType dataWritten = { {1, {{{5, {{sdbus::ObjectPath{"/some/object"}, true, 45, {{6, "hello"}, {7, "world"}}}}}}, sdbus::Signature{"av"}, 3.14}}};
472480

473481
msg << dataWritten;
474482
msg.seal();
@@ -596,10 +604,10 @@ TEST(AMessage, CanDeserializeDictionaryOfStringsToVariantsIntoUserDefinedStruct)
596604
{
597605
auto msg = sdbus::createPlainMessage();
598606

599-
std::map<std::string, sdbus::Variant> dataWritten{ {"i", sdbus::Variant{3545342}}
600-
, {"s", sdbus::Variant{"hello"s}}
601-
, {"l", sdbus::Variant{std::list<double>{3.14, 2.4568546}}}
602-
, {"e", sdbus::Variant{my::Enum::Value2}} };
607+
const std::map<std::string, sdbus::Variant> dataWritten{ {"i", sdbus::Variant{3545342}}
608+
, {"s", sdbus::Variant{"hello"s}}
609+
, {"l", sdbus::Variant{std::list<double>{3.14, 2.4568546}}}
610+
, {"e", sdbus::Variant{my::Enum::Value2}} };
603611

604612
msg << dataWritten;
605613
msg.seal();
@@ -614,10 +622,10 @@ TEST(AMessage, FailsDeserializingDictionaryIntoUserDefinedStructIfStructMemberIs
614622
{
615623
auto msg = sdbus::createPlainMessage();
616624

617-
std::map<std::string, sdbus::Variant> dataWritten{ {"i", sdbus::Variant{3545342}}
618-
, {"nonexistent", sdbus::Variant{"hello"s}}
619-
, {"l", sdbus::Variant{std::list<double>{3.14, 2.4568546}}}
620-
, {"e", sdbus::Variant{my::Enum::Value2}} };
625+
const std::map<std::string, sdbus::Variant> dataWritten{ {"i", sdbus::Variant{3545342}}
626+
, {"nonexistent", sdbus::Variant{"hello"s}}
627+
, {"l", sdbus::Variant{std::list<double>{3.14, 2.4568546}}}
628+
, {"e", sdbus::Variant{my::Enum::Value2}} };
621629

622630
msg << dataWritten;
623631
msg.seal();
@@ -631,10 +639,10 @@ TEST(AMessage, DeserializesDictionaryIntoStructWithMissingMembersSuccessfullyIfR
631639
{
632640
auto msg = sdbus::createPlainMessage();
633641

634-
std::map<std::string, sdbus::Variant> dataWritten{ {"some_nonexistent_struct_member", sdbus::Variant{3545342}}
635-
, {"another_nonexistent_struct_member", sdbus::Variant{"hello"s}}
636-
, {"l", sdbus::Variant{std::list<double>{3.14, 2.4568546}}}
637-
, {"e", sdbus::Variant{my::Enum::Value2}} };
642+
const std::map<std::string, sdbus::Variant> dataWritten{ {"some_nonexistent_struct_member", sdbus::Variant{3545342}}
643+
, {"another_nonexistent_struct_member", sdbus::Variant{"hello"s}}
644+
, {"l", sdbus::Variant{std::list<double>{3.14, 2.4568546}}}
645+
, {"e", sdbus::Variant{my::Enum::Value2}} };
638646

639647
msg << dataWritten;
640648
msg.seal();

0 commit comments

Comments
 (0)