Skip to content

Commit 4f6cd69

Browse files
committed
More coverage
1 parent b9acd94 commit 4f6cd69

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

Tests/Source/LuaRefTests.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,41 @@ TEST_F(LuaRefTests, UnsafeRawgetFieldBypassesIndexMetamethod)
970970
EXPECT_EQ(0, luabridge::getGlobal(L, "indexCalls").unsafe_cast<int>());
971971
}
972972

973+
TEST_F(LuaRefTests, UserdataIndexMetamethodPropgetFastPath)
974+
{
975+
struct PropsClass
976+
{
977+
int value = 7;
978+
};
979+
980+
int getterCalls = 0;
981+
982+
luabridge::getGlobalNamespace(L)
983+
.beginClass<PropsClass>("PropsClass")
984+
.addConstructor<void (*)()>()
985+
.addProperty("tracked", [&getterCalls](const PropsClass* self) {
986+
++getterCalls;
987+
return self->value;
988+
})
989+
.endClass();
990+
991+
runLua("obj = PropsClass(); result = obj");
992+
auto obj = result();
993+
ASSERT_TRUE(obj.isUserdata());
994+
995+
auto tracked = obj["tracked"];
996+
auto trackedValue = luabridge::LuaRef(tracked);
997+
ASSERT_TRUE(trackedValue.isNumber());
998+
EXPECT_EQ(7, luabridge::unsafe_cast<int>(trackedValue));
999+
EXPECT_EQ(1, getterCalls);
1000+
1001+
// Missing key exercises the propget fast-path miss branch (line 556 pop) and falls through to nil.
1002+
auto missing = obj["definitely_missing"];
1003+
auto missingValue = luabridge::LuaRef(missing);
1004+
EXPECT_TRUE(missingValue.isNil());
1005+
EXPECT_EQ(1, getterCalls);
1006+
}
1007+
9731008
TEST_F(LuaRefTests, TableItemNestedRawHelpersAndCopy)
9741009
{
9751010
runLua("result = { outer = { value = 10 } }");
@@ -989,7 +1024,7 @@ TEST_F(LuaRefTests, TableItemNestedRawHelpersAndCopy)
9891024

9901025
auto rawField = replacedOuter.rawget("value");
9911026
ASSERT_TRUE(rawField.isNumber());
992-
EXPECT_EQ(77, rawField.unsafe_cast<int>());
1027+
EXPECT_EQ(77, luabridge::cast<int>(rawField).valueOr(0));
9931028
}
9941029

9951030
TEST_F(LuaRefTests, TableItemOperatorIndexAdoptPathAndRawRoundTrip)
@@ -1006,7 +1041,7 @@ TEST_F(LuaRefTests, TableItemOperatorIndexAdoptPathAndRawRoundTrip)
10061041

10071042
auto fromRaw = outer.rawget(childKey);
10081043
ASSERT_TRUE(fromRaw.isNumber());
1009-
EXPECT_EQ(19, fromRaw.unsafe_cast<int>());
1044+
EXPECT_EQ(19, luabridge::unsafe_cast<int>(fromRaw));
10101045

10111046
child.rawset(luabridge::newTable(L));
10121047

0 commit comments

Comments
 (0)