Skip to content

Commit 0cfea87

Browse files
authored
Adding a new file where we can put basic tests. (#203)
* Adding a new file where we can put basic tests. * Adding a test for the reverse. * More tests. * Testing all examples from the README. * Minor fix * Build everything
1 parent 5bad7e3 commit 0cfea87

File tree

4 files changed

+148
-4
lines changed

4 files changed

+148
-4
lines changed

.github/workflows/ubuntu-sanitized.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ jobs:
3030
env:
3131
CXX: g++-12
3232
- name: Build
33-
run: cmake --build build --target wpt_tests --target from_file_tests --target demo --target basic_fuzzer -j=2
33+
run: cmake --build build -j=2
3434
- name: Test
3535
run: ctest --output-on-failure --test-dir build

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ if(url) { /* URL is valid */ }
3030
ada::result url = ada::parse("https://www.google.com");
3131
url->set_username("username");
3232
url->set_password("password");
33-
// ada->get_href() will return "https://username:password@www.google.com"
33+
// ada->get_href() will return "https://username:password@www.google.com/"
3434
```
3535

3636
- Get/Update Protocol
3737

3838
```cpp
3939
ada::result url = ada::parse("https://www.google.com");
4040
url->set_protocol("wss");
41-
// url->get_protocol() will return "wss"
42-
// url->get_href() will return "wss://www.google.com"
41+
// url->get_protocol() will return "wss:"
42+
// url->get_href() will return "wss://www.google.com/"
4343
```
4444
4545
- Get/Update host

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ endif()
1919

2020
add_cpp_test(basic_fuzzer)
2121
add_cpp_test(from_file_tests)
22+
add_cpp_test(basic_tests)
2223

tests/basic_tests.cpp

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include "ada.h"
2+
3+
#include <cstdlib>
4+
#include <iostream>
5+
6+
#define TEST_START() \
7+
do { \
8+
std::cout << "> Running " << __func__ << " ..." << std::endl; \
9+
} while (0);
10+
#define RUN_TEST(ACTUAL) \
11+
do { \
12+
if (!(ACTUAL)) { \
13+
return false; \
14+
} \
15+
} while (0);
16+
#define TEST_FAIL(MESSAGE) \
17+
do { \
18+
std::cerr << "FAIL: " << (MESSAGE) << std::endl; \
19+
return false; \
20+
} while (0);
21+
#define TEST_SUCCEED() \
22+
do { \
23+
return true; \
24+
} while (0);
25+
#define TEST_ASSERT(LHS, RHS, MESSAGE) \
26+
do { \
27+
if (LHS != RHS) { \
28+
std::cerr << "Mismatch: '" << LHS << "' - '" << RHS << "'" << std::endl; \
29+
TEST_FAIL(MESSAGE); \
30+
} \
31+
} while (0); \
32+
33+
34+
bool set_host_should_return_false_sometimes() {
35+
TEST_START()
36+
ada::result r = ada::parse("mailto:a@b.com");
37+
bool b = r->set_host("something");
38+
TEST_ASSERT(b, false, "set_host should return false")
39+
TEST_SUCCEED()
40+
}
41+
42+
bool set_host_should_return_true_sometimes() {
43+
TEST_START()
44+
ada::result r = ada::parse("https://www.google.com");
45+
bool b = r->set_host("something");
46+
TEST_ASSERT(b, true, "set_host should return true")
47+
TEST_SUCCEED()
48+
}
49+
50+
51+
bool set_hostname_should_return_false_sometimes() {
52+
TEST_START()
53+
ada::result r = ada::parse("mailto:a@b.com");
54+
bool b = r->set_hostname("something");
55+
TEST_ASSERT(b, false, "set_hostname should return false")
56+
TEST_SUCCEED()
57+
}
58+
59+
bool set_hostname_should_return_true_sometimes() {
60+
TEST_START()
61+
ada::result r = ada::parse("https://www.google.com");
62+
bool b = r->set_hostname("something");
63+
TEST_ASSERT(b, true, "set_hostname should return true")
64+
TEST_SUCCEED()
65+
}
66+
67+
bool readme1() {
68+
TEST_START()
69+
ada::result url = ada::parse("https://www.google.com");
70+
TEST_ASSERT(bool(url), true, "URL is valid")
71+
TEST_SUCCEED()
72+
}
73+
74+
bool readme2() {
75+
TEST_START()
76+
ada::result url = ada::parse("https://www.google.com");
77+
url->set_username("username");
78+
url->set_password("password");
79+
TEST_ASSERT(url->get_href(), "https://username:password@www.google.com/", "href returned bad result")
80+
TEST_SUCCEED()
81+
}
82+
83+
bool readme3() {
84+
TEST_START()
85+
ada::result url = ada::parse("https://www.google.com");
86+
url->set_protocol("wss");
87+
TEST_ASSERT(url->get_protocol(), "wss:", "get_protocol returned bad result")
88+
TEST_ASSERT(url->get_href(), "wss://www.google.com/", "get_href returned bad result")
89+
90+
TEST_SUCCEED()
91+
}
92+
93+
bool readme4() {
94+
TEST_START()
95+
ada::result url = ada::parse("https://www.google.com");
96+
url->set_host("github.com");
97+
TEST_ASSERT(url->get_host(), "github.com", "get_host returned bad result")
98+
TEST_SUCCEED()
99+
}
100+
101+
bool readme5() {
102+
TEST_START()
103+
ada::result url = ada::parse("https://www.google.com");
104+
url->set_port("8080");
105+
TEST_ASSERT(url->get_port(), "8080", "get_port returned bad result")
106+
TEST_SUCCEED()
107+
}
108+
109+
bool readme6() {
110+
TEST_START()
111+
ada::result url = ada::parse("https://www.google.com");
112+
url->set_pathname("/my-super-long-path");
113+
TEST_ASSERT(url->get_pathname(), "/my-super-long-path", "get_pathname returned bad result")
114+
TEST_SUCCEED()
115+
}
116+
117+
bool readme7() {
118+
TEST_START()
119+
ada::result url = ada::parse("https://www.google.com");
120+
url->set_search("target=self");
121+
TEST_ASSERT(url->get_search(), "?target=self", "get_pathname returned bad result");
122+
TEST_SUCCEED()
123+
}
124+
125+
bool readme8() {
126+
TEST_START()
127+
ada::result url = ada::parse("https://www.google.com");
128+
url->set_hash("is-this-the-real-life");
129+
TEST_ASSERT(url->get_hash(), "#is-this-the-real-life", "get_hash returned bad result");
130+
TEST_SUCCEED()
131+
}
132+
133+
int main() {
134+
bool success = set_host_should_return_false_sometimes()
135+
&& set_host_should_return_true_sometimes()
136+
&& set_hostname_should_return_false_sometimes()
137+
&& set_hostname_should_return_true_sometimes()
138+
&& readme1() && readme2() && readme3()
139+
&& readme4() && readme5() && readme6()
140+
&& readme7();
141+
if(success) { return EXIT_SUCCESS; }
142+
return EXIT_FAILURE;
143+
}

0 commit comments

Comments
 (0)