From e212aada344be31acc1d9ca20091e762f5998905 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 22 Jun 2019 11:47:50 +0100 Subject: [PATCH] Add more tests for SetLoggingCallback() Updating loot-api-python to use pybind11 2.3.0 seems to have broken passing callbacks, so I tried passing them as const references, but these tests show that wouldn't work. --- src/tests/api/interface/main.cpp | 92 +++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 6 deletions(-) diff --git a/src/tests/api/interface/main.cpp b/src/tests/api/interface/main.cpp index a685edca..f468c0d8 100644 --- a/src/tests/api/interface/main.cpp +++ b/src/tests/api/interface/main.cpp @@ -37,14 +37,75 @@ int main(int argc, char **argv) { namespace loot { namespace test { -TEST(SetLoggingCallback, shouldWriteMessagesToGivenCallback) { +void testLoggingCallback(LogLevel level, const char * message) { + // Do nothing. +} + +struct TestLogger { + void callback(LogLevel level, const char * message) { + loggedMessages += std::string(message); + } + std::string loggedMessages; - SetLoggingCallback([&](LogLevel level, const char *string) { - loggedMessages += std::string(string); - }); +}; + +TEST(SetLoggingCallback, shouldAcceptAFreeFunction) { + SetLoggingCallback(testLoggingCallback); try { CreateGameHandle(GameType::tes4, "dummy"); + FAIL(); + } + catch (...) { + //SetLoggingCallback([](LogLevel, const char *) {}); + } +} + +TEST(SetLoggingCallback, shouldAcceptAMemberFunction) { + TestLogger testLogger; + auto boundCallback = std::bind(&TestLogger::callback, &testLogger, std::placeholders::_1, std::placeholders::_2); + SetLoggingCallback(boundCallback); + + try { + CreateGameHandle(GameType::tes4, "dummy"); + FAIL(); + } + catch (...) { + EXPECT_EQ( + "Attempting to create a game handle with game path \"dummy\" " + "and local path \"\"", + testLogger.loggedMessages); + + SetLoggingCallback([](LogLevel, const char *) {}); + } +} + +TEST(SetLoggingCallback, shouldNotBreakLoggingIfPassedMemberFunctionGoesOutOfScope) { + { + TestLogger testLogger; + auto boundCallback = std::bind(&TestLogger::callback, &testLogger, std::placeholders::_1, std::placeholders::_2); + SetLoggingCallback(boundCallback); + } + + try { + CreateGameHandle(GameType::tes4, "dummy"); + FAIL(); + } + catch (...) { + SetLoggingCallback([](LogLevel, const char *) {}); + } +} + +TEST(SetLoggingCallback, shouldAcceptALambdaFunction) { + std::string loggedMessages; + auto callback = [&](LogLevel level, const char *string) { + loggedMessages += std::string(string); + }; + SetLoggingCallback(callback); + + try { + CreateGameHandle(GameType::tes4, "dummy"); + FAIL(); } catch (...) { EXPECT_EQ( "Attempting to create a game handle with game path \"dummy\" " @@ -52,10 +113,29 @@ TEST(SetLoggingCallback, shouldWriteMessagesToGivenCallback) { loggedMessages); SetLoggingCallback([](LogLevel, const char *) {}); - return; + } +} + +TEST(SetLoggingCallback, shouldNotBreakLoggingIfPassedLambdaFunctionGoesOutOfScope) { + std::string loggedMessages; + { + SetLoggingCallback([&](LogLevel level, const char *string) { + loggedMessages += std::string(string); + }); } - FAIL(); + try { + CreateGameHandle(GameType::tes4, "dummy"); + FAIL(); + } + catch (...) { + EXPECT_EQ( + "Attempting to create a game handle with game path \"dummy\" " + "and local path \"\"", + loggedMessages); + + SetLoggingCallback([](LogLevel, const char *) {}); + } } } }