loolwsd/LOOLProtocol.cpp | 18 +++++++++++++++++ loolwsd/LOOLProtocol.hpp | 4 +++ loolwsd/test/WhiteBoxTests.cpp | 42 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+)
New commits: commit 6081160bd45bccf6effa7be4b0250a1f0bb8d5f3 Author: Tor Lillqvist <t...@collabora.com> Date: Mon Sep 26 19:31:19 2016 +0300 Add LOOLProtocol::getToken*FromMessage() Tokenizes a string and looks for the requested named parameter (in the form name=value, like the other getToken* functions). Also add a unit test for the getToken* functions. diff --git a/loolwsd/LOOLProtocol.cpp b/loolwsd/LOOLProtocol.cpp index ec820db..7e08864 100644 --- a/loolwsd/LOOLProtocol.cpp +++ b/loolwsd/LOOLProtocol.cpp @@ -174,6 +174,24 @@ namespace LOOLProtocol return false; } + bool getTokenIntegerFromMessage(const std::string& message, const std::string& name, int& value) + { + Poco::StringTokenizer tokens(message, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM); + return getTokenInteger(tokens, name, value); + } + + bool getTokenStringFromMessage(const std::string& message, const std::string& name, std::string& value) + { + Poco::StringTokenizer tokens(message, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM); + return getTokenString(tokens, name, value); + } + + bool getTokenKeywordFromMessage(const std::string& message, const std::string& name, const std::map<std::string, int>& map, int& value) + { + Poco::StringTokenizer tokens(message, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM); + return getTokenKeyword(tokens, name, map, value); + } + bool parseStatus(const std::string& message, LibreOfficeKitDocumentType& type, int& nParts, int& currentPart, int& width, int& height) { StringTokenizer tokens(message, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM); diff --git a/loolwsd/LOOLProtocol.hpp b/loolwsd/LOOLProtocol.hpp index c7c42e1..d4fb8a6 100644 --- a/loolwsd/LOOLProtocol.hpp +++ b/loolwsd/LOOLProtocol.hpp @@ -49,6 +49,10 @@ namespace LOOLProtocol bool getTokenString(const Poco::StringTokenizer& tokens, const std::string& name, std::string& value); bool getTokenKeyword(const Poco::StringTokenizer& tokens, const std::string& name, const std::map<std::string, int>& map, int& value); + bool getTokenIntegerFromMessage(const std::string& message, const std::string& name, int& value); + bool getTokenStringFromMessage(const std::string& message, const std::string& name, std::string& value); + bool getTokenKeywordFromMessage(const std::string& message, const std::string& name, const std::map<std::string, int>& map, int& value); + // Functions that parse messages. All return false if parsing fails bool parseStatus(const std::string& message, LibreOfficeKitDocumentType& type, int& nParts, int& currentPart, int& width, int& height); diff --git a/loolwsd/test/WhiteBoxTests.cpp b/loolwsd/test/WhiteBoxTests.cpp index 606abfb..15c5b27 100644 --- a/loolwsd/test/WhiteBoxTests.cpp +++ b/loolwsd/test/WhiteBoxTests.cpp @@ -12,6 +12,7 @@ #include <cppunit/extensions/HelperMacros.h> #include <Common.hpp> +#include <LOOLProtocol.hpp> #include <Util.hpp> /// WhiteBox unit-tests. @@ -19,15 +20,56 @@ class WhiteBoxTests : public CPPUNIT_NS::TestFixture { CPPUNIT_TEST_SUITE(WhiteBoxTests); + CPPUNIT_TEST(testLOOLProtocolFunctions); CPPUNIT_TEST(testRegexListMatcher); CPPUNIT_TEST(testRegexListMatcher_Init); CPPUNIT_TEST_SUITE_END(); + void testLOOLProtocolFunctions(); void testRegexListMatcher(); void testRegexListMatcher_Init(); }; +void WhiteBoxTests::testLOOLProtocolFunctions() +{ + int foo; + CPPUNIT_ASSERT(LOOLProtocol::getTokenInteger("foo=42", "foo", foo)); + CPPUNIT_ASSERT_EQUAL(42, foo); + + std::string bar; + CPPUNIT_ASSERT(LOOLProtocol::getTokenString("bar=hello-sailor", "bar", bar)); + CPPUNIT_ASSERT_EQUAL(std::string("hello-sailor"), bar); + + int mumble; + std::map<std::string, int> map { { "hello", 1 }, { "goodbye", 2 }, { "adieu", 3 } }; + + CPPUNIT_ASSERT(LOOLProtocol::getTokenKeyword("mumble=goodbye", "mumble", map, mumble)); + CPPUNIT_ASSERT_EQUAL(2, mumble); + + std::string message("hello x=1 y=2 foo=42 bar=hello-sailor mumble=goodbye zip zap"); + Poco::StringTokenizer tokens(message, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM); + + CPPUNIT_ASSERT(LOOLProtocol::getTokenInteger(tokens, "foo", foo)); + CPPUNIT_ASSERT_EQUAL(42, foo); + + CPPUNIT_ASSERT(LOOLProtocol::getTokenString(tokens, "bar", bar)); + CPPUNIT_ASSERT_EQUAL(std::string("hello-sailor"), bar); + + CPPUNIT_ASSERT(LOOLProtocol::getTokenKeyword(tokens, "mumble", map, mumble)); + CPPUNIT_ASSERT_EQUAL(2, mumble); + + CPPUNIT_ASSERT(LOOLProtocol::getTokenIntegerFromMessage(message, "foo", foo)); + CPPUNIT_ASSERT_EQUAL(42, foo); + + CPPUNIT_ASSERT(LOOLProtocol::getTokenStringFromMessage(message, "bar", bar)); + CPPUNIT_ASSERT_EQUAL(std::string("hello-sailor"), bar); + + CPPUNIT_ASSERT(LOOLProtocol::getTokenKeywordFromMessage(message, "mumble", map, mumble)); + CPPUNIT_ASSERT_EQUAL(2, mumble); + +} + void WhiteBoxTests::testRegexListMatcher() { Util::RegexListMatcher matcher; _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits