Skip to content

Commit f3a3b65

Browse files
committed
Add tests for LookupConstructor and new codepaths in IsConstructor
1 parent bdabe79 commit f3a3b65

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,45 @@ TEST(FunctionReflectionTest, InstantiateTemplateMethod) {
605605
EXPECT_TRUE(TA1.getAsType()->isIntegerType());
606606
}
607607

608+
TEST(FunctionReflectionTest, LookupConstructors) {
609+
std::vector<Decl*> Decls;
610+
std::string code = R"(
611+
class MyClass {
612+
public:
613+
MyClass();
614+
void helperMethod();
615+
MyClass(const MyClass&);
616+
static void staticFunc();
617+
MyClass(MyClass&&);
618+
template<typename T>
619+
MyClass(T);
620+
~MyClass();
621+
};
622+
623+
MyClass::MyClass() {}
624+
void MyClass::helperMethod() {}
625+
MyClass::MyClass(const MyClass&) {}
626+
void MyClass::staticFunc() {}
627+
MyClass::MyClass(MyClass&&) {}
628+
template<typename T>
629+
MyClass::MyClass(T t) {}
630+
MyClass::~MyClass() {}
631+
)";
632+
633+
GetAllTopLevelDecls(code, Decls);
634+
std::vector<Cpp::TCppFunction_t> ctors;
635+
Cpp::LookupConstructors("MyClass", Decls[0], ctors);
636+
637+
EXPECT_EQ(ctors.size(), 4)
638+
<< "Constructor lookup did not retrieve the expected set";
639+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[0]), "MyClass::MyClass()");
640+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[1]),
641+
"MyClass::MyClass(const MyClass &)");
642+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[2]),
643+
"MyClass::MyClass(MyClass &&)");
644+
EXPECT_EQ(Cpp::GetFunctionSignature(ctors[3]), "MyClass::MyClass(T t)");
645+
}
646+
608647
TEST(FunctionReflectionTest, BestOverloadFunctionMatch1) {
609648
std::vector<Decl*> Decls;
610649
std::string code = R"(
@@ -1004,6 +1043,28 @@ TEST(FunctionReflectionTest, IsConstructor) {
10041043
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[4]));
10051044
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[6]));
10061045
EXPECT_FALSE(Cpp::IsConstructor(SubDecls[8]));
1046+
1047+
// Test for templated constructor
1048+
std::vector<Decl*> templDecls, templSubDecls;
1049+
std::string templCode = R"(
1050+
class T {
1051+
public:
1052+
template<typename U>
1053+
T(U) {}
1054+
void func() {}
1055+
~T() {}
1056+
};
1057+
)";
1058+
1059+
GetAllTopLevelDecls(templCode, templDecls);
1060+
GetAllSubDecls(templDecls[0], templSubDecls);
1061+
1062+
int templCtorCount = 0;
1063+
for (auto* decl : templSubDecls) {
1064+
if (Cpp::IsConstructor(decl))
1065+
templCtorCount++;
1066+
}
1067+
EXPECT_EQ(templCtorCount, 1);
10071068
}
10081069

10091070
TEST(FunctionReflectionTest, IsDestructor) {

0 commit comments

Comments
 (0)