Skip to content

Commit 6c73ffd

Browse files
committed
Add tests for LookupConstructor and new codepaths in IsConstructor
1 parent aa3630f commit 6c73ffd

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,48 @@ TEST(FunctionReflectionTest, InstantiateTemplateMethod) {
662662
EXPECT_TRUE(TA1.getAsType()->isIntegerType());
663663
}
664664

665+
TEST(FunctionReflectionTest, LookupConstructors) {
666+
if (llvm::sys::RunningOnValgrind())
667+
GTEST_SKIP() << "XFAIL due to Valgrind report";
668+
669+
std::vector<Decl*> Decls;
670+
std::string code = R"(
671+
class MyClass {
672+
public:
673+
MyClass();
674+
void helperMethod();
675+
MyClass(const MyClass&);
676+
static void staticFunc();
677+
MyClass(MyClass&&);
678+
template<typename T>
679+
MyClass(T);
680+
~MyClass();
681+
};
682+
683+
MyClass::MyClass() {}
684+
void MyClass::helperMethod() {}
685+
MyClass::MyClass(const MyClass&) {}
686+
void MyClass::staticFunc() {}
687+
MyClass::MyClass(MyClass&&) {}
688+
template<typename T>
689+
MyClass::MyClass(T t) {}
690+
MyClass::~MyClass() {}
691+
)";
692+
693+
GetAllTopLevelDecls(code, Decls);
694+
std::vector<Cpp::TCppFunction_t> ctors;
695+
Cpp::LookupConstructors("MyClass", Decls[0], ctors);
696+
697+
EXPECT_EQ(ctors.size(), 4)
698+
<< "Constructor lookup did not retrieve the expected set";
699+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[0]), "MyClass::MyClass()");
700+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[1]),
701+
"MyClass::MyClass(const MyClass &)");
702+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[2]),
703+
"MyClass::MyClass(MyClass &&)");
704+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[3]), "MyClass::MyClass(T t)");
705+
}
706+
665707
TEST(FunctionReflectionTest, BestOverloadFunctionMatch1) {
666708
std::vector<Decl*> Decls;
667709
std::string code = R"(
@@ -1061,6 +1103,28 @@ TEST(FunctionReflectionTest, IsConstructor) {
10611103
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[4]));
10621104
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[6]));
10631105
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[8]));
1106+
1107+
// Test for templated constructor
1108+
std::vector<Decl*> templDecls, templSubDecls;
1109+
std::string templCode = R"(
1110+
class T {
1111+
public:
1112+
template<typename U>
1113+
T(U) {}
1114+
void func() {}
1115+
~T() {}
1116+
};
1117+
)";
1118+
1119+
GetAllTopLevelDecls(templCode, templDecls);
1120+
GetAllSubDecls(templDecls[0], templSubDecls);
1121+
1122+
int templCtorCount = 0;
1123+
for (auto* decl : templSubDecls) {
1124+
if (Cpp::IsConstructor(decl))
1125+
templCtorCount++;
1126+
}
1127+
EXPECT_EQ(templCtorCount, 1);
10641128
}
10651129

10661130
TEST(FunctionReflectionTest, IsDestructor) {

0 commit comments

Comments
 (0)