Skip to content

Commit

Permalink
refactor(mock): don't error out on overlapping mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Sep 2, 2023
1 parent 6c60a08 commit d93a9f8
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/manager.impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ namespace loader

void manager::impl::setup_hooks()
{
using restore_t = std::function<void()>;

auto table = mod_api.create_named("hooks");

table["intercept_require"] = [this](const std::string &module, const sol::function &callback)
Expand All @@ -87,26 +85,26 @@ namespace loader
hooks.erase(it);
};

return std::make_unique<restore_t>(restore);
return std::make_unique<std::function<void()>>(restore);
};

table["mock_require"] = [this](const std::string &module, const sol::function &callback)
{
logger::get()->debug("registering mock for \"{}\"", module);

if (mocks.contains(module))
{
logger::get()->error("mock for \"{}\" already registered", module);
return std::make_unique<restore_t>(nullptr);
logger::get()->warn("mock for \"{}\" already registered, overwriting", module);
}

logger::get()->debug("registering mock for \"{}\"", module);
mocks.emplace(module, callback.as<mock_callback>());

const auto restore = [module, this]()
{
mocks.erase(module);
};

return std::make_unique<restore_t>(restore);
return std::make_unique<std::function<void()>>(restore);
};

lua->do_string(R"lua(
Expand Down

0 comments on commit d93a9f8

Please sign in to comment.