Skip to content

Commit

Permalink
more tests (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire authored and anonrig committed Jan 8, 2025
1 parent 757683b commit 8dc937e
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 127 deletions.
127 changes: 127 additions & 0 deletions include/ada/url_pattern_helpers-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,133 @@ bool url_pattern_parser<F>::is_duplicate_name(std::string_view name) {
parts, [&name](const auto& part) { return part.name == name; });
}

template <url_pattern_encoding_callback F>
tl::expected<std::vector<url_pattern_part>, url_pattern_errors>
parse_pattern_string(std::string_view input,
url_pattern_compile_component_options& options,
F&& encoding_callback) {
ada_log("parse_pattern_string input=", input);
// Let parser be a new pattern parser whose encoding callback is encoding
// callback and segment wildcard regexp is the result of running generate a
// segment wildcard regexp given options.
auto parser = url_pattern_parser<F>(
encoding_callback, generate_segment_wildcard_regexp(options));
// Set parser’s token list to the result of running tokenize given input and
// "strict".
auto tokenize_result = tokenize(input, token_policy::STRICT);
if (!tokenize_result) {
ada_log("parse_pattern_string tokenize failed");
return tl::unexpected(tokenize_result.error());
}
parser.tokens = std::move(*tokenize_result);

// While parser’s index is less than parser’s token list's size:
while (parser.index < parser.tokens.size()) {
// Let char token be the result of running try to consume a token given
// parser and "char".
auto char_token = parser.try_consume_token(token_type::CHAR);
// Let name token be the result of running try to consume a token given
// parser and "name".
auto name_token = parser.try_consume_token(token_type::NAME);
// Let regexp or wildcard token be the result of running try to consume a
// regexp or wildcard token given parser and name token.
auto regexp_or_wildcard_token =
parser.try_consume_regexp_or_wildcard_token(name_token);
// If name token is not null or regexp or wildcard token is not null:
if (name_token || regexp_or_wildcard_token) {
// Let prefix be the empty string.
std::string prefix{};
// If char token is not null then set prefix to char token’s value.
if (char_token) prefix = char_token->value;
// If prefix is not the empty string and not options’s prefix code point:
if (!prefix.empty() && prefix != options.get_prefix()) {
// Append prefix to the end of parser’s pending fixed value.
parser.pending_fixed_value.append(prefix);
// Set prefix to the empty string.
prefix.clear();
}
// Run maybe add a part from the pending fixed value given parser.
if (auto error = parser.maybe_add_part_from_the_pending_fixed_value()) {
ada_log("maybe_add_part_from_the_pending_fixed_value failed");
return tl::unexpected(*error);
}
// Let modifier token be the result of running try to consume a modifier
// token given parser.
auto modifier_token = parser.try_consume_modifier_token();
// Run add a part given parser, prefix, name token, regexp or wildcard
// token, the empty string, and modifier token.
if (auto error =
parser.add_part(prefix, name_token, regexp_or_wildcard_token, {},
modifier_token)) {
ada_log("parser.add_part failed");
return tl::unexpected(*error);
}
// Continue.
continue;
}

// Let fixed token be char token.
auto fixed_token = char_token;
// If fixed token is null, then set fixed token to the result of running try
// to consume a token given parser and "escaped-char".
if (!fixed_token)
fixed_token = parser.try_consume_token(token_type::ESCAPED_CHAR);
// If fixed token is not null:
if (fixed_token) {
// Append fixed token’s value to parser’s pending fixed value.
parser.pending_fixed_value.append(fixed_token->value);
// Continue.
continue;
}
// Let open token be the result of running try to consume a token given
// parser and "open".
auto open_token = parser.try_consume_token(token_type::OPEN);
// If open token is not null:
if (open_token) {
// Set prefix be the result of running consume text given parser.
auto prefix_ = parser.consume_text();
// Set name token to the result of running try to consume a token given
// parser and "name".
name_token = parser.try_consume_token(token_type::NAME);
// Set regexp or wildcard token to the result of running try to consume a
// regexp or wildcard token given parser and name token.
regexp_or_wildcard_token =
parser.try_consume_regexp_or_wildcard_token(name_token);
// Let suffix be the result of running consume text given parser.
auto suffix_ = parser.consume_text();
// Run consume a required token given parser and "close".
if (!parser.consume_required_token(token_type::CLOSE)) {
ada_log("parser.consume_required_token failed");
return tl::unexpected(url_pattern_errors::type_error);
}
// Set modifier token to the result of running try to consume a modifier
// token given parser.
auto modifier_token = parser.try_consume_modifier_token();
// Run add a part given parser, prefix, name token, regexp or wildcard
// token, suffix, and modifier token.
if (auto error =
parser.add_part(prefix_, name_token, regexp_or_wildcard_token,
suffix_, modifier_token)) {
return tl::unexpected(*error);
}
// Continue.
continue;
}
// Run maybe add a part from the pending fixed value given parser.
if (auto error = parser.maybe_add_part_from_the_pending_fixed_value()) {
ada_log("maybe_add_part_from_the_pending_fixed_value failed on line 992");
return tl::unexpected(*error);
}
// Run consume a required token given parser and "end".
if (!parser.consume_required_token(token_type::END)) {
return tl::unexpected(url_pattern_errors::type_error);
}
}
ada_log("parser.parts size is: ", parser.parts.size());
// Return parser’s part list.
return parser.parts;
}

} // namespace ada::url_pattern_helpers

#endif
127 changes: 0 additions & 127 deletions src/url_pattern_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,133 +1104,6 @@ constexpr bool is_absolute_pathname(std::string_view input,
return false;
}

template <url_pattern_encoding_callback F>
tl::expected<std::vector<url_pattern_part>, url_pattern_errors>
parse_pattern_string(std::string_view input,
url_pattern_compile_component_options& options,
F&& encoding_callback) {
ada_log("parse_pattern_string input=", input);
// Let parser be a new pattern parser whose encoding callback is encoding
// callback and segment wildcard regexp is the result of running generate a
// segment wildcard regexp given options.
auto parser = url_pattern_parser<F>(
encoding_callback, generate_segment_wildcard_regexp(options));
// Set parser’s token list to the result of running tokenize given input and
// "strict".
auto tokenize_result = tokenize(input, token_policy::STRICT);
if (!tokenize_result) {
ada_log("parse_pattern_string tokenize failed");
return tl::unexpected(tokenize_result.error());
}
parser.tokens = std::move(*tokenize_result);

// While parser’s index is less than parser’s token list's size:
while (parser.index < parser.tokens.size()) {
// Let char token be the result of running try to consume a token given
// parser and "char".
auto char_token = parser.try_consume_token(token_type::CHAR);
// Let name token be the result of running try to consume a token given
// parser and "name".
auto name_token = parser.try_consume_token(token_type::NAME);
// Let regexp or wildcard token be the result of running try to consume a
// regexp or wildcard token given parser and name token.
auto regexp_or_wildcard_token =
parser.try_consume_regexp_or_wildcard_token(name_token);
// If name token is not null or regexp or wildcard token is not null:
if (name_token || regexp_or_wildcard_token) {
// Let prefix be the empty string.
std::string prefix{};
// If char token is not null then set prefix to char token’s value.
if (char_token) prefix = char_token->value;
// If prefix is not the empty string and not options’s prefix code point:
if (!prefix.empty() && prefix != options.get_prefix()) {
// Append prefix to the end of parser’s pending fixed value.
parser.pending_fixed_value.append(prefix);
// Set prefix to the empty string.
prefix.clear();
}
// Run maybe add a part from the pending fixed value given parser.
if (auto error = parser.maybe_add_part_from_the_pending_fixed_value()) {
ada_log("maybe_add_part_from_the_pending_fixed_value failed");
return tl::unexpected(*error);
}
// Let modifier token be the result of running try to consume a modifier
// token given parser.
auto modifier_token = parser.try_consume_modifier_token();
// Run add a part given parser, prefix, name token, regexp or wildcard
// token, the empty string, and modifier token.
if (auto error =
parser.add_part(prefix, name_token, regexp_or_wildcard_token, {},
modifier_token)) {
ada_log("parser.add_part failed");
return tl::unexpected(*error);
}
// Continue.
continue;
}

// Let fixed token be char token.
auto fixed_token = char_token;
// If fixed token is null, then set fixed token to the result of running try
// to consume a token given parser and "escaped-char".
if (!fixed_token)
fixed_token = parser.try_consume_token(token_type::ESCAPED_CHAR);
// If fixed token is not null:
if (fixed_token) {
// Append fixed token’s value to parser’s pending fixed value.
parser.pending_fixed_value.append(fixed_token->value);
// Continue.
continue;
}
// Let open token be the result of running try to consume a token given
// parser and "open".
auto open_token = parser.try_consume_token(token_type::OPEN);
// If open token is not null:
if (open_token) {
// Set prefix be the result of running consume text given parser.
auto prefix_ = parser.consume_text();
// Set name token to the result of running try to consume a token given
// parser and "name".
name_token = parser.try_consume_token(token_type::NAME);
// Set regexp or wildcard token to the result of running try to consume a
// regexp or wildcard token given parser and name token.
regexp_or_wildcard_token =
parser.try_consume_regexp_or_wildcard_token(name_token);
// Let suffix be the result of running consume text given parser.
auto suffix_ = parser.consume_text();
// Run consume a required token given parser and "close".
if (!parser.consume_required_token(token_type::CLOSE)) {
ada_log("parser.consume_required_token failed");
return tl::unexpected(url_pattern_errors::type_error);
}
// Set modifier token to the result of running try to consume a modifier
// token given parser.
auto modifier_token = parser.try_consume_modifier_token();
// Run add a part given parser, prefix, name token, regexp or wildcard
// token, suffix, and modifier token.
if (auto error =
parser.add_part(prefix_, name_token, regexp_or_wildcard_token,
suffix_, modifier_token)) {
return tl::unexpected(*error);
}
// Continue.
continue;
}
// Run maybe add a part from the pending fixed value given parser.
if (auto error = parser.maybe_add_part_from_the_pending_fixed_value()) {
ada_log("maybe_add_part_from_the_pending_fixed_value failed on line 992");
return tl::unexpected(*error);
}
// Run consume a required token given parser and "end".
if (!parser.consume_required_token(token_type::END)) {
return tl::unexpected(url_pattern_errors::type_error);
}
}
ada_log("parser.parts size is: ", parser.parts.size());
// Return parser’s part list.
return parser.parts;
}

std::string generate_pattern_string(
std::vector<url_pattern_part>& part_list,
url_pattern_compile_component_options& options) {
Expand Down
15 changes: 15 additions & 0 deletions tests/wpt_urlpattern_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ using namespace simdjson;
constexpr std::string_view URL_PATTERN_TEST_DATA =
"wpt/urlpatterntestdata.json";

TEST(wpt_urlpattern_tests, parse_pattern_string_basic_tests) {
auto part_list = ada::url_pattern_helpers::parse_pattern_string(
"*", ada::url_pattern_compile_component_options::DEFAULT,
ada::url_pattern_helpers::canonicalize_protocol);

ASSERT_TRUE(part_list);
}

TEST(wpt_urlpattern_tests, compile_basic_tests) {
auto protocol_component = ada::url_pattern_component::compile(
"*", ada::url_pattern_helpers::canonicalize_protocol,
ada::url_pattern_compile_component_options::DEFAULT);
ASSERT_TRUE(protocol_component);
}

TEST(wpt_urlpattern_tests, basic_tests) {
auto init = ada::url_pattern_init{};
init.pathname = "/books";
Expand Down

0 comments on commit 8dc937e

Please sign in to comment.