Index: test/UtilForEachQueryStringParameterTest.cpp |
diff --git a/test/UtilForEachQueryStringParameterTest.cpp b/test/UtilForEachQueryStringParameterTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0f2e45e1af875abb11ba2aef1d2d7f881590fd8f |
--- /dev/null |
+++ b/test/UtilForEachQueryStringParameterTest.cpp |
@@ -0,0 +1,190 @@ |
+/* |
+ * 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 |
+ { |
+ 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; |
+ }; |
+} |
+ |
+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)); |
+ 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) |
+{ |
+ struct LimitedParameterHandler : ParameterHandler |
+ { |
+ LimitedParameterHandler(uint32_t limit) |
+ : callCounter(0), limit(limit) |
+ { |
+ } |
+ bool operator()(const std::wstring& parameter, const std::wstring& value) |
+ { |
+ ++callCounter; |
+ return ParameterHandler::operator()(parameter, value) && shouldContinue(); |
+ } |
+ bool shouldContinue() const |
+ { |
+ return parameters.size() < limit; |
+ } |
+ uint32_t callCounter; |
+ uint32_t limit; |
+ } parameterHandler(2); |
Eric
2015/02/13 16:33:43
If you'd like to have the subclass definition insi
|
+ 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); |
+} |