1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include "Common.h"
SCENARIO("conditional logging")
{
GIVEN("logger is initialised")
{
plog::TestAppender testAppender;
plog::Logger<PLOG_DEFAULT_INSTANCE_ID> logger(plog::info);
logger.addAppender(&testAppender);
WHEN("condition is true")
{
int var = 0;
PLOG_INFO_IF(var == 0) << "message";
THEN("the message is printed")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("message"));
}
}
WHEN("condition is false")
{
int var = 0;
PLOG_INFO_IF(var != 0) << "message";
THEN("the message is not printed")
{
CHECK(testAppender.getMessage().empty());
}
}
WHEN("log level check is true")
{
int var = 0;
IF_PLOG(plog::info) var = 5; // one line
IF_PLOG(plog::info) // block
{
var++;
}
THEN("statements were executed")
{
CHECK_EQ(var, 5 + 1);
}
}
WHEN("log level check is false")
{
int var = 0;
IF_PLOG(plog::debug) var = 5; // one line
IF_PLOG(plog::debug) // block
{
var++;
}
THEN("statements were not executed")
{
CHECK_EQ(var, 0);
}
}
WHEN("log macros are used in 'then-else' clauses without braces and condition is true")
{
int var = 0;
if (var == 0) PLOGI << "then clause"; else PLOGI << "else clause";
THEN("nothing is broken, 'then' clause is executed")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("then clause"));
}
}
WHEN("log macros are used in 'then-else' clauses without braces and condition is false")
{
int var = 0;
if (var != 0) PLOGI << "then clause"; else PLOGI << "else clause";
THEN("nothing is broken, 'else' clause is executed")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("else clause"));
}
}
}
}
|