Skip to content

Commit

Permalink
why are our examples so complicated? (#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire authored Jan 30, 2025
1 parent ff92faa commit f8507a1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ components (path, host, and so forth).
- Parse and validate a URL from an ASCII or a valid UTF-8 string.
```cpp
ada::result<ada::url_aggregator> url = ada::parse<ada::url_aggregator>("https://www.google.com");
auto url = ada::parse("https://www.google.com");
if (url) { /* URL is valid */ }
```

Expand All @@ -140,14 +140,14 @@ accessing it when you are not sure that it will succeed. The following
code is unsafe:

```cpp
ada::result<ada::url_aggregator> url = ada::parse<ada::url_aggregator>("some bad url");
auto url = ada::parse("some bad url");
url->get_href();
```

You should do...

```cpp
ada::result<ada::url_aggregator> url = ada::parse<ada::url_aggregator>("some bad url");
auto url = ada::parse("some bad url");
if(url) {
// next line is now safe:
url->get_href();
Expand All @@ -165,7 +165,7 @@ UTF-8 strings.
- Get/Update credentials

```cpp
ada::result<ada::url_aggregator> url = ada::parse<ada::url_aggregator>("https://www.google.com");
auto url = ada::parse("https://www.google.com");
url->set_username("username");
url->set_password("password");
// ada->get_href() will return "https://username:[email protected]/"
Expand All @@ -174,7 +174,7 @@ url->set_password("password");
- Get/Update Protocol
```cpp
ada::result<ada::url_aggregator> url = ada::parse<ada::url_aggregator>("https://www.google.com");
auto url = ada::parse("https://www.google.com");
url->set_protocol("wss");
// url->get_protocol() will return "wss:"
// url->get_href() will return "wss://www.google.com/"
Expand All @@ -183,7 +183,7 @@ url->set_protocol("wss");
- Get/Update host

```cpp
ada::result<ada::url_aggregator> url = ada::parse<ada::url_aggregator>("https://www.google.com");
auto url = ada::parse("https://www.google.com");
url->set_host("github.com");
// url->get_host() will return "github.com"
// you can use `url.set_hostname` depending on your usage.
Expand All @@ -192,31 +192,31 @@ url->set_host("github.com");
- Get/Update port
```cpp
ada::result<ada::url_aggregator> url = ada::parse<ada::url_aggregator>("https://www.google.com");
auto url = ada::parse("https://www.google.com");
url->set_port("8080");
// url->get_port() will return "8080"
```

- Get/Update pathname

```cpp
ada::result<ada::url_aggregator> url = ada::parse<ada::url_aggregator>("https://www.google.com");
auto url = ada::parse("https://www.google.com");
url->set_pathname("/my-super-long-path")
// url->get_pathname() will return "/my-super-long-path"
```
- Get/Update search/query
```cpp
ada::result<ada::url_aggregator> url = ada::parse<ada::url_aggregator>("https://www.google.com");
auto url = ada::parse("https://www.google.com");
url->set_search("target=self");
// url->get_search() will return "?target=self"
```

- Get/Update hash/fragment

```cpp
ada::result<ada::url_aggregator> url = ada::parse<ada::url_aggregator>("https://www.google.com");
auto url = ada::parse("https://www.google.com");
url->set_hash("is-this-the-real-life");
// url->get_hash() will return "#is-this-the-real-life"
```
Expand Down
22 changes: 22 additions & 0 deletions tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ TYPED_TEST(basic_tests, readme) {
SUCCEED();
}

TYPED_TEST(basic_tests, readmefree) {
auto url = ada::parse("https://www.google.com");
ASSERT_TRUE(bool(url));
SUCCEED();
}

TYPED_TEST(basic_tests, readme2) {
auto url = ada::parse<TypeParam>("https://www.google.com");
url->set_username("username");
Expand All @@ -102,6 +108,14 @@ TYPED_TEST(basic_tests, readme2) {
SUCCEED();
}

TYPED_TEST(basic_tests, readme2free) {
auto url = ada::parse("https://www.google.com");
url->set_username("username");
url->set_password("password");
ASSERT_EQ(url->get_href(), "https://username:[email protected]/");
SUCCEED();
}

TYPED_TEST(basic_tests, readme3) {
auto url = ada::parse<TypeParam>("https://www.google.com");
ASSERT_EQ(url->set_protocol("wss"), true);
Expand All @@ -110,6 +124,14 @@ TYPED_TEST(basic_tests, readme3) {
SUCCEED();
}

TYPED_TEST(basic_tests, readme3free) {
auto url = ada::parse("https://www.google.com");
ASSERT_EQ(url->set_protocol("wss"), true);
ASSERT_EQ(url->get_protocol(), "wss:");
ASSERT_EQ(url->get_href(), "wss://www.google.com/");
SUCCEED();
}

TYPED_TEST(basic_tests, set_protocol_should_return_false_sometimes) {
auto url = ada::parse<TypeParam>("file:");
ASSERT_EQ(url->set_protocol("https"), false);
Expand Down

0 comments on commit f8507a1

Please sign in to comment.