Index: test/UtilForEachTokenTest.cpp |
diff --git a/test/UtilForEachTokenTest.cpp b/test/UtilForEachTokenTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..42e13029b34c5f62bfde6be697e775dd659b2668 |
--- /dev/null |
+++ b/test/UtilForEachTokenTest.cpp |
@@ -0,0 +1,140 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2015 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+#include <cstdint> |
+#include <gtest/gtest.h> |
+#include "../src/shared/Utils.h" |
+ |
+namespace |
+{ |
+ struct TokenHandler |
+ { |
+ bool operator()(std::wstring::const_iterator begin, std::wstring::const_iterator end) |
+ { |
+ tokens.push_back(std::wstring(begin, end)); |
+ return true; |
+ } |
+ std::vector<std::wstring> tokens; |
+ }; |
+} |
+ |
+TEST(ForEachTokenTest, EmptyHandler) |
+{ |
+ ASSERT_THROW(ForEachToken(L"aaa!asdasd!", L'!', |
+ std::function<bool(std::wstring::const_iterator, std::wstring::const_iterator)>()), |
+ std::bad_function_call); |
+} |
+ |
+TEST(ForEachTokenTest, EmptyQueryString) |
+{ |
+ TokenHandler tokenHandler; |
+ ForEachToken(L"", L'\0', std::ref(tokenHandler)); |
+ EXPECT_EQ(0, tokenHandler.tokens.size()); |
+} |
+ |
+TEST(ForEachTokenTest, StringWithoutDelimiter) |
+{ |
+ TokenHandler tokenHandler; |
+ ForEachToken(L"value", L'D', std::ref(tokenHandler)); |
+ ASSERT_EQ(1, tokenHandler.tokens.size()); |
+ EXPECT_EQ(L"value", tokenHandler.tokens[0]); |
+} |
+ |
+TEST(ForEachTokenTest, StringWithDelimiter) |
+{ |
+ TokenHandler tokenHandler; |
+ ForEachToken(L"value1&value2", L'&', std::ref(tokenHandler)); |
+ ASSERT_EQ(2, tokenHandler.tokens.size()); |
+ EXPECT_EQ(L"value1", tokenHandler.tokens[0]); |
+ EXPECT_EQ(L"value2", tokenHandler.tokens[1]); |
+} |
+ |
+TEST(ForEachTokenTest, StringWithDelimiterAtTheEnd) |
+{ |
+ TokenHandler tokenHandler; |
+ ForEachToken(L"value1&value2&", L'&', std::ref(tokenHandler)); |
+ ASSERT_EQ(3, tokenHandler.tokens.size()); |
+ EXPECT_EQ(L"value1", tokenHandler.tokens[0]); |
+ EXPECT_EQ(L"value2", tokenHandler.tokens[1]); |
+ EXPECT_EQ(L"", tokenHandler.tokens[2]); |
+} |
+ |
+TEST(ForEachTokenTest, StringWithDelimiterAtTheBeginning) |
+{ |
+ TokenHandler tokenHandler; |
+ ForEachToken(L"&value1&value2", L'&', std::ref(tokenHandler)); |
+ ASSERT_EQ(3, tokenHandler.tokens.size()); |
+ EXPECT_EQ(L"", tokenHandler.tokens[0]); |
+ EXPECT_EQ(L"value1", tokenHandler.tokens[1]); |
+ EXPECT_EQ(L"value2", tokenHandler.tokens[2]); |
+} |
+ |
+TEST(ForEachTokenTest, StringWithOnlyDelimiters) |
+{ |
+ TokenHandler tokenHandler; |
+ ForEachToken(L"&&&", L'&', std::ref(tokenHandler)); |
+ ASSERT_EQ(4, tokenHandler.tokens.size()); |
+ EXPECT_EQ(L"", tokenHandler.tokens[0]); |
+ EXPECT_EQ(L"", tokenHandler.tokens[1]); |
+ EXPECT_EQ(L"", tokenHandler.tokens[2]); |
+ EXPECT_EQ(L"", tokenHandler.tokens[3]); |
+} |
+ |
+namespace |
+{ |
+ struct LimitedTokenHandler : TokenHandler |
+ { |
+ LimitedTokenHandler(uint32_t limit) |
+ : callCounter(0), limit(limit) |
+ { |
+ } |
+ bool operator()(std::wstring::const_iterator begin, std::wstring::const_iterator end) |
+ { |
+ ++callCounter; |
+ return TokenHandler::operator()(begin, end) && shouldContinue(); |
+ } |
+ bool shouldContinue() const |
+ { |
+ return tokens.size() < limit; |
+ } |
+ uint32_t callCounter; |
+ uint32_t limit; |
+ }; |
+} |
+ |
+TEST(ForEachTokenTest, ProcessUntilHandlerReturnsFalse0) |
+{ |
+ LimitedTokenHandler tokenHandler(3); |
+ ForEachToken(L"&&&&&&", L'&', std::ref(tokenHandler)); |
+ EXPECT_EQ(3, tokenHandler.callCounter); |
+ ASSERT_EQ(3, tokenHandler.tokens.size()); |
+ EXPECT_EQ(L"", tokenHandler.tokens[0]); |
+ EXPECT_EQ(L"", tokenHandler.tokens[1]); |
+ EXPECT_EQ(L"", tokenHandler.tokens[2]); |
+} |
+ |
+TEST(ForEachTokenTest, ProcessUntilHandlerReturnsFalse1) |
+{ |
+ LimitedTokenHandler tokenHandler(4); |
+ ForEachToken(L"go&go&go&stop&EEK&EEK", L'&', std::ref(tokenHandler)); |
+ EXPECT_EQ(4, tokenHandler.callCounter); |
+ ASSERT_EQ(4, tokenHandler.tokens.size()); |
+ EXPECT_EQ(L"go", tokenHandler.tokens[0]); |
+ EXPECT_EQ(L"go", tokenHandler.tokens[1]); |
+ EXPECT_EQ(L"go", tokenHandler.tokens[2]); |
+ EXPECT_EQ(L"stop", tokenHandler.tokens[3]); |
+} |