Left: | ||
Right: |
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 ParameterHandler | |
Eric
2015/02/12 17:24:41
The code reads much better with this class present
sergei
2015/02/13 15:21:56
Yes, it's not possible due to compiler limitation,
Eric
2015/02/13 16:33:43
I've written such things, and you can get the loca
| |
25 { | |
26 bool operator()(const std::wstring& parameter, const std::wstring& value) | |
27 { | |
28 parameters.emplace_back(parameter, value); | |
29 return true; | |
30 } | |
31 struct Parameter | |
32 { | |
33 Parameter(const std::wstring& name, const std::wstring& value) | |
34 : name(name), value(value) | |
35 { | |
36 } | |
37 std::wstring name; | |
38 std::wstring value; | |
39 }; | |
40 std::vector<Parameter> parameters; | |
41 }; | |
42 | |
43 struct LimitedParameterHandler : ParameterHandler | |
Eric
2015/02/12 17:24:41
I think I'd prefer than ParameterHandler take the
sergei
2015/02/13 15:21:56
I thought about it when I was creating of it and d
Eric
2015/02/13 16:33:43
If we had more variation in the tests, subclassing
| |
44 { | |
45 LimitedParameterHandler(uint32_t limit) | |
46 : callCounter(0), limit(limit) | |
47 { | |
48 } | |
49 bool operator()(const std::wstring& parameter, const std::wstring& value) | |
50 { | |
51 ++callCounter; | |
52 return shouldContinue() && ParameterHandler::operator()(parameter, value) && shouldContinue(); | |
Eric
2015/02/12 17:24:41
No need for the first shouldContinue() call here.
sergei
2015/02/13 15:21:56
I've added it with another intention but it's fine
| |
53 } | |
54 bool shouldContinue() const | |
55 { | |
56 return parameters.size() < limit; | |
57 } | |
58 uint32_t callCounter; | |
59 uint32_t limit; | |
60 }; | |
61 } | |
62 | |
63 TEST(ForEachQueryStringParameterTest, EmptyParameterHandler) | |
64 { | |
65 ASSERT_THROW(ForEachQueryStringParameter(L"some=query&string", | |
66 std::function<bool(const std::wstring&, const std::wstring&)>()), std::bad_f unction_call); | |
67 } | |
68 | |
69 TEST(ForEachQueryStringParameterTest, EmptyQueryString) | |
70 { | |
71 ParameterHandler parameterHandler; | |
72 ForEachQueryStringParameter(L"", std::ref(parameterHandler)); | |
Eric
2015/02/12 17:24:41
Do we need reference wrappers? It seems like would
sergei
2015/02/13 15:21:56
The tested function accepts `std::function` but we
Eric
2015/02/13 16:33:43
You may have misunderstood. I was suggesting decla
| |
73 EXPECT_EQ(0, parameterHandler.parameters.size()); | |
74 } | |
75 | |
76 TEST(ForEachQueryStringParameterTest, QueryStringWithOneNormalParameter) | |
77 { | |
78 ParameterHandler parameterHandler; | |
79 ForEachQueryStringParameter(L"param=value", std::ref(parameterHandler)); | |
80 ASSERT_EQ(1, parameterHandler.parameters.size()); | |
81 EXPECT_EQ(L"param", parameterHandler.parameters[0].name); | |
82 EXPECT_EQ(L"value", parameterHandler.parameters[0].value); | |
83 } | |
84 | |
85 TEST(ForEachQueryStringParameterTest, QueryStringWithSeveralNormalParameters) | |
86 { | |
87 ParameterHandler parameterHandler; | |
88 ForEachQueryStringParameter(L"param1=value1¶m2=value2¶m3=value3", std: :ref(parameterHandler)); | |
89 ASSERT_EQ(3, parameterHandler.parameters.size()); | |
90 EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); | |
91 EXPECT_EQ(L"value1", parameterHandler.parameters[0].value); | |
92 EXPECT_EQ(L"param2", parameterHandler.parameters[1].name); | |
93 EXPECT_EQ(L"value2", parameterHandler.parameters[1].value); | |
94 EXPECT_EQ(L"param3", parameterHandler.parameters[2].name); | |
95 EXPECT_EQ(L"value3", parameterHandler.parameters[2].value); | |
96 } | |
97 | |
98 TEST(ForEachQueryStringParameterTest, QueryStringWithEmptyParameterAtTheEnd) | |
99 { | |
100 ParameterHandler parameterHandler; | |
101 ForEachQueryStringParameter(L"param=value&", std::ref(parameterHandler)); | |
102 ASSERT_EQ(2, parameterHandler.parameters.size()); | |
103 EXPECT_EQ(L"param", parameterHandler.parameters[0].name); | |
104 EXPECT_EQ(L"value", parameterHandler.parameters[0].value); | |
105 EXPECT_EQ(L"", parameterHandler.parameters[1].name); | |
106 EXPECT_EQ(L"", parameterHandler.parameters[1].value); | |
107 } | |
108 | |
109 TEST(ForEachQueryStringParameterTest, QueryStringWithEmptyParameterAtTheBeginnin g) | |
110 { | |
111 ParameterHandler parameterHandler; | |
112 ForEachQueryStringParameter(L"¶m=value", std::ref(parameterHandler)); | |
113 ASSERT_EQ(2, parameterHandler.parameters.size()); | |
114 EXPECT_EQ(L"", parameterHandler.parameters[0].name); | |
115 EXPECT_EQ(L"", parameterHandler.parameters[0].value); | |
116 EXPECT_EQ(L"param", parameterHandler.parameters[1].name); | |
117 EXPECT_EQ(L"value", parameterHandler.parameters[1].value); | |
118 } | |
119 | |
120 TEST(ForEachQueryStringParameterTest, ParameterWithoutAssignSign) | |
121 { | |
122 ParameterHandler parameterHandler; | |
123 ForEachQueryStringParameter(L"param", std::ref(parameterHandler)); | |
124 ASSERT_EQ(1, parameterHandler.parameters.size()); | |
125 EXPECT_EQ(L"param", parameterHandler.parameters[0].name); | |
126 EXPECT_EQ(L"", parameterHandler.parameters[0].value); | |
127 } | |
128 | |
129 TEST(ForEachQueryStringParameterTest, ParametersWithoutAssignSign) | |
130 { | |
131 ParameterHandler parameterHandler; | |
132 ForEachQueryStringParameter(L"param1¶m2¶m3", std::ref(parameterHandler )); | |
133 ASSERT_EQ(3, parameterHandler.parameters.size()); | |
134 EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); | |
135 EXPECT_EQ(L"", parameterHandler.parameters[0].value); | |
136 EXPECT_EQ(L"param2", parameterHandler.parameters[1].name); | |
137 EXPECT_EQ(L"", parameterHandler.parameters[1].value); | |
138 EXPECT_EQ(L"param3", parameterHandler.parameters[2].name); | |
139 EXPECT_EQ(L"", parameterHandler.parameters[2].value); | |
140 } | |
141 | |
142 TEST(ForEachQueryStringParameterTest, ParameterWithSeveralAssignSigns) | |
143 { | |
144 ParameterHandler parameterHandler; | |
145 ForEachQueryStringParameter(L"param1=value1.1=value1.2", std::ref(parameterHan dler)); | |
146 ASSERT_EQ(1, parameterHandler.parameters.size()); | |
147 EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); | |
148 EXPECT_EQ(L"value1.1=value1.2", parameterHandler.parameters[0].value); | |
149 } | |
150 | |
151 TEST(ForEachQueryStringParameterTest, ParameterWithEmptyValue) | |
152 { | |
153 ParameterHandler parameterHandler; | |
154 ForEachQueryStringParameter(L"param1=", std::ref(parameterHandler)); | |
155 ASSERT_EQ(1, parameterHandler.parameters.size()); | |
156 EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); | |
157 EXPECT_EQ(L"", parameterHandler.parameters[0].value); | |
158 } | |
159 | |
160 TEST(ForEachQueryStringParameterTest, StopProcessingWhenHandlerReturnsFalse) | |
161 { | |
162 LimitedParameterHandler parameterHandler(2); | |
163 ForEachQueryStringParameter(L"param1=value1¶m2=value2¶m3=value3", std: :ref(parameterHandler)); | |
164 EXPECT_EQ(2, parameterHandler.callCounter); | |
165 ASSERT_EQ(2, parameterHandler.parameters.size()); | |
166 EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); | |
167 EXPECT_EQ(L"value1", parameterHandler.parameters[0].value); | |
168 EXPECT_EQ(L"param2", parameterHandler.parameters[1].name); | |
169 EXPECT_EQ(L"value2", parameterHandler.parameters[1].value); | |
170 } | |
171 | |
172 TEST(ForEachQueryStringParameterTest, ParameterWithoutName) | |
173 { | |
174 ParameterHandler parameterHandler; | |
175 ForEachQueryStringParameter(L"=param", std::ref(parameterHandler)); | |
176 ASSERT_EQ(1, parameterHandler.parameters.size()); | |
177 EXPECT_EQ(L"", parameterHandler.parameters[0].name); | |
178 EXPECT_EQ(L"param", parameterHandler.parameters[0].value); | |
179 } | |
180 | |
181 TEST(ForEachQueryStringParameterTest, ParametersWithoutName) | |
182 { | |
183 ParameterHandler parameterHandler; | |
184 ForEachQueryStringParameter(L"=param1&=param2&=param3", std::ref(parameterHand ler)); | |
185 ASSERT_EQ(3, parameterHandler.parameters.size()); | |
186 EXPECT_EQ(L"", parameterHandler.parameters[0].name); | |
187 EXPECT_EQ(L"param1", parameterHandler.parameters[0].value); | |
188 EXPECT_EQ(L"", parameterHandler.parameters[1].name); | |
189 EXPECT_EQ(L"param2", parameterHandler.parameters[1].value); | |
190 EXPECT_EQ(L"", parameterHandler.parameters[2].name); | |
191 EXPECT_EQ(L"param3", parameterHandler.parameters[2].value); | |
192 } | |
OLD | NEW |