Skip to content

Commit 64d5430

Browse files
committed
refactor tests: enhance GetNamespace coverage and modernize type usage
Refactored tests to improve clarity and robustness. Updated type definitions to modern equivalents (`std::array`, `enum` with explicit width, etc.) and replaced namespace definitions with inline syntax. Expanded test cases for built-in, unsigned, floating-point, and character types to ensure comprehensive coverage of edge scenarios.
1 parent 004b420 commit 64d5430

2 files changed

Lines changed: 30 additions & 29 deletions

File tree

modules/util/tests/util.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <gtest/gtest.h>
44

5+
#include <array>
6+
#include <cstdint>
57
#include <libenvpp/detail/environment.hpp>
68
#include <libenvpp/detail/get.hpp>
79
#include <string>
@@ -101,7 +103,7 @@ TEST(GetNamespaceTest, GetNamespace_WithAnonymousNamespace_HandlesCorrectly) {
101103
// Additional edge case tests for better coverage
102104
TEST(GetNamespaceTest, GetNamespace_WithGlobalNamespaceType_ReturnsEmpty) {
103105
// Global namespace enum defined inside a function gets special handling
104-
enum GlobalEnum { VALUE };
106+
enum GlobalEnum : std::uint8_t { kValue };
105107
std::string k_ns = ppc::util::GetNamespace<GlobalEnum>();
106108
// Local enums defined in functions can have function names in their type
107109
EXPECT_TRUE(k_ns.find("GetNamespaceTest") != std::string::npos || k_ns.empty());
@@ -117,23 +119,20 @@ TEST(GetNamespaceTest, GetNamespace_WithFunctionPointer_HandlesCorrectly) {
117119

118120
// Test with array type
119121
TEST(GetNamespaceTest, GetNamespace_WithArrayType_ReturnsEmpty) {
120-
using ArrayType = int[10];
122+
using ArrayType = std::array<int, 10>;
121123
std::string k_ns = ppc::util::GetNamespace<ArrayType>();
122-
EXPECT_EQ(k_ns, "");
124+
// std::array is in std namespace
125+
EXPECT_TRUE(k_ns.find("std") == 0);
123126
}
124127

125128
// Test with deeply nested template to stress the demangler
126-
namespace deeply {
127-
namespace nested {
128-
namespace ns {
129+
namespace deeply::nested::ns {
129130
template <typename T, typename U, typename V>
130131
struct ComplexTemplate {
131132
template <typename X, typename Y>
132133
struct Inner {};
133134
};
134-
} // namespace ns
135-
} // namespace nested
136-
} // namespace deeply
135+
} // namespace deeply::nested::ns
137136

138137
TEST(GetNamespaceTest, GetNamespace_WithDeeplyNestedTemplate_ExtractsCorrectly) {
139138
using ComplexType = deeply::nested::ns::ComplexTemplate<int, double, char>::Inner<float, bool>;

modules/util/tests/util_demangle_edge_cases.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#include <gtest/gtest.h>
22

3+
#include <string>
4+
35
#include "util/include/util.hpp"
46

57
// This file tests edge cases that might cause demangling failures
68
// or other uncovered branches in GetNamespace
79

810
// Test with an extern "C" function type that might have special handling
911
extern "C" {
10-
typedef void (*CFunction)();
12+
using CFunction = void (*)();
1113
}
1214

1315
TEST(GetNamespaceEdgeCases, GetNamespace_WithExternCFunction_HandlesCorrectly) {
@@ -25,21 +27,35 @@ TEST(GetNamespaceEdgeCases, GetNamespace_WithNoColonColon_ReturnsEmpty) {
2527
EXPECT_EQ(k_ns, "");
2628
}
2729

28-
// Test with built-in types that might have special mangling
29-
TEST(GetNamespaceEdgeCases, GetNamespace_WithBuiltinTypes_ReturnsEmpty) {
30+
// Test with basic built-in types
31+
TEST(GetNamespaceEdgeCases, GetNamespace_WithBasicBuiltinTypes_ReturnsEmpty) {
3032
EXPECT_EQ(ppc::util::GetNamespace<void>(), "");
3133
EXPECT_EQ(ppc::util::GetNamespace<char>(), "");
3234
EXPECT_EQ(ppc::util::GetNamespace<short>(), "");
35+
EXPECT_EQ(ppc::util::GetNamespace<int>(), "");
3336
EXPECT_EQ(ppc::util::GetNamespace<long>(), "");
3437
EXPECT_EQ(ppc::util::GetNamespace<long long>(), "");
38+
EXPECT_EQ(ppc::util::GetNamespace<bool>(), "");
39+
}
40+
41+
// Test with unsigned built-in types
42+
TEST(GetNamespaceEdgeCases, GetNamespace_WithUnsignedBuiltinTypes_ReturnsEmpty) {
3543
EXPECT_EQ(ppc::util::GetNamespace<unsigned char>(), "");
3644
EXPECT_EQ(ppc::util::GetNamespace<unsigned short>(), "");
45+
EXPECT_EQ(ppc::util::GetNamespace<unsigned int>(), "");
3746
EXPECT_EQ(ppc::util::GetNamespace<unsigned long>(), "");
3847
EXPECT_EQ(ppc::util::GetNamespace<unsigned long long>(), "");
48+
}
49+
50+
// Test with floating point types
51+
TEST(GetNamespaceEdgeCases, GetNamespace_WithFloatingPointTypes_ReturnsEmpty) {
3952
EXPECT_EQ(ppc::util::GetNamespace<float>(), "");
4053
EXPECT_EQ(ppc::util::GetNamespace<double>(), "");
4154
EXPECT_EQ(ppc::util::GetNamespace<long double>(), "");
42-
EXPECT_EQ(ppc::util::GetNamespace<bool>(), "");
55+
}
56+
57+
// Test with character types
58+
TEST(GetNamespaceEdgeCases, GetNamespace_WithCharacterTypes_ReturnsEmpty) {
4359
EXPECT_EQ(ppc::util::GetNamespace<wchar_t>(), "");
4460
EXPECT_EQ(ppc::util::GetNamespace<char16_t>(), "");
4561
EXPECT_EQ(ppc::util::GetNamespace<char32_t>(), "");
@@ -49,23 +65,9 @@ TEST(GetNamespaceEdgeCases, GetNamespace_WithBuiltinTypes_ReturnsEmpty) {
4965
}
5066

5167
// Test with very long namespace chain
52-
namespace a {
53-
namespace b {
54-
namespace c {
55-
namespace d {
56-
namespace e {
57-
namespace f {
58-
namespace g {
59-
namespace h {
68+
namespace a::b::c::d::e::f::g::h {
6069
struct DeepType {};
61-
} // namespace h
62-
} // namespace g
63-
} // namespace f
64-
} // namespace e
65-
} // namespace d
66-
} // namespace c
67-
} // namespace b
68-
} // namespace a
70+
} // namespace a::b::c::d::e::f::g::h
6971

7072
TEST(GetNamespaceEdgeCases, GetNamespace_WithVeryDeepNamespace_ExtractsCorrectly) {
7173
std::string k_ns = ppc::util::GetNamespace<a::b::c::d::e::f::g::h::DeepType>();

0 commit comments

Comments
 (0)