Skip to content

Commit 98a57b4

Browse files
aaronj0vgvassilev
authored andcommitted
Add test for nested constructor calls
1 parent 79ddc61 commit 98a57b4

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,66 @@ TEST(FunctionReflectionTest, Construct) {
17661766
clang_Interpreter_dispose(I);
17671767
}
17681768

1769+
// Test nested constructor calls
1770+
TEST(FunctionReflectionTest, ConstructNested) {
1771+
#ifdef EMSCRIPTEN
1772+
GTEST_SKIP() << "Test fails for Emscipten builds";
1773+
#endif
1774+
if (llvm::sys::RunningOnValgrind())
1775+
GTEST_SKIP() << "XFAIL due to Valgrind report";
1776+
#ifdef _WIN32
1777+
GTEST_SKIP() << "Disabled on Windows. Needs fixing.";
1778+
#endif
1779+
1780+
Cpp::CreateInterpreter();
1781+
1782+
Interp->declare(R"(
1783+
#include <new>
1784+
extern "C" int printf(const char*,...);
1785+
class A {
1786+
public:
1787+
int a_val;
1788+
A() : a_val(7) {
1789+
printf("A Constructor Called\n");
1790+
}
1791+
};
1792+
1793+
class B {
1794+
public:
1795+
A a;
1796+
int b_val;
1797+
B() : b_val(99) {
1798+
printf("B Constructor Called\n");
1799+
}
1800+
};
1801+
)");
1802+
1803+
testing::internal::CaptureStdout();
1804+
Cpp::TCppScope_t scope_A = Cpp::GetNamed("A");
1805+
Cpp::TCppScope_t scope_B = Cpp::GetNamed("B");
1806+
Cpp::TCppObject_t object = Cpp::Construct(scope_B);
1807+
EXPECT_TRUE(object != nullptr);
1808+
std::string output = testing::internal::GetCapturedStdout();
1809+
EXPECT_EQ(output, "A Constructor Called\nB Constructor Called\n");
1810+
output.clear();
1811+
1812+
// In-memory construction
1813+
testing::internal::CaptureStdout();
1814+
void* arena = Cpp::Allocate(scope_B);
1815+
EXPECT_TRUE(arena == Cpp::Construct(scope_B, arena));
1816+
1817+
// Check if both integers a_val and b_val were set.
1818+
EXPECT_EQ(*(int*)arena, 7);
1819+
size_t a_size = Cpp::SizeOf(scope_A);
1820+
int* b_val_ptr =
1821+
reinterpret_cast<int*>(reinterpret_cast<char*>(arena) + a_size);
1822+
EXPECT_EQ(*b_val_ptr, 99);
1823+
Cpp::Deallocate(scope_B, arena);
1824+
output = testing::internal::GetCapturedStdout();
1825+
EXPECT_EQ(output, "A Constructor Called\nB Constructor Called\n");
1826+
output.clear();
1827+
}
1828+
17691829
TEST(FunctionReflectionTest, Destruct) {
17701830
#ifdef EMSCRIPTEN
17711831
GTEST_SKIP() << "Test fails for Emscipten builds";

0 commit comments

Comments
 (0)