Skip to content

Commit

Permalink
test: add more tests around ada_clear_*
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Aug 29, 2023
1 parent 07fd981 commit 89ca465
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,27 @@ bool ada_set_pathname(ada_url result, const char* input,
return r->set_pathname(std::string_view(input, length));
}

/**
* Update the search/query of the URL.
*
* If a URL has `?` as the search value, passing empty string to this function
* does not remove the attribute. If you need to remove it, please use
* `ada_clear_search` method.
*/
void ada_set_search(ada_url result, const char* input, size_t length) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (r) {
r->set_search(std::string_view(input, length));
}
}

/**
* Update the hash/fragment of the URL.
*
* If a URL has `#` as the hash value, passing empty string to this function
* does not remove the attribute. If you need to remove it, please use
* `ada_clear_hash` method.
*/
void ada_set_hash(ada_url result, const char* input, size_t length) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (r) {
Expand All @@ -308,13 +322,25 @@ void ada_clear_port(ada_url result) noexcept {
}
}

/**
* Removes the hash of the URL.
*
* Despite `ada_set_hash` method, this function allows the complete
* removal of the hash attribute, even if it has a value of `#`.
*/
void ada_clear_hash(ada_url result) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (r) {
r->clear_hash();
}
}

/**
* Removes the search of the URL.
*
* Despite `ada_set_search` method, this function allows the complete
* removal of the search attribute, even if it has a value of `?`.
*/
void ada_clear_search(ada_url result) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (r) {
Expand Down
32 changes: 32 additions & 0 deletions tests/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,35 @@ TEST(ada_c, ada_idna) {
ada_free_owned_string(unicode);
SUCCEED();
}

TEST(ada_c, ada_clear_hash) {
// Make sure a hash attribute with `#` is removed.
std::string_view input = "https://www.google.com/hello-world?query=1#";
ada_url out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));

ada_clear_hash(out);
ASSERT_EQ(convert_string(ada_get_hash(out)), "");
ASSERT_FALSE(ada_has_hash(out));
ASSERT_EQ(convert_string(ada_get_href(out)),
"https://www.google.com/hello-world?query=1");

ada_free(out);
SUCCEED();
}

TEST(ada_c, ada_clear_search) {
// Make sure a search attribute with `?` is removed.
std::string_view input = "https://www.google.com/hello-world?#hash";
ada_url out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));

ada_clear_search(out);
ASSERT_EQ(convert_string(ada_get_search(out)), "");
ASSERT_FALSE(ada_has_search(out));
ASSERT_EQ(convert_string(ada_get_href(out)),
"https://www.google.com/hello-world#hash");

ada_free(out);
SUCCEED();
}

0 comments on commit 89ca465

Please sign in to comment.