MCPcopy Create free account
hub / github.com/OpenMW/openmw / TEST

Function TEST

apps/components_tests/lua/teststorage.cpp:18–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16 }
17
18 TEST(LuaUtilStorageTest, Subscribe)
19 {
20 // Note: LuaUtil::Callback can be used only if Lua is initialized via LuaUtil::LuaState
21 LuaUtil::LuaState luaState{ nullptr, nullptr };
22 luaState.protectedCall([](LuaUtil::LuaView& view) {
23 sol::state_view& lua = view.sol();
24 LuaUtil::LuaStorage::initLuaBindings(view);
25 LuaUtil::LuaStorage storage;
26 storage.setActive(true);
27
28 sol::table callbackHiddenData(lua, sol::create);
29 callbackHiddenData[LuaUtil::ScriptsContainer::sScriptIdKey] = LuaUtil::ScriptId{};
30 LuaUtil::getAsyncPackageInitializer(
31 lua.lua_state(), []() { return 0.0; }, []() { return 0.0; })(callbackHiddenData);
32 lua["async"] = LuaUtil::AsyncPackageId{ nullptr, 0, callbackHiddenData };
33
34 lua["mutable"] = storage.getMutableSection(lua, "test");
35 lua["ro"] = storage.getReadOnlySection(lua, "test");
36
37 lua.safe_script(R"(
38 callbackCalls = {}
39 ro:subscribe(async:callback(function(section, key)
40 table.insert(callbackCalls, section .. '_' .. (key or '*'))
41 end))
42 )");
43
44 lua.safe_script("mutable:set('x', 5)");
45 EXPECT_EQ(get<int>(lua, "mutable:get('x')"), 5);
46 EXPECT_EQ(get<int>(lua, "ro:get('x')"), 5);
47
48 EXPECT_THROW(lua.safe_script("ro:set('y', 3)"), std::exception);
49
50 lua.safe_script("t1 = mutable:asTable()");
51 lua.safe_script("t2 = ro:asTable()");
52 EXPECT_EQ(get<int>(lua, "t1.x"), 5);
53 EXPECT_EQ(get<int>(lua, "t2.x"), 5);
54
55 lua.safe_script("mutable:reset()");
56 EXPECT_TRUE(get<bool>(lua, "ro:get('x') == nil"));
57
58 lua.safe_script("mutable:reset({x=4, y=7})");
59 EXPECT_EQ(get<int>(lua, "ro:get('x')"), 4);
60 EXPECT_EQ(get<int>(lua, "ro:get('y')"), 7);
61
62 EXPECT_THAT(get<std::string>(lua, "table.concat(callbackCalls, ', ')"), "test_x, test_*, test_*");
63 });
64 }
65
66 TEST(LuaUtilStorageTest, Table)
67 {

Callers

nothing calls this directly

Calls 11

solMethod · 0.80
getMutableSectionMethod · 0.80
getReadOnlySectionMethod · 0.80
safe_scriptMethod · 0.80
protectedCallMethod · 0.45
setActiveMethod · 0.45
lua_stateMethod · 0.45
saveMethod · 0.45
loadMethod · 0.45

Tested by

no test coverage detected