Skip to content

Commit

Permalink
Use std::filesystem::path for test file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
rmisev committed Dec 8, 2024
1 parent 96dbc1f commit 1450038
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
8 changes: 4 additions & 4 deletions test/bench-url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// -----------------------------------------------------------------------------
// Read samples from text file (URL in each line) and benchmark

int benchmark_txt(const char* file_name, uint64_t min_iters) {
int benchmark_txt(const std::filesystem::path& file_name, uint64_t min_iters) {
std::vector<std::string> url_strings;

// Load URL samples
Expand Down Expand Up @@ -60,7 +60,7 @@ int benchmark_txt(const char* file_name, uint64_t min_iters) {
// -----------------------------------------------------------------------------
// Read samples from urltestdata.json and benchmark

int benchmark_wpt(const char* file_name, uint64_t min_iters) {
int benchmark_wpt(const std::filesystem::path& file_name, uint64_t min_iters) {
std::vector<std::pair<std::string, std::string>> url_samples;

// Load URL samples
Expand Down Expand Up @@ -144,9 +144,9 @@ int main(int argc, const char* argv[])
: min_iters_def;

if (file_name.extension() == ".json") {
return benchmark_wpt(file_name.string().c_str(), min_iters);
return benchmark_wpt(file_name, min_iters);
} else if (file_name.extension() == ".txt") {
return benchmark_txt(file_name.string().c_str(), min_iters);
return benchmark_txt(file_name, min_iters);
}

std::cerr << "File containing URLs should have .json or .txt extension.\n";
Expand Down
3 changes: 2 additions & 1 deletion test/picojson_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// https://github.com/kazuho/picojson
#include "picojson/picojson.h"

#include <filesystem>
#include <fstream>
#include <iostream>
#include <iterator>
Expand Down Expand Up @@ -134,7 +135,7 @@ enum {
// Parses a JSON file using the specified PicoJSON parsing context

template <typename Context>
inline int load_file(Context& ctx, const char* file_name, const char* title = nullptr) {
inline int load_file(Context& ctx, const std::filesystem::path& file_name, const char* title = nullptr) {
try {
if (title)
std::cout << title << ": " << file_name << '\n';
Expand Down
21 changes: 11 additions & 10 deletions test/wpt-url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "picojson_util.h"

#include <filesystem>
#include <iostream>
#include <map>
#include <string>
Expand All @@ -22,11 +23,11 @@

// Test runner

int run_parser_tests(DataDrivenTest& ddt, const char* file_name);
int run_host_parser_tests(DataDrivenTest& ddt, const char* file_name);
int run_idna_v2_tests(DataDrivenTest& ddt, const char* file_name, const char* fixes_file_name);
int run_setter_tests(DataDrivenTest& ddt, const char* file_name);
int run_percent_encoding_tests(DataDrivenTest& ddt, const char* file_name);
int run_parser_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name);
int run_host_parser_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name);
int run_idna_v2_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name, const char* fixes_file_name);
int run_setter_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name);
int run_percent_encoding_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name);

template <typename RunTests, typename ...Args>
int test_from_file(RunTests run_tests, Args&&... args);
Expand Down Expand Up @@ -436,7 +437,7 @@ void test_percent_encoding(DataDrivenTest& ddt, const EncodingObj& obj)

// Read samples from JSON files and run tests

int run_parser_tests(DataDrivenTest& ddt, const char* file_name) {
int run_parser_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name) {
const auto test_item = [&](const picojson::value& item) {
// analyze array item
if (item.is<picojson::object>()) {
Expand All @@ -455,7 +456,7 @@ int run_parser_tests(DataDrivenTest& ddt, const char* file_name) {
return json_util::load_file(context, file_name);
}

int run_host_parser_tests(DataDrivenTest& ddt, const char* file_name) {
int run_host_parser_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name) {
const auto test_item = [&](const picojson::value& item) {
// analyze array item
if (item.is<picojson::object>()) {
Expand All @@ -474,7 +475,7 @@ int run_host_parser_tests(DataDrivenTest& ddt, const char* file_name) {
return json_util::load_file(context, file_name);
}

int run_idna_v2_tests(DataDrivenTest& ddt, const char* file_name, const char* fixes_file_name) {
int run_idna_v2_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name, const char* fixes_file_name) {
std::unordered_map<std::string, parsed_obj> fixes;

if (fixes_file_name) {
Expand Down Expand Up @@ -520,7 +521,7 @@ int run_idna_v2_tests(DataDrivenTest& ddt, const char* file_name, const char* fi
}

// parses setters_tests.json
int run_setter_tests(DataDrivenTest& ddt, const char* file_name) {
int run_setter_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name) {
const auto test_item = [&](const std::string& setter_name, const picojson::value& item) {
// analyze array item
if (item.is<picojson::object>()) {
Expand Down Expand Up @@ -556,7 +557,7 @@ int run_setter_tests(DataDrivenTest& ddt, const char* file_name) {
}

// parses percent-encoding.json
int run_percent_encoding_tests(DataDrivenTest& ddt, const char* file_name) {
int run_percent_encoding_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name) {
const auto test_item = [&](const picojson::value& item) {
// analyze array item
if (item.is<picojson::object>()) {
Expand Down
5 changes: 3 additions & 2 deletions test/wpt-urlencoded-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// https://github.com/kazuho/picojson
#include "picojson_util.h"

#include <filesystem>
#include <iostream>
#include <string>

Expand All @@ -24,7 +25,7 @@ std::basic_ostream<CharT, Traits>& operator<<(
// https://github.com/web-platform-tests/wpt/blob/master/url/urlsearchparams-sort.any.js
//

int test_from_file(const char* file_name, bool sort = false);
int test_from_file(const std::filesystem::path& file_name, bool sort = false);

int main(int argc, char** argv)
{
Expand Down Expand Up @@ -108,7 +109,7 @@ void test_urlsearchparams_sort(DataDrivenTest& ddt, const TestObj& obj) {

// Read data file and run tests from it

int test_from_file(const char* file_name, bool sort)
int test_from_file(const std::filesystem::path& file_name, bool sort)
{
DataDrivenTest ddt;
ddt.config_show_passed(false);
Expand Down

0 comments on commit 1450038

Please sign in to comment.