diff options
-rw-r--r-- | Swift/Controllers/PreviousStatusStore.cpp | 47 | ||||
-rw-r--r-- | Swift/Controllers/PreviousStatusStore.h | 22 | ||||
-rw-r--r-- | Swift/Controllers/SConscript | 4 | ||||
-rw-r--r-- | Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp | 39 | ||||
-rw-r--r-- | Swiften/Base/String.h | 4 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/StringTest.cpp | 16 |
6 files changed, 131 insertions, 1 deletions
diff --git a/Swift/Controllers/PreviousStatusStore.cpp b/Swift/Controllers/PreviousStatusStore.cpp new file mode 100644 index 0000000..42f2124 --- /dev/null +++ b/Swift/Controllers/PreviousStatusStore.cpp @@ -0,0 +1,47 @@ +#include "PreviousStatusStore.h" + +#include "Swiften/Base/foreach.h" + +namespace Swift { + +PreviousStatusStore::PreviousStatusStore() { + +} + +PreviousStatusStore::~PreviousStatusStore() { + +} + +void PreviousStatusStore::addStatus(StatusShow::Type status, const String& message) { + //FIXME: remove old entries + store_.push_back(TypeStringPair(status, message)); +} + +std::vector<TypeStringPair> PreviousStatusStore::exactMatchSuggestions(StatusShow::Type status, const String& message) { + std::vector<TypeStringPair> suggestions; + suggestions.push_back(TypeStringPair(status, message)); + return suggestions; +} + +std::vector<TypeStringPair> PreviousStatusStore::getSuggestions(const String& message) { + std::vector<TypeStringPair> suggestions; + foreach (TypeStringPair status, store_) { + if (status.second == message) { + suggestions.clear(); + suggestions.push_back(status); + break; + } else if (status.second.contains(message)) { + suggestions.push_back(status); + } + } + if (suggestions.size() == 0) { + TypeStringPair suggestion(StatusShow::Online, message); + suggestions.push_back(suggestion); + } + if (suggestions.size() == 1) { + suggestions = exactMatchSuggestions(suggestions[0].first, suggestions[0].second); + } + return suggestions; +} + +} diff --git a/Swift/Controllers/PreviousStatusStore.h b/Swift/Controllers/PreviousStatusStore.h new file mode 100644 index 0000000..476febe --- /dev/null +++ b/Swift/Controllers/PreviousStatusStore.h @@ -0,0 +1,22 @@ +#pragma once + +#include <utility> /* std::pair */ +#include <vector> + +#include "Swiften/Base/String.h" +#include "Swiften/Elements/StatusShow.h" + +namespace Swift { + typedef std::pair<StatusShow::Type, String> TypeStringPair; + class PreviousStatusStore { + public: + PreviousStatusStore(); + ~PreviousStatusStore(); + void addStatus(StatusShow::Type status, const String& message); + std::vector<TypeStringPair> getSuggestions(const String& message); + + private: + std::vector<TypeStringPair> exactMatchSuggestions(StatusShow::Type status, const String& message); + std::vector<TypeStringPair> store_; + }; +} diff --git a/Swift/Controllers/SConscript b/Swift/Controllers/SConscript index 79357b5..02d0b52 100644 --- a/Swift/Controllers/SConscript +++ b/Swift/Controllers/SConscript @@ -1,30 +1,32 @@ Import("env") env["SWIFT_CONTROLLERS_FLAGS"] = { "LIBPATH": [Dir(".")], "LIBS": ["SwiftControllers"] } myenv = env.Clone() myenv.MergeFlags(env["BOOST_FLAGS"]) myenv.StaticLibrary("SwiftControllers", [ "ChatController.cpp", "ChatControllerBase.cpp", "MainController.cpp", "NickResolver.cpp", "RosterController.cpp", "XMPPRosterController.cpp", "MUCController.cpp", "EventController.cpp", "SoundEventController.cpp", "SystemTrayController.cpp", "XMLConsoleController.cpp", "UIEvents/UIEvent.cpp", "UIInterfaces/XMLConsoleWidget.cpp", + "PreviousStatusStore.cpp", ]) env.Append(UNITTEST_SOURCES = [ File("UnitTest/NickResolverTest.cpp"), File("UnitTest/RosterControllerTest.cpp"), - File("UnitTest/XMPPRosterControllerTest.cpp") + File("UnitTest/XMPPRosterControllerTest.cpp"), + File("UnitTest/PreviousStatusStoreTest.cpp"), ]) diff --git a/Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp b/Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp new file mode 100644 index 0000000..c9e55e4 --- /dev/null +++ b/Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp @@ -0,0 +1,39 @@ +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include "Swift/Controllers/PreviousStatusStore.h" + +using namespace Swift; + +class PreviousStatusStoreTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(PreviousStatusStoreTest); + CPPUNIT_TEST(testGetAll); + //CPPUNIT_TEST(testGetAllLimited); + //CPPUNIT_TEST(testGetSuggestionsInexact); + //CPPUNIT_TEST(testGetSuggestionsExact); + CPPUNIT_TEST_SUITE_END(); + +public: + PreviousStatusStoreTest() {}; + + void setUp() { + store_ = new PreviousStatusStore(); + store_->addStatus(StatusShow::Online, "At home in the study"); + store_->addStatus(StatusShow::DND, "In a meeting"); + store_->addStatus(StatusShow::DND, "With a client"); + store_->addStatus(StatusShow::Away, "Walking the elephant"); + store_->addStatus(StatusShow::Online, "In the office, at my desk"); + } + + void tearDown() { + + } + + void testGetAll() { + + } + +private: + PreviousStatusStore* store_; +}; diff --git a/Swiften/Base/String.h b/Swiften/Base/String.h index 0a5530c..7a6a9cc 100644 --- a/Swiften/Base/String.h +++ b/Swiften/Base/String.h @@ -49,79 +49,83 @@ namespace Swift { bool beginsWith(char c) const { return data_.size() > 0 && data_[0] == c; } bool beginsWith(const String& s) const { return data_.substr(0, s.data_.size()) == s; } bool endsWith(char c) const { return data_.size() > 0 && data_[data_.size()-1] == c; } String getSubstring(size_t begin, size_t end) const { return String(data_.substr(begin, end)); } size_t find(char c) const { assert((c & 0x80) == 0); return data_.find(c); } size_t npos() const { return data_.npos; } friend String operator+(const String& a, const String& b) { return String(a.data_ + b.data_); } friend String operator+(const String& a, char b) { return String(a.data_ + b); } String& operator+=(const String& o) { data_ += o.data_; return *this; } String& operator+=(char c) { data_ += c; return *this; } String& operator=(const String& o) { data_ = o.data_; return *this; } + bool contains(const String& o) { + return data_.find(o.data_) != std::string::npos; + } + char operator[](size_t i) const { return data_[i]; } friend bool operator>(const String& a, const String& b) { return a.data_ > b.data_; } friend bool operator<(const String& a, const String& b) { return a.data_ < b.data_; } friend bool operator!=(const String& a, const String& b) { return a.data_ != b.data_; } friend bool operator==(const String& a, const String& b) { return a.data_ == b.data_; } friend std::ostream& operator<<(std::ostream& os, const String& s) { os << s.data_; return os; } private: std::string data_; }; } #endif diff --git a/Swiften/Base/UnitTest/StringTest.cpp b/Swiften/Base/UnitTest/StringTest.cpp index 6ad12fd..87e1a99 100644 --- a/Swiften/Base/UnitTest/StringTest.cpp +++ b/Swiften/Base/UnitTest/StringTest.cpp @@ -1,75 +1,78 @@ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> #include "Swiften/Base/String.h" using namespace Swift; class StringTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(StringTest); CPPUNIT_TEST(testGetLength); CPPUNIT_TEST(testGetLength_EncodedLength2); CPPUNIT_TEST(testGetLength_EncodedLength3); CPPUNIT_TEST(testGetLength_EncodedLength4); CPPUNIT_TEST(testGetUnicodeCodePoints); CPPUNIT_TEST(testGetSplittedAtFirst); CPPUNIT_TEST(testGetSplittedAtFirst_CharacterAtEnd); CPPUNIT_TEST(testGetSplittedAtFirst_NoSuchCharacter); CPPUNIT_TEST(testRemoveAll); CPPUNIT_TEST(testRemoveAll_LastChar); CPPUNIT_TEST(testRemoveAll_ConsecutiveChars); CPPUNIT_TEST(testReplaceAll); CPPUNIT_TEST(testReplaceAll_LastChar); CPPUNIT_TEST(testReplaceAll_ConsecutiveChars); CPPUNIT_TEST(testReplaceAll_MatchingReplace); CPPUNIT_TEST(testGetLowerCase); CPPUNIT_TEST(testSplit); + CPPUNIT_TEST(testContains); + CPPUNIT_TEST(testContainsFalse); + CPPUNIT_TEST(testContainsExact); CPPUNIT_TEST_SUITE_END(); public: StringTest() {} void testGetLength() { String testling("xyz$xyz"); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(7), testling.getLength()); } void testGetLength_EncodedLength2() { String testling("xyz\xC2\xA2xyz"); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(7), testling.getLength()); } void testGetLength_EncodedLength3() { String testling("xyz\xE2\x82\xACxyz"); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(7), testling.getLength()); } void testGetLength_EncodedLength4() { String testling("xyz\xf4\x8a\xaf\x8dxyz"); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(7), testling.getLength()); } void testGetUnicodeCodePoints() { String testling("$\xc2\xa2\xe2\x82\xac\xf4\x8a\xaf\x8d"); std::vector<unsigned int> points = testling.getUnicodeCodePoints(); CPPUNIT_ASSERT_EQUAL(0x24U, points[0]); CPPUNIT_ASSERT_EQUAL(0xA2U, points[1]); CPPUNIT_ASSERT_EQUAL(0x20ACU, points[2]); CPPUNIT_ASSERT_EQUAL(0x10ABCDU, points[3]); } void testGetSplittedAtFirst() { String testling("ab@cd@ef"); std::pair<String,String> result = testling.getSplittedAtFirst('@'); CPPUNIT_ASSERT_EQUAL(String("ab"), result.first); CPPUNIT_ASSERT_EQUAL(String("cd@ef"), result.second); } void testGetSplittedAtFirst_CharacterAtEnd() { @@ -113,51 +116,64 @@ class StringTest : public CppUnit::TestFixture } void testReplaceAll() { String testling("abcbd"); testling.replaceAll('b', "xyz"); CPPUNIT_ASSERT_EQUAL(String("axyzcxyzd"), testling); } void testReplaceAll_LastChar() { String testling("abc"); testling.replaceAll('c', "xyz"); CPPUNIT_ASSERT_EQUAL(String("abxyz"), testling); } void testReplaceAll_ConsecutiveChars() { String testling("abbc"); testling.replaceAll('b',"xyz"); CPPUNIT_ASSERT_EQUAL(String("axyzxyzc"), testling); } void testReplaceAll_MatchingReplace() { String testling("abc"); testling.replaceAll('b',"bbb"); CPPUNIT_ASSERT_EQUAL(String("abbbc"), testling); } void testGetLowerCase() { String testling("aBcD e"); CPPUNIT_ASSERT_EQUAL(String("abcd e"), testling.getLowerCase()); } void testSplit() { std::vector<String> result = String("abc def ghi").split(' '); CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(result.size())); CPPUNIT_ASSERT_EQUAL(String("abc"), result[0]); CPPUNIT_ASSERT_EQUAL(String("def"), result[1]); CPPUNIT_ASSERT_EQUAL(String("ghi"), result[2]); } + + void testContains() { + CPPUNIT_ASSERT(String("abcde").contains(String("bcd"))); + } + + void testContainsFalse() { + CPPUNIT_ASSERT(!String("abcde").contains(String("abcdef"))); + } + + void testContainsExact() { + CPPUNIT_ASSERT(String("abcde").contains(String("abcde"))); + } + }; CPPUNIT_TEST_SUITE_REGISTRATION(StringTest); |