Skip to content

Commit 9e2e8a9

Browse files
committed
test: add more tests around ada_clear_*
1 parent 79ea5b2 commit 9e2e8a9

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/ada_c.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,27 @@ bool ada_set_pathname(ada_url result, const char* input,
287287
return r->set_pathname(std::string_view(input, length));
288288
}
289289

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

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

325+
/**
326+
* Removes the hash of the URL.
327+
*
328+
* Despite `ada_set_hash` method, this function allows the complete
329+
* removal of the hash attribute, even if it has a value of `#`.
330+
*/
311331
void ada_clear_hash(ada_url result) noexcept {
312332
ada::result<ada::url_aggregator>& r = get_instance(result);
313333
if (r) {
314334
r->clear_hash();
315335
}
316336
}
317337

338+
/**
339+
* Removes the search of the URL.
340+
*
341+
* Despite `ada_set_search` method, this function allows the complete
342+
* removal of the search attribute, even if it has a value of `?`.
343+
*/
318344
void ada_clear_search(ada_url result) noexcept {
319345
ada::result<ada::url_aggregator>& r = get_instance(result);
320346
if (r) {

tests/ada_c.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,35 @@ TEST(ada_c, ada_idna) {
177177
ada_free_owned_string(unicode);
178178
SUCCEED();
179179
}
180+
181+
TEST(ada_c, ada_clear_hash) {
182+
// Make sure a hash attribute with `#` is removed.
183+
std::string_view input = "https://www.google.com/hello-world?query=1#";
184+
ada_url out = ada_parse(input.data(), input.size());
185+
ASSERT_TRUE(ada_is_valid(out));
186+
187+
ada_clear_hash(out);
188+
ASSERT_EQ(convert_string(ada_get_hash(out)), "");
189+
ASSERT_FALSE(ada_has_hash(out));
190+
ASSERT_EQ(convert_string(ada_get_href(out)),
191+
"https://www.google.com/hello-world?query=1");
192+
193+
ada_free(out);
194+
SUCCEED();
195+
}
196+
197+
TEST(ada_c, ada_clear_search) {
198+
// Make sure a search attribute with `?` is removed.
199+
std::string_view input = "https://www.google.com/hello-world?#hash";
200+
ada_url out = ada_parse(input.data(), input.size());
201+
ASSERT_TRUE(ada_is_valid(out));
202+
203+
ada_clear_search(out);
204+
ASSERT_EQ(convert_string(ada_get_search(out)), "");
205+
ASSERT_FALSE(ada_has_search(out));
206+
ASSERT_EQ(convert_string(ada_get_href(out)),
207+
"https://www.google.com/hello-world#hash");
208+
209+
ada_free(out);
210+
SUCCEED();
211+
}

0 commit comments

Comments
 (0)