Skip to content

Commit

Permalink
update urlpatterntestdata.json
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jan 3, 2025
1 parent f2b423e commit 9f8b316
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/url_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ tl::expected<url_pattern_init, errors> url_pattern_init::process(
// Set result["pathname"] to the result of process pathname for init given
// result["pathname"], result["protocol"], and type.
auto pathname_processing_result = process_pathname(
*result.pathname, result.protocol.value_or("fake"), type);
*result.pathname, result.protocol.value_or(""), type);
if (!pathname_processing_result) {
return tl::unexpected(pathname_processing_result.error());
}
Expand Down
18 changes: 17 additions & 1 deletion tests/wpt/urlpatterntestdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,15 @@
"pathname": { "input": "var%20x%20=%201;", "groups": {}}
}
},
{
"pattern": [{ "pathname": "/foo/bar" }],
"inputs": [ "./foo/bar", "https://example.com" ],
"expected_match": {
"hostname": { "input": "example.com", "groups": { "0": "example.com" } },
"pathname": { "input": "/foo/bar", "groups": {} },
"protocol": { "input": "https", "groups": { "0": "https" } }
}
},
{
"pattern": [{ "pathname": "/foo/bar" }],
"inputs": [ { "pathname": "/foo/bar" }, "https://example.com" ],
Expand Down Expand Up @@ -2624,6 +2633,13 @@
"pathname": { "input": "/FOO/BAR", "groups": {} }
}
},
{
"pattern": [{ "ignoreCase": true }],
"inputs": [{ "pathname": "/FOO/BAR" }],
"expected_match": {
"pathname": { "input": "/FOO/BAR", "groups": { "0": "/FOO/BAR" } }
}
},
{
"pattern": [ "https://example.com:8080/foo?bar#baz",
{ "ignoreCase": true }],
Expand Down Expand Up @@ -2720,4 +2736,4 @@
"hash": { "input": "foo", "groups": {} }
}
}
]
]
40 changes: 26 additions & 14 deletions tests/wpt_urlpattern_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@ TEST(wpt_urlpattern_tests, has_regexp_groups) {
SUCCEED();
}

ada::url_pattern_init parse_init(ondemand::object& object) {
std::variant<ada::url_pattern_init, ada::url_pattern_options> parse_init(
ondemand::object& object) {
ada::url_pattern_init init{};
for (auto field : object) {
auto key = field.key().value();
std::string_view value;
EXPECT_FALSE(field.value().get_string(value));
if (field.value().get_string(value)) {
bool value_true;
EXPECT_FALSE(field.value().get_bool().get(value_true));
return ada::url_pattern_options{.ignore_case = value_true};
}
if (key == "protocol") {
init.protocol = std::string(value);
} else if (key == "username") {
Expand Down Expand Up @@ -177,8 +182,14 @@ parse_pattern_field(ondemand::array& patterns) {
} else {
EXPECT_TRUE(pattern.type() == ondemand::json_type::object);
ondemand::object object = pattern.get_object();
// TODO: URLPattern({ ignoreCase: true }) should also work...
init_obj = parse_init(object);
auto init_result = parse_init(object);
if (std::holds_alternative<ada::url_pattern_init>(init_result)) {
init_obj = std::get<ada::url_pattern_init>(init_result);
} else {
init_obj = {};
options = std::get<ada::url_pattern_options>(init_result);
return std::tuple(*init_obj, base_url, options);
}
}
} else if (pattern_size == 1) {
// The second value can be a base url or an option.
Expand Down Expand Up @@ -253,7 +264,8 @@ std::variant<std::string, ada::url_pattern_init> parse_inputs_array(

ondemand::object attribute;
EXPECT_FALSE(input.get_object().get(attribute));
return parse_init(attribute);
// We always know that this function is called with url pattern init.
return std::get<ada::url_pattern_init>(parse_init(attribute));
}

return ada::url_pattern_init{};
Expand Down Expand Up @@ -368,15 +380,15 @@ TEST(wpt_urlpattern_tests, urlpattern_test_data) {
}
}

ondemand::array inputs;
if (!main_object["inputs"].get_array().get(inputs)) {
// Expected match can be:
// - "error"
// - null
// - {} // response here.
auto input_value = parse_inputs_array(inputs);
// TODO: Parse "expected_match" field here.
}
// ondemand::array inputs;
// if (!main_object["inputs"].get_array().get(inputs)) {
// // Expected match can be:
// // - "error"
// // - null
// // - {} // response here.
// auto input_value = parse_inputs_array(inputs);
// // TODO: Parse "expected_match" field here.
// }
}
} catch (simdjson_error& error) {
std::cerr << "JSON error: " << error.what() << " near "
Expand Down

0 comments on commit 9f8b316

Please sign in to comment.