-
-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get rid of unnecessary string copies in the the keys() method
- Loading branch information
1 parent
4fbd9b4
commit 1b6a4f4
Showing
3 changed files
with
62 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "catch.hpp" | ||
#include "crow/query_string.h" | ||
|
||
namespace | ||
{ | ||
std::string buildQueryStr(const std::vector<std::pair<std::string, std::string>>& paramList) | ||
{ | ||
std::string paramsStr{}; | ||
for (const auto& param : paramList) | ||
paramsStr.append(param.first).append(1, '=').append(param.second).append(1, '&'); | ||
if (!paramsStr.empty()) | ||
paramsStr.resize(paramsStr.size() - 1); | ||
return paramsStr; | ||
} | ||
} | ||
|
||
TEST_CASE( "empty query params" ) | ||
{ | ||
const crow::query_string query_params(""); | ||
const std::vector<std::string> keys = query_params.keys(); | ||
|
||
REQUIRE(keys.empty() == true); | ||
} | ||
|
||
TEST_CASE( "query string keys" ) | ||
{ | ||
const std::vector<std::pair<std::string, std::string>> params { | ||
{"foo", "bar"}, {"mode", "night"}, {"page", "2"}, | ||
{"tag", "js"}, {"name", "John Smith"}, {"age", "25"}, | ||
}; | ||
|
||
const crow::query_string query_params("params?" + buildQueryStr(params)); | ||
const std::vector<std::string> keys = query_params.keys(); | ||
|
||
for (const auto& entry: params) | ||
{ | ||
const bool exist = std::any_of(keys.cbegin(), keys.cend(), [&](const std::string& key) { | ||
return key == entry.first;}); | ||
REQUIRE(exist == true); | ||
} | ||
} |