From bc4474c72fdebad50ef5fecb809e954462a7375e Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Thu, 21 Sep 2023 11:10:50 -0400 Subject: [PATCH] print: utest for print::quoted, print::unq --- utest/CMakeLists.txt | 2 +- utest/quoted.test.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 utest/quoted.test.cpp diff --git a/utest/CMakeLists.txt b/utest/CMakeLists.txt index 707b7fd..c4ecf78 100644 --- a/utest/CMakeLists.txt +++ b/utest/CMakeLists.txt @@ -1,7 +1,7 @@ # indentlog unit test set(SELF_EXECUTABLE_NAME utest.indentlog) -set(SELF_SOURCE_FILES fixed.test.cpp indentlog_utest_main.cpp) +set(SELF_SOURCE_FILES fixed.test.cpp quoted.test.cpp indentlog_utest_main.cpp) add_executable(${SELF_EXECUTABLE_NAME} ${SELF_SOURCE_FILES}) xo_include_options(${SELF_EXECUTABLE_NAME}) diff --git a/utest/quoted.test.cpp b/utest/quoted.test.cpp new file mode 100644 index 0000000..35583d4 --- /dev/null +++ b/utest/quoted.test.cpp @@ -0,0 +1,76 @@ +/* @file fixed.test.cpp */ + +#include "indentlog/print/quoted.hpp" +#include "indentlog/print/tag.hpp" +#include +#include + +using namespace xo; +using namespace xo::print; + +namespace ut { + struct quoted_tcase { + quoted_tcase() = default; + quoted_tcase(std::string x, bool unq_flag, std::string s) + : x_{x}, unq_flag_{unq_flag}, s_{std::move(s)} {} + + /* string to be printed-in-machine-readable-form */ + std::string x_; + /* if true: omit surrounding " chars when unambiguous + * (printed string does not contain spaces or escaped chars) + * if false: always require surrounding " chars + */ + bool unq_flag_ = true; + /* expected result */ + std::string s_; + }; /*quoted_tcase*/ + + std::vector s_quoted_tcase_v( + { + quoted_tcase("", true, "\"\""), + quoted_tcase("", false, "\"\""), + + quoted_tcase("foo", true, "foo"), + quoted_tcase("foo", false, "\"foo\""), + + quoted_tcase("foo\n", true, "\"foo\\\n\""), + quoted_tcase("foo\n", false, "\"foo\\\n\""), + + quoted_tcase("two words", true, "\"two words\""), + quoted_tcase("two words", false, "\"two words\""), + + quoted_tcase("1st\n2nd", true, "\"1st\\\n2nd\""), + quoted_tcase("1st\n2nd", true, "\"1st\\\n2nd\""), + + quoted_tcase("misakte\rfix", true, "\"misakte\\\rfix\""), + quoted_tcase("misakte\rfix", true, "\"misakte\\\rfix\""), + + quoted_tcase("\"oh!\", she said", true, "\"\\\"oh!\\\", she said\""), + quoted_tcase("\"oh!\", she said", false, "\"\\\"oh!\\\", she said\""), + + quoted_tcase("", true, ""), + quoted_tcase("", false, ""), + }); + + TEST_CASE("quoted", "[quoted]") { + for (std::uint32_t i_tc = 0, z_tc = s_quoted_tcase_v.size(); i_tc < z_tc; ++i_tc) { + quoted_tcase const & tc = s_quoted_tcase_v[i_tc]; + + INFO(tostr(xtag("i_tc", i_tc), xtag("x", tc.x_), xtag("unq_flag", tc.unq_flag_))); + + std::stringstream ss; + if (tc.unq_flag_) + ss << unq(tc.x_); + else + ss << quoted(tc.x_); + + INFO(xtag("ss.str", ss.str())); + + REQUIRE(ss.str() == tc.s_); + } + + REQUIRE(s_quoted_tcase_v.size() > 1); + } +} /*namespace ut*/ + +/* end quoted.test.cpp */