Skip to content

Commit 9f7f388

Browse files
committed
Add tests for LookupConstructor and new codepaths in IsConstructor
1 parent f4949de commit 9f7f388

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,47 @@ 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+
)";
691+
692+
GetAllTopLevelDecls(code, Decls);
693+
std::vector<Cpp::TCppFunction_t> ctors;
694+
Cpp::LookupConstructors("MyClass", Decls[0], ctors);
695+
696+
EXPECT_EQ(ctors.size(), 4)
697+
<< "Constructor lookup did not retrieve the expected set";
698+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[0]), "MyClass::MyClass()");
699+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[1]),
700+
"MyClass::MyClass(const MyClass &)");
701+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[2]),
702+
"MyClass::MyClass(MyClass &&)");
703+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[3]), "MyClass::MyClass(T t)");
704+
}
705+
665706
TEST(FunctionReflectionTest, BestOverloadFunctionMatch1) {
666707
std::vector<Decl*> Decls;
667708
std::string code = R"(
@@ -1061,6 +1102,28 @@ TEST(FunctionReflectionTest, IsConstructor) {
10611102
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[4]));
10621103
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[6]));
10631104
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[8]));
1105+
1106+
// Test for templated constructor
1107+
std::vector<Decl*> templDecls, templSubDecls;
1108+
std::string templCode = R"(
1109+
class T {
1110+
public:
1111+
template<typename U>
1112+
T(U) {}
1113+
void func() {}
1114+
~T() {}
1115+
};
1116+
)";
1117+
1118+
GetAllTopLevelDecls(templCode, templDecls);
1119+
GetAllSubDecls(templDecls[0], templSubDecls);
1120+
1121+
int templCtorCount = 0;
1122+
for (auto* decl : templSubDecls) {
1123+
if (Cpp::IsConstructor(decl))
1124+
templCtorCount++;
1125+
}
1126+
EXPECT_EQ(templCtorCount, 1);
10641127
}
10651128

10661129
TEST(FunctionReflectionTest, IsDestructor) {

0 commit comments

Comments
 (0)