/* * Copyright (c) 2013 Kevin Smith * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include #include #include #include using namespace Swift; class ChatMessageParserTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(ChatMessageParserTest); CPPUNIT_TEST(testFullBody); CPPUNIT_TEST_SUITE_END(); public: void setUp() { smile1_ = ":)"; smile1Path_ = "/blah/smile1.png"; smile2_ = ":("; smile2Path_ = "/blah/smile2.jpg"; emoticons_[smile1_] = smile1Path_; emoticons_[smile2_] = smile2Path_; } void tearDown() { emoticons_.clear(); } void assertText(const ChatWindow::ChatMessage& result, size_t index, const std::string& text) { boost::shared_ptr part = boost::dynamic_pointer_cast(result.getParts()[index]); CPPUNIT_ASSERT_EQUAL(text, part->text); } void assertEmoticon(const ChatWindow::ChatMessage& result, size_t index, const std::string& text, const std::string& path) { boost::shared_ptr part = boost::dynamic_pointer_cast(result.getParts()[index]); CPPUNIT_ASSERT_EQUAL(text, part->alternativeText); CPPUNIT_ASSERT_EQUAL(path, part->imagePath); } void assertHighlight(const ChatWindow::ChatMessage& result, size_t index, const std::string& text) { boost::shared_ptr part = boost::dynamic_pointer_cast(result.getParts()[index]); CPPUNIT_ASSERT_EQUAL(text, part->text); } void assertURL(const ChatWindow::ChatMessage& result, size_t index, const std::string& text) { boost::shared_ptr part = boost::dynamic_pointer_cast(result.getParts()[index]); CPPUNIT_ASSERT_EQUAL(text, part->target); } static HighlightRule ruleFromKeyword(const std::string& keyword) { HighlightRule rule; std::vector keywords; keywords.push_back(keyword); rule.setKeywords(keywords); return rule; } void testFullBody() { /* initialize rules */ std::vector rules; rules.push_back(ruleFromKeyword("trigger")); ChatMessageParser testling(emoticons_, rules); ChatWindow::ChatMessage result = testling.parseMessageBody(":) shiny :( trigger :) http://wonderland.lit/blah http://denmark.lit boom boom"); assertEmoticon(result, 0, smile1_, smile1Path_); assertText(result, 1, " shiny "); assertEmoticon(result, 2, smile2_, smile2Path_); assertText(result, 3, " "); assertHighlight(result, 4, "trigger"); assertText(result, 5, " "); assertEmoticon(result, 6, smile1_, smile1Path_); assertText(result, 7, " "); assertURL(result, 8, "http://wonderland.lit/blah"); assertText(result, 9, " "); assertURL(result, 10, "http://denmark.lit"); assertText(result, 11, " boom boom"); } private: std::map emoticons_; std::string smile1_; std::string smile1Path_; std::string smile2_; std::string smile2Path_; }; CPPUNIT_TEST_SUITE_REGISTRATION(ChatMessageParserTest);