mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-06 00:55:50 +00:00
Fix Content::View::remove
This commit is contained in:
parent
fc1430af95
commit
3618b3f409
@ -119,15 +119,17 @@ namespace
|
||||
auto table = makeTable();
|
||||
LuaUi::Content::View content = makeContent(table);
|
||||
EXPECT_ANY_THROW(content.at(0));
|
||||
EXPECT_EQ(content.size(), 0);
|
||||
content.assign(content.size(), makeTable());
|
||||
EXPECT_EQ(content.size(), 1);
|
||||
content.assign(content.size(), makeTable());
|
||||
EXPECT_EQ(content.size(), 2);
|
||||
content.assign(content.size(), makeTable());
|
||||
EXPECT_EQ(content.size(), 3);
|
||||
EXPECT_ANY_THROW(content.at(3));
|
||||
EXPECT_ANY_THROW(content.remove(3));
|
||||
EXPECT_NO_THROW(content.remove(1)); // TODO: something cursed happens here, even __newindex is not called!
|
||||
EXPECT_EQ(content.size(), 2);
|
||||
EXPECT_NO_THROW(content.at(2));
|
||||
content.remove(2);
|
||||
EXPECT_EQ(content.size(), 2);
|
||||
EXPECT_ANY_THROW(content.at(2));
|
||||
}
|
||||
}
|
||||
|
@ -78,16 +78,18 @@ namespace LuaUi::Content
|
||||
void remove(size_t index)
|
||||
{
|
||||
if (index < size())
|
||||
mTable[toLua(index)] = sol::nil;
|
||||
// for some reason mTable[key] = value doesn't call __newindex
|
||||
mTable[sol::metatable_key][sol::meta_function::new_index].get<sol::protected_function>()(mTable, toLua(index), sol::nil);
|
||||
else
|
||||
throw std::domain_error("Invalid Content index");
|
||||
}
|
||||
void remove(std::string_view name)
|
||||
{
|
||||
if (indexOf(name).has_value())
|
||||
mTable[name] = sol::nil;
|
||||
auto index = indexOf(name);
|
||||
if (index.has_value())
|
||||
remove(index.value());
|
||||
else
|
||||
throw std::domain_error("Invalid Content index");
|
||||
throw std::domain_error("Invalid Content key");
|
||||
}
|
||||
std::optional<size_t> indexOf(std::string_view name) const
|
||||
{
|
||||
|
@ -79,7 +79,6 @@ M.__index = function(self, key)
|
||||
return rawget(self, index)
|
||||
end
|
||||
local function remove(self, index)
|
||||
print('remove', #self, index)
|
||||
local oldName = nameAt(self, index)
|
||||
if oldName then
|
||||
self.__nameIndex[oldName] = nil
|
||||
@ -95,7 +94,6 @@ local function remove(self, index)
|
||||
end
|
||||
end
|
||||
rawset(self, #self, nil)
|
||||
print('removed', #self)
|
||||
end
|
||||
local function assign(self, index, value)
|
||||
local oldName = nameAt(self, index)
|
||||
@ -108,7 +106,6 @@ local function assign(self, index, value)
|
||||
end
|
||||
end
|
||||
M.__newindex = function(self, key, value)
|
||||
print('__newindex ', key, value)
|
||||
local index = getIndexFromKey(self, key)
|
||||
if value == nil then
|
||||
remove(self, index)
|
||||
@ -131,14 +128,5 @@ M.__pairs = function(self)
|
||||
end
|
||||
M.__ipairs = M.__pairs
|
||||
|
||||
local test = M.new({})
|
||||
test:insert(1, {})
|
||||
test[2] = {}
|
||||
assert(#test == 2, "Wrong size")
|
||||
test:add({ name = 'a' })
|
||||
assert(getIndexFromKey(test, 'a') == 3, getIndexFromKey(test, 'a'))
|
||||
assert(type(test.a) == 'table', type(test.a))
|
||||
assert(test.a.name == 'a', 'wrong table')
|
||||
|
||||
return M
|
||||
)"
|
Loading…
Reference in New Issue
Block a user