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 |
| 35 TEST(ForEachTokenTest, EmptyHandler) |
| 36 { |
| 37 ASSERT_THROW(ForEachToken(L"aaa!asdasd!", L'!', |
| 38 std::function<bool(std::wstring::const_iterator, std::wstring::const_iterato
r)>()), |
| 39 std::bad_function_call); |
| 40 } |
| 41 |
| 42 TEST(ForEachTokenTest, EmptyQueryString) |
| 43 { |
| 44 TokenHandler tokenHandler; |
| 45 ForEachToken(L"", L'\0', std::ref(tokenHandler)); |
| 46 EXPECT_EQ(0, tokenHandler.tokens.size()); |
| 47 } |
| 48 |
| 49 TEST(ForEachTokenTest, StringWithoutDelimiter) |
| 50 { |
| 51 TokenHandler tokenHandler; |
| 52 ForEachToken(L"value", L'D', std::ref(tokenHandler)); |
| 53 ASSERT_EQ(1, tokenHandler.tokens.size()); |
| 54 EXPECT_EQ(L"value", tokenHandler.tokens[0]); |
| 55 } |
| 56 |
| 57 TEST(ForEachTokenTest, StringWithDelimiter) |
| 58 { |
| 59 TokenHandler tokenHandler; |
| 60 ForEachToken(L"value1&value2", L'&', std::ref(tokenHandler)); |
| 61 ASSERT_EQ(2, tokenHandler.tokens.size()); |
| 62 EXPECT_EQ(L"value1", tokenHandler.tokens[0]); |
| 63 EXPECT_EQ(L"value2", tokenHandler.tokens[1]); |
| 64 } |
| 65 |
| 66 TEST(ForEachTokenTest, StringWithDelimiterAtTheEnd) |
| 67 { |
| 68 TokenHandler tokenHandler; |
| 69 ForEachToken(L"value1&value2&", L'&', std::ref(tokenHandler)); |
| 70 ASSERT_EQ(3, tokenHandler.tokens.size()); |
| 71 EXPECT_EQ(L"value1", tokenHandler.tokens[0]); |
| 72 EXPECT_EQ(L"value2", tokenHandler.tokens[1]); |
| 73 EXPECT_EQ(L"", tokenHandler.tokens[2]); |
| 74 } |
| 75 |
| 76 TEST(ForEachTokenTest, StringWithDelimiterAtTheBeginning) |
| 77 { |
| 78 TokenHandler tokenHandler; |
| 79 ForEachToken(L"&value1&value2", L'&', std::ref(tokenHandler)); |
| 80 ASSERT_EQ(3, tokenHandler.tokens.size()); |
| 81 EXPECT_EQ(L"", tokenHandler.tokens[0]); |
| 82 EXPECT_EQ(L"value1", tokenHandler.tokens[1]); |
| 83 EXPECT_EQ(L"value2", tokenHandler.tokens[2]); |
| 84 } |
| 85 |
| 86 TEST(ForEachTokenTest, StringWithOnlyDelimiters) |
| 87 { |
| 88 TokenHandler tokenHandler; |
| 89 ForEachToken(L"&&&", L'&', std::ref(tokenHandler)); |
| 90 ASSERT_EQ(4, tokenHandler.tokens.size()); |
| 91 EXPECT_EQ(L"", tokenHandler.tokens[0]); |
| 92 EXPECT_EQ(L"", tokenHandler.tokens[1]); |
| 93 EXPECT_EQ(L"", tokenHandler.tokens[2]); |
| 94 EXPECT_EQ(L"", tokenHandler.tokens[3]); |
| 95 } |
| 96 |
| 97 namespace |
| 98 { |
| 99 struct LimitedTokenHandler : TokenHandler |
| 100 { |
| 101 LimitedTokenHandler(uint32_t limit) |
| 102 : callCounter(0), limit(limit) |
| 103 { |
| 104 } |
| 105 bool operator()(std::wstring::const_iterator begin, std::wstring::const_iter
ator end) |
| 106 { |
| 107 ++callCounter; |
| 108 return TokenHandler::operator()(begin, end) && shouldContinue(); |
| 109 } |
| 110 bool shouldContinue() const |
| 111 { |
| 112 return tokens.size() < limit; |
| 113 } |
| 114 uint32_t callCounter; |
| 115 uint32_t limit; |
| 116 }; |
| 117 } |
| 118 |
| 119 TEST(ForEachTokenTest, ProcessUntilHandlerReturnsFalse0) |
| 120 { |
| 121 LimitedTokenHandler tokenHandler(3); |
| 122 ForEachToken(L"&&&&&&", L'&', std::ref(tokenHandler)); |
| 123 EXPECT_EQ(3, tokenHandler.callCounter); |
| 124 ASSERT_EQ(3, tokenHandler.tokens.size()); |
| 125 EXPECT_EQ(L"", tokenHandler.tokens[0]); |
| 126 EXPECT_EQ(L"", tokenHandler.tokens[1]); |
| 127 EXPECT_EQ(L"", tokenHandler.tokens[2]); |
| 128 } |
| 129 |
| 130 TEST(ForEachTokenTest, ProcessUntilHandlerReturnsFalse1) |
| 131 { |
| 132 LimitedTokenHandler tokenHandler(4); |
| 133 ForEachToken(L"go&go&go&stop&EEK&EEK", L'&', std::ref(tokenHandler)); |
| 134 EXPECT_EQ(4, tokenHandler.callCounter); |
| 135 ASSERT_EQ(4, tokenHandler.tokens.size()); |
| 136 EXPECT_EQ(L"go", tokenHandler.tokens[0]); |
| 137 EXPECT_EQ(L"go", tokenHandler.tokens[1]); |
| 138 EXPECT_EQ(L"go", tokenHandler.tokens[2]); |
| 139 EXPECT_EQ(L"stop", tokenHandler.tokens[3]); |
| 140 } |
OLD | NEW |