summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libadhocutil/unittests/testCompileTimeFormatter.cpp154
1 files changed, 71 insertions, 83 deletions
diff --git a/libadhocutil/unittests/testCompileTimeFormatter.cpp b/libadhocutil/unittests/testCompileTimeFormatter.cpp
index b3e3dac..d1afb68 100644
--- a/libadhocutil/unittests/testCompileTimeFormatter.cpp
+++ b/libadhocutil/unittests/testCompileTimeFormatter.cpp
@@ -21,158 +21,146 @@ extern constexpr const char * formatStringEscape2 = "literal %%? percentage.";
extern constexpr const char * formatStringEscape3 = "literal %%%? percentage.";
extern constexpr const char * formatStringEscape4 = "literal %%%?%% percentage.";
+namespace AdHoc {
+ // Custom stream writer formatter, formats as (bracketed expression)
+ template<const char * const & S, int start, typename stream, char ... sn>
+ struct StreamWriter<S, start, stream, '%', '(', ')', sn...> {
+ template<typename P, typename ... Pn>
+ static void write(stream & s, const P & p, const Pn & ... pn)
+ {
+ s << "-( " << p << " )-";
+ StreamWriter<S, start + 3, stream, sn...>::write(s, pn...);
+ }
+ };
+ // Custom stream writer formatter, formats
+ // right-aligned by given width
+ template<const char * const & S, int start, typename stream, char ... sn>
+ struct StreamWriter<S, start, stream, '%', 'r', 'a', sn...> {
+ template<typename P, typename ... Pn>
+ static void write(stream & s, int width, const P & p, const Pn & ... pn)
+ {
+ std::stringstream buf;
+ buf << p;
+ std::string spaces(width - buf.str().length(), ' ');
+ s << spaces << buf.str();
+ StreamWriter<S, start + 3, stream, sn...>::write(s, pn...);
+ }
+ };
+}
+
+BOOST_FIXTURE_TEST_SUITE( TestStreamWrite, std::stringstream )
+
BOOST_AUTO_TEST_CASE ( empty )
{
- std::stringstream buf;
- Formatter<formatEdgeCaseEmpty>::write(buf);
- BOOST_REQUIRE(buf.str().empty());
+ Formatter<formatEdgeCaseEmpty>::write(*this);
+ BOOST_REQUIRE(this->str().empty());
}
BOOST_AUTO_TEST_CASE ( single )
{
- std::stringstream buf;
- Formatter<formatEdgeCaseSingle>::write(buf);
- BOOST_REQUIRE_EQUAL(buf.str(), "1");
+ Formatter<formatEdgeCaseSingle>::write(*this);
+ BOOST_REQUIRE_EQUAL(this->str(), "1");
}
BOOST_AUTO_TEST_CASE ( start )
{
- std::stringstream buf;
- Formatter<formatEdgeCaseFormatStart>::write(buf, 10);
- BOOST_REQUIRE_EQUAL(buf.str(), "10 after");
+ Formatter<formatEdgeCaseFormatStart>::write(*this, 10);
+ BOOST_REQUIRE_EQUAL(this->str(), "10 after");
}
BOOST_AUTO_TEST_CASE ( end )
{
- std::stringstream buf;
- Formatter<formatEdgeCaseFormatEnd>::write(buf, 10);
- BOOST_REQUIRE_EQUAL(buf.str(), "before 10");
+ Formatter<formatEdgeCaseFormatEnd>::write(*this, 10);
+ BOOST_REQUIRE_EQUAL(this->str(), "before 10");
}
BOOST_AUTO_TEST_CASE ( lonely )
{
- std::stringstream buf;
- Formatter<formatEdgeCaseFormatLonely>::write(buf, 10);
- BOOST_REQUIRE_EQUAL(buf.str(), "10");
+ Formatter<formatEdgeCaseFormatLonely>::write(*this, 10);
+ BOOST_REQUIRE_EQUAL(this->str(), "10");
}
BOOST_AUTO_TEST_CASE ( literal )
{
- std::stringstream buf;
- Formatter<formatStringLiteral>::write(buf);
- BOOST_REQUIRE_EQUAL(buf.str(), "literal");
+ Formatter<formatStringLiteral>::write(*this);
+ BOOST_REQUIRE_EQUAL(this->str(), "literal");
}
BOOST_AUTO_TEST_CASE ( singleInt )
{
- std::stringstream buf;
- Formatter<formatStringSingle>::write(buf, 32);
- BOOST_REQUIRE_EQUAL(buf.str(), "single 32.");
+ Formatter<formatStringSingle>::write(*this, 32);
+ BOOST_REQUIRE_EQUAL(this->str(), "single 32.");
}
BOOST_AUTO_TEST_CASE ( singleDouble )
{
- std::stringstream buf;
- Formatter<formatStringSingle>::write(buf, 3.14);
- BOOST_REQUIRE_EQUAL(buf.str(), "single 3.14.");
+ Formatter<formatStringSingle>::write(*this, 3.14);
+ BOOST_REQUIRE_EQUAL(this->str(), "single 3.14.");
}
BOOST_AUTO_TEST_CASE ( singlePath )
{
- std::stringstream buf;
boost::filesystem::path p("/tmp/test/path");
- Formatter<formatStringSingle>::write(buf, p);
- BOOST_REQUIRE_EQUAL(buf.str(), R"(single "/tmp/test/path".)");
+ Formatter<formatStringSingle>::write(*this, p);
+ BOOST_REQUIRE_EQUAL(this->str(), R"(single "/tmp/test/path".)");
}
BOOST_AUTO_TEST_CASE ( multi )
{
- std::stringstream buf;
- Formatter<formatStringMulti>::write(buf, "one", "two");
- BOOST_REQUIRE_EQUAL(buf.str(), "First one, then two.");
+ Formatter<formatStringMulti>::write(*this, "one", "two");
+ BOOST_REQUIRE_EQUAL(this->str(), "First one, then two.");
}
BOOST_AUTO_TEST_CASE ( escape1 )
{
- std::stringstream buf;
- Formatter<formatStringEscape1>::write(buf);
- BOOST_REQUIRE_EQUAL(buf.str(), "literal % percentage.");
+ Formatter<formatStringEscape1>::write(*this);
+ BOOST_REQUIRE_EQUAL(this->str(), "literal % percentage.");
}
BOOST_AUTO_TEST_CASE ( escape2 )
{
- std::stringstream buf;
- Formatter<formatStringEscape2>::write(buf);
- BOOST_REQUIRE_EQUAL(buf.str(), "literal %? percentage.");
+ Formatter<formatStringEscape2>::write(*this);
+ BOOST_REQUIRE_EQUAL(this->str(), "literal %? percentage.");
}
BOOST_AUTO_TEST_CASE ( escape3 )
{
- std::stringstream buf;
- Formatter<formatStringEscape3>::write(buf, 3);
- BOOST_REQUIRE_EQUAL(buf.str(), "literal %3 percentage.");
+ Formatter<formatStringEscape3>::write(*this, 3);
+ BOOST_REQUIRE_EQUAL(this->str(), "literal %3 percentage.");
}
BOOST_AUTO_TEST_CASE ( escape4 )
{
- std::stringstream buf;
- Formatter<formatStringEscape4>::write(buf, 3);
- BOOST_REQUIRE_EQUAL(buf.str(), "literal %3% percentage.");
-}
-
-namespace AdHoc {
- // Custom stream writer formatter, formats as (bracketed expression)
- template<const char * const & S, int start, typename stream, char ... sn>
- struct StreamWriter<S, start, stream, '%', '(', ')', sn...> {
- template<typename P, typename ... Pn>
- static void write(stream & s, const P & p, const Pn & ... pn)
- {
- s << "-( " << p << " )-";
- StreamWriter<S, start + 3, stream, sn...>::write(s, pn...);
- }
- };
- // Custom stream writer formatter, formats
- // right-aligned by given width
- template<const char * const & S, int start, typename stream, char ... sn>
- struct StreamWriter<S, start, stream, '%', 'r', 'a', sn...> {
- template<typename P, typename ... Pn>
- static void write(stream & s, int width, const P & p, const Pn & ... pn)
- {
- std::stringstream buf;
- buf << p;
- std::string spaces(width - buf.str().length(), ' ');
- s << spaces << buf.str();
- StreamWriter<S, start + 3, stream, sn...>::write(s, pn...);
- }
- };
+ Formatter<formatStringEscape4>::write(*this, 3);
+ BOOST_REQUIRE_EQUAL(this->str(), "literal %3% percentage.");
}
extern constexpr const char * formatStringCustom = "custom %()";
BOOST_AUTO_TEST_CASE ( customBracketted )
{
- std::stringstream buf;
- Formatter<formatStringCustom>::write(buf, "expr");
- BOOST_REQUIRE_EQUAL(buf.str(), "custom -( expr )-");
+ Formatter<formatStringCustom>::write(*this, "expr");
+ BOOST_REQUIRE_EQUAL(this->str(), "custom -( expr )-");
+}
+
+extern constexpr const char * formatStringLong = " ";
+BOOST_AUTO_TEST_CASE ( longFormatString )
+{
+ Formatter<formatStringLong>::write(*this);
+ BOOST_REQUIRE_EQUAL(this->str().length(), 246);
}
+BOOST_AUTO_TEST_SUITE_END();
+
extern constexpr const char * formatStringMultiArg = "value%ra";
BOOST_AUTO_TEST_CASE ( customMultiArgRightAlign )
{
std::stringstream buf1, buf2, buf3;
const int width = 20;
- Formatter<formatStringMultiArg>::write(buf1, width, "something");
- Formatter<formatStringMultiArg>::write(buf2, width, "something else");
- Formatter<formatStringMultiArg>::write(buf3, width, 123.45);
+ Formatter<formatStringMultiArg>::write(buf1, width, "something");
+ Formatter<formatStringMultiArg>::write(buf2, width, "something else");
+ Formatter<formatStringMultiArg>::write(buf3, width, 123.45);
BOOST_REQUIRE_EQUAL(buf1.str(), "value something");
BOOST_REQUIRE_EQUAL(buf2.str(), "value something else");
BOOST_REQUIRE_EQUAL(buf3.str(), "value 123.45");
}
-extern constexpr const char * formatStringLong = " ";
-BOOST_AUTO_TEST_CASE ( longFormatString )
-{
- std::stringstream buf;
- Formatter<formatStringLong>::write(buf);
- BOOST_REQUIRE_EQUAL(buf.str().length(), 246);
-}
-