Skip to content

Commit 4537e9e

Browse files
committed
refactor GetNamespace tests and implementation to standardize variable naming and trim type prefixes
1 parent 4375000 commit 4537e9e

3 files changed

Lines changed: 29 additions & 20 deletions

File tree

modules/core/performance/tests/perf_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,14 @@ using TestTypes = ::testing::Types<my::nested::Type, my::Another, int>;
263263
TYPED_TEST_SUITE(GetNamespaceTest, TestTypes);
264264

265265
TYPED_TEST(GetNamespaceTest, ExtractsNamespaceCorrectly) {
266-
std::string kNs = ppc::util::GetNamespace<TypeParam>();
266+
std::string k_ns = ppc::util::GetNamespace<TypeParam>();
267267

268268
if constexpr (std::is_same_v<TypeParam, my::nested::Type>) {
269-
EXPECT_EQ(kNs, "my::nested");
269+
EXPECT_EQ(k_ns, "my::nested");
270270
} else if constexpr (std::is_same_v<TypeParam, my::Another>) {
271-
EXPECT_EQ(kNs, "my");
271+
EXPECT_EQ(k_ns, "my");
272272
} else if constexpr (std::is_same_v<TypeParam, int>) {
273-
EXPECT_EQ(kNs, "");
273+
EXPECT_EQ(k_ns, "");
274274
} else {
275275
FAIL() << "Unhandled type in test";
276276
}

modules/core/util/include/util.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <algorithm>
34
#include <cstdint>
45
#include <cstdlib>
56
#include <memory>
@@ -60,6 +61,14 @@ std::string GetNamespace() {
6061
std::free};
6162
name = (status == 0) ? demangled.get() : name;
6263
#endif
64+
const std::string prefixes[] = {"class ", "struct ", "enum ", "union "};
65+
for (const auto& prefix : prefixes) {
66+
if (name.starts_with(prefix)) {
67+
name = name.substr(prefix.size());
68+
break;
69+
}
70+
}
71+
name.erase(0, name.find_first_not_of(' '));
6372
auto pos = name.rfind("::");
6473
return (pos != std::string::npos) ? name.substr(0, pos) : std::string{};
6574
}

modules/core/util/tests/util.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ struct Type {};
1212
} // namespace my::nested
1313

1414
TEST(util_tests, extracts_correct_namespace) {
15-
std::string kNs = ppc::util::GetNamespace<my::nested::Type>();
16-
EXPECT_EQ(kNs, "my::nested");
15+
std::string k_ns = ppc::util::GetNamespace<my::nested::Type>();
16+
EXPECT_EQ(k_ns, "my::nested");
1717
}
1818

1919
TEST(util_tests, threads_control_check_openmp_disabled_valgrind) {
@@ -29,49 +29,49 @@ struct TypeInNamespace {};
2929
struct PlainType {};
3030

3131
TEST(GetNamespaceTest, ReturnsExpectedNamespace) {
32-
std::string kNs = ppc::util::GetNamespace<test_ns::TypeInNamespace>();
33-
EXPECT_EQ(kNs, "test_ns");
32+
std::string k_ns = ppc::util::GetNamespace<test_ns::TypeInNamespace>();
33+
EXPECT_EQ(k_ns, "test_ns");
3434
}
3535

3636
TEST(GetNamespaceTest, ReturnsEmptyIfNoNamespace_PrimitiveType) {
37-
std::string kNs = ppc::util::GetNamespace<int>();
38-
EXPECT_EQ(kNs, "");
37+
std::string k_ns = ppc::util::GetNamespace<int>();
38+
EXPECT_EQ(k_ns, "");
3939
}
4040

4141
TEST(GetNamespaceTest, ReturnsEmptyIfNoNamespace_PlainStruct) {
42-
std::string kNs = ppc::util::GetNamespace<PlainType>();
43-
EXPECT_EQ(kNs, "");
42+
std::string k_ns = ppc::util::GetNamespace<PlainType>();
43+
EXPECT_EQ(k_ns, "");
4444
}
4545

4646
namespace test_ns {
4747
struct Nested {};
4848
} // namespace test_ns
4949

5050
TEST(GetNamespaceTest, ReturnsNamespaceCorrectly) {
51-
std::string kNs = ppc::util::GetNamespace<test_ns::Nested>();
52-
EXPECT_EQ(kNs, "test_ns");
51+
std::string k_ns = ppc::util::GetNamespace<test_ns::Nested>();
52+
EXPECT_EQ(k_ns, "test_ns");
5353
}
5454

5555
struct NoNamespaceType {};
5656

5757
TEST(GetNamespaceTest, NoNamespaceInType) {
58-
std::string kNs = ppc::util::GetNamespace<NoNamespaceType>();
59-
EXPECT_EQ(kNs, "");
58+
std::string k_ns = ppc::util::GetNamespace<NoNamespaceType>();
59+
EXPECT_EQ(k_ns, "");
6060
}
6161

6262
template <typename T>
6363
struct NotATemplate {};
6464

6565
TEST(GetNamespaceTest, NoKeyInPrettyFunction) {
66-
std::string kNs = ppc::util::GetNamespace<NotATemplate<void>>();
67-
EXPECT_EQ(kNs, "");
66+
std::string k_ns = ppc::util::GetNamespace<NotATemplate<void>>();
67+
EXPECT_EQ(k_ns, "");
6868
}
6969

7070
namespace crazy {
7171
struct VeryLongTypeNameWithOnlyLettersAndUnderscores {};
7272
} // namespace crazy
7373

7474
TEST(GetNamespaceTest, NoTerminatorCharactersInPrettyFunction) {
75-
std::string kNs = ppc::util::GetNamespace<crazy::VeryLongTypeNameWithOnlyLettersAndUnderscores>();
76-
EXPECT_EQ(kNs, "crazy");
75+
std::string k_ns = ppc::util::GetNamespace<crazy::VeryLongTypeNameWithOnlyLettersAndUnderscores>();
76+
EXPECT_EQ(k_ns, "crazy");
7777
}

0 commit comments

Comments
 (0)