Commit db0ca2fb authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

[trac901] The arg method without replacing

parent 2aca19e7
......@@ -64,6 +64,8 @@ private:
const unsigned nextPlaceholder_;
/// \brief Should we do output?
bool active_;
Formatter& operator =(const Formatter& other);
Formatter(const Formatter& other);
public:
/// \brief Constructor of "active" formatter
///
......@@ -77,7 +79,7 @@ public:
/// are used internally in the chain.
/// \param logger The logger where the final output will go.
Formatter(const char* prefix, const std::string& message,
const unsigned& nextPlaceholder, Logger& logger) :
const unsigned nextPlaceholder, Logger& logger) :
logger_(&logger), prefix_(prefix), message_(message),
nextPlaceholder_(nextPlaceholder), active_(true)
{
......@@ -103,8 +105,16 @@ public:
/// Replaces another placeholder and returns a new formatter with it.
/// Deactivates the current formatter. In case the formatter is not active,
/// only produces another inactive formatter.
template<class Arg> Formatter arg(const Arg& arg) {
///
/// \param arg The argument to place into the placeholder.
template<class Arg> Formatter arg(const Arg&) {
if (active_) {
active_ = false;
return (Formatter<Logger>(prefix_, message_, nextPlaceholder_ + 1,
*logger_));
} else {
return (Formatter<Logger>());
}
}
};
......
......@@ -49,4 +49,60 @@ TEST_F(FormatterTest, active) {
EXPECT_EQ("Text of message", outputs[0].second);
}
// No output even when we have an arg on the inactive formatter
TEST_F(FormatterTest, inactiveArg) {
Formatter().arg("Hello");
EXPECT_EQ(0, outputs.size());
}
// Create an active formatter and replace a placeholder with string
TEST_F(FormatterTest, stringArg) {
{
SCOPED_TRACE("C++ string");
Formatter("TEST", "Hello %1", 1, *this).arg(string("World"));
ASSERT_LE(1, outputs.size());
EXPECT_EQ(1, outputs.size());
EXPECT_STREQ("TEST", outputs[0].first);
EXPECT_EQ("Hello World", outputs[0].second);
}
{
SCOPED_TRACE("C++ string");
Formatter("TEST", "Hello %1", 1, *this).arg(string("Internet"));
ASSERT_LE(2, outputs.size());
EXPECT_EQ(2, outputs.size());
EXPECT_STREQ("TEST", outputs[1].first);
EXPECT_EQ("Hello Internet", outputs[1].second);
}
}
// Can convert to string
TEST_F(FormatterTest, intArg) {
Formatter("TEST", "The answer is %1", 1, *this).arg(42);
ASSERT_LE(1, outputs.size());
EXPECT_EQ(1, outputs.size());
EXPECT_STREQ("TEST", outputs[0].first);
EXPECT_EQ("The answer is 42", outputs[0].second);
}
// Can use multiple arguments at different places
TEST_F(FormatterTest, multiArg) {
Formatter("TEST", "The %2 are %1", 1, *this).arg("switched").
arg("arguments");
ASSERT_LE(1, outputs.size());
EXPECT_EQ(1, outputs.size());
EXPECT_STREQ("TEST", outputs[0].first);
EXPECT_EQ("The arguments are switched", outputs[0].second);
}
// Can survive and complains if placeholder is missing
TEST_F(FormatterTest, missingPlace) {
Formatter("TEST", "Missing the first %2", 1, *this).arg("missing").
arg("argument");
ASSERT_LE(1, outputs.size());
EXPECT_EQ(1, outputs.size());
EXPECT_STREQ("TEST", outputs[0].first);
EXPECT_EQ("Missing the first argument "
"@@Missing placeholder %1 for 'missing'@@", outputs[0].second);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment