From 3536bad518fb67881d04fa57c9260483a552571a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 9 Dec 2016 16:45:08 +0000 Subject: Tidy tests --- .../unittests/testCompileTimeFormatter.cpp | 154 ++++++++++----------- 1 file 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 + struct StreamWriter { + template + static void write(stream & s, const P & p, const Pn & ... pn) + { + s << "-( " << p << " )-"; + StreamWriter::write(s, pn...); + } + }; + // Custom stream writer formatter, formats + // right-aligned by given width + template + struct StreamWriter { + template + 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::write(s, pn...); + } + }; +} + +BOOST_FIXTURE_TEST_SUITE( TestStreamWrite, std::stringstream ) + BOOST_AUTO_TEST_CASE ( empty ) { - std::stringstream buf; - Formatter::write(buf); - BOOST_REQUIRE(buf.str().empty()); + Formatter::write(*this); + BOOST_REQUIRE(this->str().empty()); } BOOST_AUTO_TEST_CASE ( single ) { - std::stringstream buf; - Formatter::write(buf); - BOOST_REQUIRE_EQUAL(buf.str(), "1"); + Formatter::write(*this); + BOOST_REQUIRE_EQUAL(this->str(), "1"); } BOOST_AUTO_TEST_CASE ( start ) { - std::stringstream buf; - Formatter::write(buf, 10); - BOOST_REQUIRE_EQUAL(buf.str(), "10 after"); + Formatter::write(*this, 10); + BOOST_REQUIRE_EQUAL(this->str(), "10 after"); } BOOST_AUTO_TEST_CASE ( end ) { - std::stringstream buf; - Formatter::write(buf, 10); - BOOST_REQUIRE_EQUAL(buf.str(), "before 10"); + Formatter::write(*this, 10); + BOOST_REQUIRE_EQUAL(this->str(), "before 10"); } BOOST_AUTO_TEST_CASE ( lonely ) { - std::stringstream buf; - Formatter::write(buf, 10); - BOOST_REQUIRE_EQUAL(buf.str(), "10"); + Formatter::write(*this, 10); + BOOST_REQUIRE_EQUAL(this->str(), "10"); } BOOST_AUTO_TEST_CASE ( literal ) { - std::stringstream buf; - Formatter::write(buf); - BOOST_REQUIRE_EQUAL(buf.str(), "literal"); + Formatter::write(*this); + BOOST_REQUIRE_EQUAL(this->str(), "literal"); } BOOST_AUTO_TEST_CASE ( singleInt ) { - std::stringstream buf; - Formatter::write(buf, 32); - BOOST_REQUIRE_EQUAL(buf.str(), "single 32."); + Formatter::write(*this, 32); + BOOST_REQUIRE_EQUAL(this->str(), "single 32."); } BOOST_AUTO_TEST_CASE ( singleDouble ) { - std::stringstream buf; - Formatter::write(buf, 3.14); - BOOST_REQUIRE_EQUAL(buf.str(), "single 3.14."); + Formatter::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::write(buf, p); - BOOST_REQUIRE_EQUAL(buf.str(), R"(single "/tmp/test/path".)"); + Formatter::write(*this, p); + BOOST_REQUIRE_EQUAL(this->str(), R"(single "/tmp/test/path".)"); } BOOST_AUTO_TEST_CASE ( multi ) { - std::stringstream buf; - Formatter::write(buf, "one", "two"); - BOOST_REQUIRE_EQUAL(buf.str(), "First one, then two."); + Formatter::write(*this, "one", "two"); + BOOST_REQUIRE_EQUAL(this->str(), "First one, then two."); } BOOST_AUTO_TEST_CASE ( escape1 ) { - std::stringstream buf; - Formatter::write(buf); - BOOST_REQUIRE_EQUAL(buf.str(), "literal % percentage."); + Formatter::write(*this); + BOOST_REQUIRE_EQUAL(this->str(), "literal % percentage."); } BOOST_AUTO_TEST_CASE ( escape2 ) { - std::stringstream buf; - Formatter::write(buf); - BOOST_REQUIRE_EQUAL(buf.str(), "literal %? percentage."); + Formatter::write(*this); + BOOST_REQUIRE_EQUAL(this->str(), "literal %? percentage."); } BOOST_AUTO_TEST_CASE ( escape3 ) { - std::stringstream buf; - Formatter::write(buf, 3); - BOOST_REQUIRE_EQUAL(buf.str(), "literal %3 percentage."); + Formatter::write(*this, 3); + BOOST_REQUIRE_EQUAL(this->str(), "literal %3 percentage."); } BOOST_AUTO_TEST_CASE ( escape4 ) { - std::stringstream buf; - Formatter::write(buf, 3); - BOOST_REQUIRE_EQUAL(buf.str(), "literal %3% percentage."); -} - -namespace AdHoc { - // Custom stream writer formatter, formats as (bracketed expression) - template - struct StreamWriter { - template - static void write(stream & s, const P & p, const Pn & ... pn) - { - s << "-( " << p << " )-"; - StreamWriter::write(s, pn...); - } - }; - // Custom stream writer formatter, formats - // right-aligned by given width - template - struct StreamWriter { - template - 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::write(s, pn...); - } - }; + Formatter::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::write(buf, "expr"); - BOOST_REQUIRE_EQUAL(buf.str(), "custom -( expr )-"); + Formatter::write(*this, "expr"); + BOOST_REQUIRE_EQUAL(this->str(), "custom -( expr )-"); +} + +extern constexpr const char * formatStringLong = " "; +BOOST_AUTO_TEST_CASE ( longFormatString ) +{ + Formatter::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::write(buf1, width, "something"); - Formatter::write(buf2, width, "something else"); - Formatter::write(buf3, width, 123.45); + Formatter::write(buf1, width, "something"); + Formatter::write(buf2, width, "something else"); + Formatter::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::write(buf); - BOOST_REQUIRE_EQUAL(buf.str().length(), 246); -} - -- cgit v1.2.3