[Libreoffice-commits] online.git: common/Protocol.hpp test/WhiteBoxTests.cpp
Ashod Nakashian
ashod.nakashian at collabora.co.uk
Mon Dec 19 05:57:23 UTC 2016
common/Protocol.hpp | 33 ++++++++++++++++++++++++++-------
test/WhiteBoxTests.cpp | 38 ++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+), 7 deletions(-)
New commits:
commit 2428a4e3a107feb94128c88e1420eb86a670c6ff
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date: Sun Dec 18 13:09:13 2016 -0500
wsd: more Protocol unittests
Change-Id: I72742f767d9dcfcf1edbfaed4fada628ebef156f
Reviewed-on: https://gerrit.libreoffice.org/32159
Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
Tested-by: Ashod Nakashian <ashnakash at gmail.com>
diff --git a/common/Protocol.hpp b/common/Protocol.hpp
index 31cb16e..fa7ba7a 100644
--- a/common/Protocol.hpp
+++ b/common/Protocol.hpp
@@ -120,16 +120,22 @@ namespace LOOLProtocol
return tokenize(s.data(), s.size());
}
- inline
- std::string getDelimitedInitialSubstring(const char *message, const int length, const char delim)
+ inline size_t getDelimiterPosition(const char* message, const int length, const char delim)
{
- if (message == nullptr || length <= 0)
+ if (message && length > 0)
{
- return "";
+ const char *founddelim = static_cast<const char *>(std::memchr(message, delim, length));
+ const auto size = (founddelim == nullptr ? length : founddelim - message);
+ return size;
}
- const char *founddelim = static_cast<const char *>(std::memchr(message, delim, length));
- const auto size = (founddelim == nullptr ? length : founddelim - message);
+ return 0;
+ }
+
+ inline
+ std::string getDelimitedInitialSubstring(const char *message, const int length, const char delim)
+ {
+ const auto size = getDelimiterPosition(message, length, delim);
return std::string(message, size);
}
@@ -221,7 +227,7 @@ namespace LOOLProtocol
{
if (message == nullptr || length <= 0)
{
- return "";
+ return std::string();
}
const auto firstLine = getFirstLine(message, std::min(length, 120));
@@ -235,6 +241,19 @@ namespace LOOLProtocol
return firstLine;
}
+ inline std::string getAbbreviatedMessage(const std::string& message)
+ {
+ const auto pos = getDelimiterPosition(message.data(), std::min(message.size(), 120UL), '\n');
+
+ // If first line is less than the length (minus newline), add ellipsis.
+ if (pos < static_cast<std::string::size_type>(message.size()) - 1)
+ {
+ return message.substr(0, pos) + "...";
+ }
+
+ return message;
+ }
+
template <typename T>
std::string getAbbreviatedMessage(const T& message)
{
diff --git a/test/WhiteBoxTests.cpp b/test/WhiteBoxTests.cpp
index 03cf59b..fae37b6 100644
--- a/test/WhiteBoxTests.cpp
+++ b/test/WhiteBoxTests.cpp
@@ -24,6 +24,7 @@ class WhiteBoxTests : public CPPUNIT_NS::TestFixture
CPPUNIT_TEST_SUITE(WhiteBoxTests);
CPPUNIT_TEST(testLOOLProtocolFunctions);
+ CPPUNIT_TEST(testMessageAbbreviation);
CPPUNIT_TEST(testTokenizer);
CPPUNIT_TEST(testRegexListMatcher);
CPPUNIT_TEST(testRegexListMatcher_Init);
@@ -32,6 +33,7 @@ class WhiteBoxTests : public CPPUNIT_NS::TestFixture
CPPUNIT_TEST_SUITE_END();
void testLOOLProtocolFunctions();
+ void testMessageAbbreviation();
void testTokenizer();
void testRegexListMatcher();
void testRegexListMatcher_Init();
@@ -126,6 +128,42 @@ void WhiteBoxTests::testLOOLProtocolFunctions()
CPPUNIT_ASSERT_EQUAL(std::string(""), Util::trim(s));
}
+void WhiteBoxTests::testMessageAbbreviation()
+{
+ CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getDelimitedInitialSubstring(nullptr, 5, '\n'));
+ CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getDelimitedInitialSubstring(nullptr, -1, '\n'));
+ CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getDelimitedInitialSubstring("abc", 0, '\n'));
+ CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getDelimitedInitialSubstring("abc", -1, '\n'));
+ CPPUNIT_ASSERT_EQUAL(std::string("ab"), LOOLProtocol::getDelimitedInitialSubstring("abc", 2, '\n'));
+
+ CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getAbbreviatedMessage(nullptr, 5));
+ CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getAbbreviatedMessage(nullptr, -1));
+ CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getAbbreviatedMessage("abc", 0));
+ CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getAbbreviatedMessage("abc", -1));
+ CPPUNIT_ASSERT_EQUAL(std::string("ab"), LOOLProtocol::getAbbreviatedMessage("abc", 2));
+
+ std::string s;
+ std::string abbr;
+
+ s = "abcdefg";
+ CPPUNIT_ASSERT_EQUAL(s, LOOLProtocol::getAbbreviatedMessage(s));
+
+ s = "1234567890123\n45678901234567890123456789012345678901234567890123";
+ abbr = "1234567890123...";
+ CPPUNIT_ASSERT_EQUAL(abbr, LOOLProtocol::getAbbreviatedMessage(s.data(), s.size()));
+ CPPUNIT_ASSERT_EQUAL(abbr, LOOLProtocol::getAbbreviatedMessage(s));
+
+ // 120 characters. Change when the abbreviation max-length changes.
+ s = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
+ CPPUNIT_ASSERT_EQUAL(s, LOOLProtocol::getAbbreviatedMessage(s.data(), s.size()));
+ CPPUNIT_ASSERT_EQUAL(s, LOOLProtocol::getAbbreviatedMessage(s));
+
+ abbr = s + "...";
+ s += "more data";
+ CPPUNIT_ASSERT_EQUAL(abbr, LOOLProtocol::getAbbreviatedMessage(s.data(), s.size()));
+ CPPUNIT_ASSERT_EQUAL(abbr, LOOLProtocol::getAbbreviatedMessage(s));
+}
+
void WhiteBoxTests::testTokenizer()
{
std::vector<std::string> tokens;
More information about the Libreoffice-commits
mailing list