/// /// \file test/stringutil_test.cc /// \author kyle /// \date 10/14/23 /// \brief Ensure the stringutil functions work. /// /// Copyright 2023 K. Isom /// /// Permission to use, copy, modify, and/or distribute this software for /// any purpose with or without fee is hereby granted, provided that the /// above copyright notice and this permission notice appear in all copies. /// /// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL /// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED /// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR /// BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES /// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, /// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, /// ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS /// SOFTWARE. /// #include #include #include #include #include #include using namespace scsl; namespace { void TestTrimming(std::string line, std::string lExpected, std::string rExpected, std::string expected) { std::string result; std::string message; result = scstring::TrimLeadingWhitespaceDup(line); message = "TrimLeadingDup(\"" + line + "\"): '" + result + "'"; sctest::Assert(result == lExpected, message); result = scstring::TrimTrailingWhitespaceDup(line); message = "TrimTrailingDup(\"" + line + "\"): '" + result + "'"; sctest::Assert(result == rExpected, message); result = scstring::TrimWhitespaceDup(line); message = "TrimDup(\"" + line + "\"): '" + result + "'"; sctest::Assert(result == expected, message); result = line; scstring::TrimLeadingWhitespace(result); message = "TrimLeadingDup(\"" + line + "\"): '" + result + "'"; sctest::Assert(result == lExpected, message); result = line; scstring::TrimTrailingWhitespace(result); message = "TrimTrailingDup(\"" + line + "\"): '" + result + "'"; sctest::Assert(result == rExpected, message); result = line; scstring::TrimWhitespace(result); message = "TrimDup(\"" + line + "\"): '" + result + "'"; sctest::Assert(result == expected, message); } std::function TestSplit(std::string line, std::string delim, size_t maxCount, std::vector expected) { return [line, delim, maxCount, expected]() { return scstring::SplitN(line, delim, maxCount) == expected; }; } bool TestSplitChar() { auto expected = std::vector{"hello", "world"}; const auto *inputLine = "hello=world\n"; auto actual = scstring::SplitKeyValuePair(inputLine, '='); return actual == expected; } bool TestWrapping() { std::string testLine = "A much longer line, something that can be tested with WrapText. "; testLine += "Does it handle puncuation? I hope so."; std::vector expected{ "A much longer", "line, something", "that can be", "tested with", "WrapText. Does", "it handle", "puncuation? I", "hope so.", }; auto wrapped = scstring::WrapText(testLine, 16); if (wrapped.size() != expected.size()) { std::cerr << scstring::VectorToString(wrapped) << " != " << scstring::VectorToString(expected) << "\n"; } for (size_t i = 0; i < wrapped.size(); i++) { if (wrapped[i] == expected[i]) { continue; } std::cerr << "[" << i << "] \"" << wrapped[i] << "\" != \"" << expected[i] << "\"\n"; return false; } // scstring::WriteTabIndented(std::cout, wrapped, 4, true); return true; } } // anonymous namespace int main(int argc, char *argv[]) { auto noReport = false; auto quiet = false; auto *flags = new scsl::Flags("test_orientation", "This test validates various orientation-related components in scmp."); flags->Register("-n", false, "don't print the report"); flags->Register("-q", false, "suppress test output"); auto parsed = flags->Parse(argc, argv); if (parsed != scsl::Flags::ParseStatus::OK) { std::cerr << "Failed to parse flags: " << scsl::Flags::ParseStatusToString(parsed) << "\n"; } sctest::SimpleSuite suite; flags->GetBool("-n", noReport); flags->GetBool("-q", quiet); if (quiet) { suite.Silence(); } TestTrimming(" foo\t ", "foo\t ", " foo", "foo"); TestTrimming(" foo\tbar ", "foo\tbar ", " foo\tbar", "foo\tbar"); suite.AddTest("SplitN(0)", TestSplit("abc:def:ghij:klm", ":", 0, std::vector{"abc", "def", "ghij", "klm"})); suite.AddTest("SplitN(3)", TestSplit("abc:def:ghij:klm", ":", 3, std::vector{"abc", "def", "ghij:klm"})); suite.AddTest("SplitN(2)", TestSplit("abc:def:ghij:klm", ":", 2, std::vector{"abc", "def:ghij:klm"})); suite.AddTest("SplitN(1)", TestSplit("abc:def:ghij:klm", ":", 1, std::vector{"abc:def:ghij:klm"})); suite.AddTest("SplitN(0) with empty element", TestSplit("abc::def:ghi", ":", 0, std::vector{"abc", "", "def", "ghi"})); suite.AddTest("TestSplitKV(char)", TestSplitChar); suite.AddTest("TextWrapping", TestWrapping); delete flags; auto result = suite.Run(); if (!noReport) { std::cout << suite.GetReport() << "\n"; } return result ? 0 : 1; }