2023-10-15 00:56:26 +00:00
|
|
|
///
|
2023-10-20 03:32:46 +00:00
|
|
|
/// \file test/stringutil_test.cc
|
2023-10-15 00:56:26 +00:00
|
|
|
/// \author kyle
|
2023-10-16 09:18:09 +00:00
|
|
|
/// \date 10/14/23
|
2023-10-15 00:56:26 +00:00
|
|
|
/// \brief Ensure the stringutil functions work.
|
|
|
|
///
|
|
|
|
/// Copyright 2023 K. Isom <kyle@imap.cc>
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
|
|
|
|
|
2023-10-20 03:32:46 +00:00
|
|
|
#include <functional>
|
2023-10-15 00:56:26 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
#include <scsl/Flags.h>
|
2023-10-19 06:44:05 +00:00
|
|
|
#include <scsl/StringUtil.h>
|
|
|
|
#include <sctest/Assert.h>
|
2023-10-20 03:32:46 +00:00
|
|
|
#include <sctest/SimpleSuite.h>
|
2023-10-16 09:18:09 +00:00
|
|
|
|
|
|
|
|
2023-10-15 01:38:01 +00:00
|
|
|
using namespace scsl;
|
2023-10-15 00:56:26 +00:00
|
|
|
|
|
|
|
|
2023-10-20 03:32:46 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2023-10-15 00:56:26 +00:00
|
|
|
TestTrimming(std::string line, std::string lExpected, std::string rExpected, std::string expected)
|
|
|
|
{
|
2023-10-16 09:18:09 +00:00
|
|
|
std::string result;
|
|
|
|
std::string message;
|
2023-10-15 00:56:26 +00:00
|
|
|
|
2023-10-21 09:07:59 +00:00
|
|
|
result = string::TrimLeadingWhitespaceDup(line);
|
2023-10-15 00:56:26 +00:00
|
|
|
message = "TrimLeadingDup(\"" + line + "\"): '" + result + "'";
|
2023-10-19 07:37:56 +00:00
|
|
|
sctest::Assert(result == lExpected, message);
|
2023-10-15 00:56:26 +00:00
|
|
|
|
2023-10-21 09:07:59 +00:00
|
|
|
result = string::TrimTrailingWhitespaceDup(line);
|
2023-10-15 00:56:26 +00:00
|
|
|
message = "TrimTrailingDup(\"" + line + "\"): '" + result + "'";
|
2023-10-19 07:37:56 +00:00
|
|
|
sctest::Assert(result == rExpected, message);
|
2023-10-15 00:56:26 +00:00
|
|
|
|
2023-10-21 09:07:59 +00:00
|
|
|
result = string::TrimWhitespaceDup(line);
|
2023-10-15 00:56:26 +00:00
|
|
|
message = "TrimDup(\"" + line + "\"): '" + result + "'";
|
2023-10-19 07:37:56 +00:00
|
|
|
sctest::Assert(result == expected, message);
|
2023-10-15 00:56:26 +00:00
|
|
|
|
|
|
|
result = line;
|
2023-10-21 09:07:59 +00:00
|
|
|
string::TrimLeadingWhitespace(result);
|
2023-10-15 00:56:26 +00:00
|
|
|
message = "TrimLeadingDup(\"" + line + "\"): '" + result + "'";
|
2023-10-19 07:37:56 +00:00
|
|
|
sctest::Assert(result == lExpected, message);
|
2023-10-15 00:56:26 +00:00
|
|
|
|
|
|
|
result = line;
|
2023-10-21 09:07:59 +00:00
|
|
|
string::TrimTrailingWhitespace(result);
|
2023-10-15 00:56:26 +00:00
|
|
|
message = "TrimTrailingDup(\"" + line + "\"): '" + result + "'";
|
2023-10-19 07:37:56 +00:00
|
|
|
sctest::Assert(result == rExpected, message);
|
2023-10-15 00:56:26 +00:00
|
|
|
|
|
|
|
result = line;
|
2023-10-21 09:07:59 +00:00
|
|
|
string::TrimWhitespace(result);
|
2023-10-15 00:56:26 +00:00
|
|
|
message = "TrimDup(\"" + line + "\"): '" + result + "'";
|
2023-10-19 07:37:56 +00:00
|
|
|
sctest::Assert(result == expected, message);
|
2023-10-15 00:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-20 03:32:46 +00:00
|
|
|
std::function<bool()>
|
2023-10-15 00:56:26 +00:00
|
|
|
TestSplit(std::string line, std::string delim, size_t maxCount, std::vector<std::string> expected)
|
|
|
|
{
|
2023-10-20 03:32:46 +00:00
|
|
|
return [line, delim, maxCount, expected]() {
|
2023-10-21 09:07:59 +00:00
|
|
|
return string::SplitN(line, delim, maxCount) == expected;
|
2023-10-20 03:32:46 +00:00
|
|
|
|
|
|
|
};
|
2023-10-15 00:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-20 05:57:55 +00:00
|
|
|
bool
|
|
|
|
TestSplitChar()
|
|
|
|
{
|
|
|
|
auto expected = std::vector<std::string>{"hello", "world"};
|
|
|
|
const auto *inputLine = "hello=world\n";
|
2023-10-21 09:07:59 +00:00
|
|
|
auto actual = string::SplitKeyValuePair(inputLine, '=');
|
2023-10-20 05:57:55 +00:00
|
|
|
|
|
|
|
return actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-20 03:32:46 +00:00
|
|
|
bool
|
2023-10-16 09:18:09 +00:00
|
|
|
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<std::string> expected{
|
|
|
|
"A much longer",
|
|
|
|
"line, something",
|
|
|
|
"that can be",
|
|
|
|
"tested with",
|
|
|
|
"WrapText. Does",
|
|
|
|
"it handle",
|
|
|
|
"puncuation? I",
|
|
|
|
"hope so.",
|
|
|
|
};
|
|
|
|
|
2023-10-21 09:07:59 +00:00
|
|
|
auto wrapped = string::WrapText(testLine, 16);
|
2023-10-20 03:32:46 +00:00
|
|
|
if (wrapped.size() != expected.size()) {
|
2023-10-21 09:07:59 +00:00
|
|
|
std::cerr << string::VectorToString(wrapped)
|
2023-10-20 03:32:46 +00:00
|
|
|
<< " != "
|
2023-10-21 09:07:59 +00:00
|
|
|
<< string::VectorToString(expected)
|
2023-10-20 03:32:46 +00:00
|
|
|
<< "\n";
|
|
|
|
}
|
2023-10-16 09:18:09 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < wrapped.size(); i++) {
|
2023-10-20 03:32:46 +00:00
|
|
|
if (wrapped[i] == expected[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "[" << i << "] \"" << wrapped[i] << "\" != \""
|
|
|
|
<< expected[i] << "\"\n";
|
|
|
|
return false;
|
2023-10-16 09:18:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 09:07:59 +00:00
|
|
|
// string::WriteTabIndented(std::cout, wrapped, 4, true);
|
2023-10-20 03:32:46 +00:00
|
|
|
return true;
|
2023-10-16 09:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-20 03:32:46 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
2023-10-15 00:56:26 +00:00
|
|
|
int
|
2023-10-21 02:05:55 +00:00
|
|
|
main(int argc, char *argv[])
|
2023-10-15 00:56:26 +00:00
|
|
|
{
|
2023-10-21 02:05:55 +00:00
|
|
|
auto noReport = false;
|
|
|
|
auto quiet = false;
|
2023-10-21 09:07:59 +00:00
|
|
|
auto *flags = new scsl::Flags("test_orientation",
|
2023-10-21 02:05:55 +00:00
|
|
|
"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";
|
|
|
|
}
|
|
|
|
|
2023-10-20 03:32:46 +00:00
|
|
|
sctest::SimpleSuite suite;
|
2023-10-21 02:05:55 +00:00
|
|
|
flags->GetBool("-n", noReport);
|
|
|
|
flags->GetBool("-q", quiet);
|
|
|
|
if (quiet) {
|
|
|
|
suite.Silence();
|
|
|
|
}
|
2023-10-20 03:32:46 +00:00
|
|
|
|
2023-10-15 00:56:26 +00:00
|
|
|
TestTrimming(" foo\t ", "foo\t ", " foo", "foo");
|
|
|
|
TestTrimming(" foo\tbar ", "foo\tbar ", " foo\tbar", "foo\tbar");
|
|
|
|
|
2023-10-20 03:32:46 +00:00
|
|
|
suite.AddTest("SplitN(0)", TestSplit("abc:def:ghij:klm", ":", 0,
|
|
|
|
std::vector<std::string>{"abc", "def", "ghij", "klm"}));
|
|
|
|
suite.AddTest("SplitN(3)", TestSplit("abc:def:ghij:klm", ":", 3,
|
|
|
|
std::vector<std::string>{"abc", "def", "ghij:klm"}));
|
|
|
|
suite.AddTest("SplitN(2)", TestSplit("abc:def:ghij:klm", ":", 2,
|
|
|
|
std::vector<std::string>{"abc", "def:ghij:klm"}));
|
|
|
|
suite.AddTest("SplitN(1)", TestSplit("abc:def:ghij:klm", ":", 1,
|
|
|
|
std::vector<std::string>{"abc:def:ghij:klm"}));
|
|
|
|
suite.AddTest("SplitN(0) with empty element",
|
|
|
|
TestSplit("abc::def:ghi", ":", 0,
|
|
|
|
std::vector<std::string>{"abc", "", "def", "ghi"}));
|
2023-10-20 05:57:55 +00:00
|
|
|
suite.AddTest("TestSplitKV(char)", TestSplitChar);
|
2023-10-20 03:32:46 +00:00
|
|
|
suite.AddTest("TextWrapping", TestWrapping);
|
2023-10-21 02:05:55 +00:00
|
|
|
|
|
|
|
delete flags;
|
2023-10-20 03:32:46 +00:00
|
|
|
auto result = suite.Run();
|
2023-10-21 02:05:55 +00:00
|
|
|
if (!noReport) { std::cout << suite.GetReport() << "\n"; }
|
2023-10-20 03:32:46 +00:00
|
|
|
return result ? 0 : 1;
|
2023-10-15 00:56:26 +00:00
|
|
|
}
|