OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include <cstdint> |
| 19 #include <gtest/gtest.h> |
| 20 #include "../src/shared/Utils.h" |
| 21 |
| 22 namespace |
| 23 { |
| 24 struct TokenHandler |
| 25 { |
| 26 bool operator()(std::wstring::const_iterator begin, std::wstring::const_iter
ator end) |
| 27 { |
| 28 tokens.push_back(std::wstring(begin, end)); |
| 29 return true; |
| 30 } |
| 31 std::vector<std::wstring> tokens; |
| 32 }; |
| 33 |
| 34 struct LimitedTokenHandler : TokenHandler |
| 35 { |
| 36 LimitedTokenHandler(uint32_t limit) |
| 37 : callCounter(0), limit(limit) |
| 38 { |
| 39 } |
| 40 bool operator()(std::wstring::const_iterator begin, std::wstring::const_iter
ator end) |
| 41 { |
| 42 ++callCounter; |
| 43 return shouldContinue() && TokenHandler::operator()(begin, end) && shouldC
ontinue(); |
| 44 } |
| 45 bool shouldContinue() const |
| 46 { |
| 47 return tokens.size() < limit; |
| 48 } |
| 49 uint32_t callCounter; |
| 50 uint32_t limit; |
| 51 }; |
| 52 } |
| 53 |
| 54 TEST(ForEachTokenTest, EmptyHandler) |
| 55 { |
| 56 ASSERT_THROW(ForEachToken(L"aaa!asdasd!", L'!', |
| 57 std::function<bool(std::wstring::const_iterator, std::wstring::const_iterato
r)>()), |
| 58 std::bad_function_call); |
| 59 } |
| 60 |
| 61 TEST(ForEachTokenTest, EmptyQueryString) |
| 62 { |
| 63 TokenHandler tokenHandler; |
| 64 ForEachToken(L"", L'\0', std::ref(tokenHandler)); |
| 65 EXPECT_EQ(0, tokenHandler.tokens.size()); |
| 66 } |
| 67 |
| 68 TEST(ForEachTokenTest, StringWithoutDelimiter) |
| 69 { |
| 70 TokenHandler tokenHandler; |
| 71 ForEachToken(L"value", L'D', std::ref(tokenHandler)); |
| 72 ASSERT_EQ(1, tokenHandler.tokens.size()); |
| 73 EXPECT_EQ(L"value", tokenHandler.tokens[0]); |
| 74 } |
| 75 |
| 76 TEST(ForEachTokenTest, StringWithDelimiter) |
| 77 { |
| 78 TokenHandler tokenHandler; |
| 79 ForEachToken(L"value1&value2", L'&', std::ref(tokenHandler)); |
| 80 ASSERT_EQ(2, tokenHandler.tokens.size()); |
| 81 EXPECT_EQ(L"value1", tokenHandler.tokens[0]); |
| 82 EXPECT_EQ(L"value2", tokenHandler.tokens[1]); |
| 83 } |
| 84 |
| 85 TEST(ForEachTokenTest, StringWithDelimiterAtTheEnd) |
| 86 { |
| 87 TokenHandler tokenHandler; |
| 88 ForEachToken(L"value1&value2&", L'&', std::ref(tokenHandler)); |
| 89 ASSERT_EQ(3, tokenHandler.tokens.size()); |
| 90 EXPECT_EQ(L"value1", tokenHandler.tokens[0]); |
| 91 EXPECT_EQ(L"value2", tokenHandler.tokens[1]); |
| 92 EXPECT_EQ(L"", tokenHandler.tokens[2]); |
| 93 } |
| 94 |
| 95 TEST(ForEachTokenTest, StringWithDelimiterAtTheBeginning) |
| 96 { |
| 97 TokenHandler tokenHandler; |
| 98 ForEachToken(L"&value1&value2", L'&', std::ref(tokenHandler)); |
| 99 ASSERT_EQ(3, tokenHandler.tokens.size()); |
| 100 EXPECT_EQ(L"", tokenHandler.tokens[0]); |
| 101 EXPECT_EQ(L"value1", tokenHandler.tokens[1]); |
| 102 EXPECT_EQ(L"value2", tokenHandler.tokens[2]); |
| 103 } |
| 104 |
| 105 TEST(ForEachTokenTest, StringWithOnlyDelimiters) |
| 106 { |
| 107 TokenHandler tokenHandler; |
| 108 ForEachToken(L"&&&", L'&', std::ref(tokenHandler)); |
| 109 ASSERT_EQ(4, tokenHandler.tokens.size()); |
| 110 EXPECT_EQ(L"", tokenHandler.tokens[0]); |
| 111 EXPECT_EQ(L"", tokenHandler.tokens[1]); |
| 112 EXPECT_EQ(L"", tokenHandler.tokens[2]); |
| 113 EXPECT_EQ(L"", tokenHandler.tokens[3]); |
| 114 } |
| 115 |
| 116 TEST(ForEachTokenTest, ProcessUntilHandlerReturnsFalse0) |
| 117 { |
| 118 LimitedTokenHandler tokenHandler(3); |
| 119 ForEachToken(L"&&&&&&", L'&', std::ref(tokenHandler)); |
| 120 EXPECT_EQ(3, tokenHandler.callCounter); |
| 121 ASSERT_EQ(3, tokenHandler.tokens.size()); |
| 122 EXPECT_EQ(L"", tokenHandler.tokens[0]); |
| 123 EXPECT_EQ(L"", tokenHandler.tokens[1]); |
| 124 EXPECT_EQ(L"", tokenHandler.tokens[2]); |
| 125 } |
| 126 |
| 127 TEST(ForEachTokenTest, ProcessUntilHandlerReturnsFalse1) |
| 128 { |
| 129 LimitedTokenHandler tokenHandler(4); |
| 130 ForEachToken(L"go&go&go&stop&EEK&EEK", L'&', std::ref(tokenHandler)); |
| 131 EXPECT_EQ(4, tokenHandler.callCounter); |
| 132 ASSERT_EQ(4, tokenHandler.tokens.size()); |
| 133 EXPECT_EQ(L"go", tokenHandler.tokens[0]); |
| 134 EXPECT_EQ(L"go", tokenHandler.tokens[1]); |
| 135 EXPECT_EQ(L"go", tokenHandler.tokens[2]); |
| 136 EXPECT_EQ(L"stop", tokenHandler.tokens[3]); |
| 137 } |
OLD | NEW |