Index: test/UtilForEachQueryStringParameterTest.cpp |
diff --git a/test/UtilForEachQueryStringParameterTest.cpp b/test/UtilForEachQueryStringParameterTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f36b553903dc5e5f571d3508a500550a17dea646 |
--- /dev/null |
+++ b/test/UtilForEachQueryStringParameterTest.cpp |
@@ -0,0 +1,192 @@ |
+/* |
+ * 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 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
|
+ { |
+ bool operator()(const std::wstring& parameter, const std::wstring& value) |
+ { |
+ parameters.emplace_back(parameter, value); |
+ return true; |
+ } |
+ struct Parameter |
+ { |
+ Parameter(const std::wstring& name, const std::wstring& value) |
+ : name(name), value(value) |
+ { |
+ } |
+ std::wstring name; |
+ std::wstring value; |
+ }; |
+ std::vector<Parameter> parameters; |
+ }; |
+ |
+ 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
|
+ { |
+ LimitedParameterHandler(uint32_t limit) |
+ : callCounter(0), limit(limit) |
+ { |
+ } |
+ bool operator()(const std::wstring& parameter, const std::wstring& value) |
+ { |
+ ++callCounter; |
+ 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
|
+ } |
+ bool shouldContinue() const |
+ { |
+ return parameters.size() < limit; |
+ } |
+ uint32_t callCounter; |
+ uint32_t limit; |
+ }; |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, EmptyParameterHandler) |
+{ |
+ ASSERT_THROW(ForEachQueryStringParameter(L"some=query&string", |
+ std::function<bool(const std::wstring&, const std::wstring&)>()), std::bad_function_call); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, EmptyQueryString) |
+{ |
+ ParameterHandler parameterHandler; |
+ 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
|
+ EXPECT_EQ(0, parameterHandler.parameters.size()); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, QueryStringWithOneNormalParameter) |
+{ |
+ ParameterHandler parameterHandler; |
+ ForEachQueryStringParameter(L"param=value", std::ref(parameterHandler)); |
+ ASSERT_EQ(1, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"param", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"value", parameterHandler.parameters[0].value); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, QueryStringWithSeveralNormalParameters) |
+{ |
+ ParameterHandler parameterHandler; |
+ ForEachQueryStringParameter(L"param1=value1¶m2=value2¶m3=value3", std::ref(parameterHandler)); |
+ ASSERT_EQ(3, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"value1", parameterHandler.parameters[0].value); |
+ EXPECT_EQ(L"param2", parameterHandler.parameters[1].name); |
+ EXPECT_EQ(L"value2", parameterHandler.parameters[1].value); |
+ EXPECT_EQ(L"param3", parameterHandler.parameters[2].name); |
+ EXPECT_EQ(L"value3", parameterHandler.parameters[2].value); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, QueryStringWithEmptyParameterAtTheEnd) |
+{ |
+ ParameterHandler parameterHandler; |
+ ForEachQueryStringParameter(L"param=value&", std::ref(parameterHandler)); |
+ ASSERT_EQ(2, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"param", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"value", parameterHandler.parameters[0].value); |
+ EXPECT_EQ(L"", parameterHandler.parameters[1].name); |
+ EXPECT_EQ(L"", parameterHandler.parameters[1].value); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, QueryStringWithEmptyParameterAtTheBeginning) |
+{ |
+ ParameterHandler parameterHandler; |
+ ForEachQueryStringParameter(L"¶m=value", std::ref(parameterHandler)); |
+ ASSERT_EQ(2, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"", parameterHandler.parameters[0].value); |
+ EXPECT_EQ(L"param", parameterHandler.parameters[1].name); |
+ EXPECT_EQ(L"value", parameterHandler.parameters[1].value); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, ParameterWithoutAssignSign) |
+{ |
+ ParameterHandler parameterHandler; |
+ ForEachQueryStringParameter(L"param", std::ref(parameterHandler)); |
+ ASSERT_EQ(1, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"param", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"", parameterHandler.parameters[0].value); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, ParametersWithoutAssignSign) |
+{ |
+ ParameterHandler parameterHandler; |
+ ForEachQueryStringParameter(L"param1¶m2¶m3", std::ref(parameterHandler)); |
+ ASSERT_EQ(3, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"", parameterHandler.parameters[0].value); |
+ EXPECT_EQ(L"param2", parameterHandler.parameters[1].name); |
+ EXPECT_EQ(L"", parameterHandler.parameters[1].value); |
+ EXPECT_EQ(L"param3", parameterHandler.parameters[2].name); |
+ EXPECT_EQ(L"", parameterHandler.parameters[2].value); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, ParameterWithSeveralAssignSigns) |
+{ |
+ ParameterHandler parameterHandler; |
+ ForEachQueryStringParameter(L"param1=value1.1=value1.2", std::ref(parameterHandler)); |
+ ASSERT_EQ(1, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"value1.1=value1.2", parameterHandler.parameters[0].value); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, ParameterWithEmptyValue) |
+{ |
+ ParameterHandler parameterHandler; |
+ ForEachQueryStringParameter(L"param1=", std::ref(parameterHandler)); |
+ ASSERT_EQ(1, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"", parameterHandler.parameters[0].value); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, StopProcessingWhenHandlerReturnsFalse) |
+{ |
+ LimitedParameterHandler parameterHandler(2); |
+ ForEachQueryStringParameter(L"param1=value1¶m2=value2¶m3=value3", std::ref(parameterHandler)); |
+ EXPECT_EQ(2, parameterHandler.callCounter); |
+ ASSERT_EQ(2, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"value1", parameterHandler.parameters[0].value); |
+ EXPECT_EQ(L"param2", parameterHandler.parameters[1].name); |
+ EXPECT_EQ(L"value2", parameterHandler.parameters[1].value); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, ParameterWithoutName) |
+{ |
+ ParameterHandler parameterHandler; |
+ ForEachQueryStringParameter(L"=param", std::ref(parameterHandler)); |
+ ASSERT_EQ(1, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"param", parameterHandler.parameters[0].value); |
+} |
+ |
+TEST(ForEachQueryStringParameterTest, ParametersWithoutName) |
+{ |
+ ParameterHandler parameterHandler; |
+ ForEachQueryStringParameter(L"=param1&=param2&=param3", std::ref(parameterHandler)); |
+ ASSERT_EQ(3, parameterHandler.parameters.size()); |
+ EXPECT_EQ(L"", parameterHandler.parameters[0].name); |
+ EXPECT_EQ(L"param1", parameterHandler.parameters[0].value); |
+ EXPECT_EQ(L"", parameterHandler.parameters[1].name); |
+ EXPECT_EQ(L"param2", parameterHandler.parameters[1].value); |
+ EXPECT_EQ(L"", parameterHandler.parameters[2].name); |
+ EXPECT_EQ(L"param3", parameterHandler.parameters[2].value); |
+} |