Lolly 1.4.28
Loading...
Searching...
No Matches
url_test.cpp
Go to the documentation of this file.
1/** \file url.cpp
2 * \copyright GPLv3
3 * \details Unitests for url.
4 * \author Darcy
5 * \date 2019-2023
6 */
7#include "a_lolly_test.hpp"
8#include "sys_utils.hpp"
9#include "url.hpp"
10
11url ustc_edu = url_system ("https://ustc.edu.cn");
12url texmacs_org = url_system ("http://texmacs.org");
15url file_tmp = url_system ("file:///tmp");
17url wsl_ubuntu = url_system ("\\\\wsl.localhost\\Ubuntu");
23
25
26TEST_CASE ("label of url") {
27 string_eq (ustc_edu.label (), "concat");
28 string_eq (texmacs_org.label (), "concat");
29 string_eq (none_url.label (), "none");
30 string_eq (file_root.label (), "root");
31 string_eq (ftp_root.label (), "root");
32#if defined(OS_MINGW) || defined(OS_WIN)
33 SUBCASE ("on host windows") { string_eq (wsl_ubuntu.label (), "concat"); }
34#endif
35#if defined(OS_MACOS) || defined(OS_LINUX) || defined(OS_WASM)
36 SUBCASE ("on host macos/linux/wasm") {
37 string_eq (unix_root.label (), "root");
38 string_eq (unix_root_txt.label (), "concat");
39 string_eq (unix_tmp.label (), "concat");
40 string_eq (unix_tmp_a.label (), "concat");
41 }
42#endif
43 string_eq (file_tmp.label (), "concat");
44 string_eq (abc_url.label (), "");
45}
46
47TEST_CASE ("protocol of url") {
48 string_eq (ustc_edu.protocol (), "https");
49 string_eq (texmacs_org.protocol (), "http");
50 string_eq (none_url.protocol (), "default");
51 string_eq (file_root.protocol (), "file");
52 string_eq (ftp_root.protocol (), "ftp");
53 string_eq (wsl_ubuntu.protocol (), "default");
54 string_eq (unix_root.protocol (), "default");
55 string_eq (unix_root_txt.protocol (), "default");
56 string_eq (unix_tmp.protocol (), "default");
57 string_eq (unix_tmp_a.protocol (), "default");
58 string_eq (abc_url.protocol (), "default");
59
60 string_eq (get_root (ustc_edu), "https");
61 string_eq (get_root (texmacs_org), "http");
62 string_eq (get_root (file_root), "file");
63 string_eq (get_root (file_tmp), "file");
64 string_eq (get_root (ftp_root), "ftp");
65#if defined(OS_MINGW) || defined(OS_WIN)
66 string_eq (get_root (wsl_ubuntu), "default");
67#endif
68 string_eq (get_root (none_url), "default");
70#if defined(OS_MACOS) || defined(OS_LINUX) || defined(OS_WASM)
71 SUBCASE ("on host macos/linux/wasm") {
72 url u1= url_system ("file:///$HOME/.bashrc");
73 string_eq (get_root (u1), "file");
74 }
75#endif
76}
77
78TEST_CASE ("url with env") {
79#if defined(OS_MINGW) || defined(OS_WIN)
80 SUBCASE ("on windows") {
81 set_env ("TEST_ENV", "System;Plugins;Kernel");
82 url u= url ("$TEST_ENV");
83 CHECK (is_or (u));
84 }
85#endif
86#if defined(OS_MACOS) || defined(OS_LINUX) || defined(OS_WASM)
87 SUBCASE ("on macos/linux/wasm") {
88 set_env ("TEST_ENV", "System:Plugins:Kernel");
89 url u= url ("$TEST_ENV");
90 CHECK (is_or (u));
91 }
92#endif
93}
94
95TEST_CASE ("is here/parent/ancestor") { CHECK (is_here (url_here ())); }
96
97TEST_CASE ("is_none") {
103
104 CHECK (is_none (url ()));
105 url_eq (url (), url_none ());
106 CHECK (!is_none (url ("")));
107 url_neq (url (), url (""));
108}
109
110TEST_CASE ("is_root") {
116#if defined(OS_MACOS) || defined(OS_LINUX)
118#endif
119 CHECK (!is_root (abc_url));
120}
121
122TEST_CASE ("is_concat") {
125}
126
127#if defined(OS_MACOS) || defined(OS_LINUX) || defined(OS_WASM)
128TEST_CASE ("is_concat on unix and wasm") {
132}
133#endif
134
135TEST_CASE ("suffix/basename") {
136 SUBCASE ("empty suffix") {
137 url no_suffix= url ("/a/b/c/d/no_suffix");
139 string_eq (basename (no_suffix), "no_suffix");
140
141 url no_suffix2= url ("/a/b.c/d/no_suffix");
143 string_eq (basename (no_suffix2), "no_suffix");
144 }
145
146 SUBCASE ("normal suffix") {
147 url png= url ("/a/b/c/d.png");
148 url PNG= url ("/a/b/c/d.PNG");
149 string_eq (suffix (png), "png");
150 string_eq (suffix (PNG), "png");
151 string_eq (suffix (PNG, false), "PNG");
152 string_eq (basename (png), "d");
153 string_eq (basename (PNG), "d");
154
155 url png2= url ("/a/b.c/d.png");
156 string_eq (suffix (png2), "png");
157 }
158
159 SUBCASE ("normal http url") {
160 url png= url ("https://name.com/path/to.png");
161 string_eq (suffix (png), "png");
162 string_eq (basename (png), "to");
163
164 url jpg= url ("https://name.org/path/to.jpg");
165 string_eq (suffix (jpg), "jpg");
166 string_eq (basename (jpg), "to");
167 }
168
169 SUBCASE ("http url with paramters") {
170 url jpg= url ("https://name.cn/path/to.jpg?width=100");
171 string_eq (suffix (jpg), "jpg");
172 string_eq (basename (jpg), "to");
173 }
174}
175
176TEST_CASE ("as_string") {
177 set_env ("TEST_PWD", as_string (url_pwd ()));
178 url file_env_lua = url_system ("file:///$TEST_PWD/xmake.lua");
179 url local_env_lua= url_system ("local:$TEST_PWD/xmake.lua");
180 url dirs = url ("Data") | url ("Kernel") | url ("Plugins");
181 string_eq (as_string (dirs), string ("Data") * URL_SEPARATOR * "Kernel" *
182 URL_SEPARATOR * "Plugins");
183
184 SUBCASE ("is_atomic") {
185 string_eq (as_string (url_here ()), string ("."));
186 string_eq (as_string (url_parent ()), string (".."));
187 string_eq (as_string (url_ancestor ()), string ("..."));
188 }
189
190 SUBCASE ("tree with empty string") {
191 string_eq (as_string (url ("")), "");
192 string_eq (as_string (url ("a") * url ("")), string ("a") * URL_CONCATER);
193 }
194
195#if defined(OS_MINGW) || defined(OS_WIN)
196 SUBCASE ("on host windows") {
197 url app_mogan= url_system ("$ProgramFiles\\Mogan");
198 string_eq (as_string (app_mogan), string ("C:\\Program Files\\Mogan"));
200 "file:///" * as_string (url_pwd () * "xmake.lua"));
202 "file:///" * as_string (url_pwd () * "xmake.lua"));
203 }
204#endif
205#if defined(OS_MACOS) || defined(OS_LINUX) || defined(OS_WASM)
206 SUBCASE ("on linux/macos/wasm") {
208 "file://" * as_string (url_pwd () * "xmake.lua"));
210 "file://" * as_string (url_pwd () * "xmake.lua"));
211 }
212#endif
213}
214
215TEST_CASE ("unknown protocol like zotero") {
216 url zotero_u= url_system ("zotero://select/library/items/2AIFJFS7");
217 string_eq (zotero_u.protocol (), "zotero");
218 CHECK (!is_or (zotero_u));
219
220 url tmfs_u= url_system ("tmfs://git/status");
221 string_eq (tmfs_u.protocol (), "tmfs");
222 CHECK (!is_or (tmfs_u));
223
224 url c_u= url_system ("C://Program Files/MoganResearch");
225 string_neq (c_u.protocol (), "C");
226}
227
228TEST_CASE ("relative") {
229 SUBCASE ("on macOS/Linux/wasm") {
230 if (!os_win ()) {
231 url_eq (relative ("/tmp", "a.txt"), url_system ("/a.txt"));
232 url_eq (relative ("/tmp", "x/a.txt"), url_system ("/x/a.txt"));
233 url_eq (relative ("/tmp", "x/y/a.txt"), url_system ("/x/y/a.txt"));
234 url_eq (relative ("/tmp", "/a.txt"), url_system ("/a.txt"));
235 url_eq (relative ("/tmp", "/tmp/a.txt"), url_system ("/tmp/a.txt"));
236 url_eq (relative ("/tmp", "/x/y/z/a.txt"), url_system ("/x/y/z/a.txt"));
237 url_eq (relative ("/tmp/b.txt", "a.txt"), url_system ("/tmp/a.txt"));
239 url_eq (relative ("/tmp", url_none ()), url_none ());
240 url_eq (relative (url_none (), "a.txt"), url_none ());
241 url_eq (relative (url_none (), "/tmp/a.txt"), url ("/tmp/a.txt"));
242 }
243 }
244}
245
246TEST_CASE ("delta") {
247 SUBCASE ("on macOS/Linux/wasm") {
248 if (!os_win ()) {
249 url_eq (delta ("/tmp", "/tmp/a.txt"), url ("tmp/a.txt"));
250 url_eq (delta ("/tmp", "/tmp/x/a.txt"), url ("tmp/x/a.txt"));
251 url_eq (delta ("/tmp", "/x/a.txt"), url ("x/a.txt"));
252 url_eq (delta ("/tmp", url_none ()), url_none ());
253 url_eq (delta (url_none (), "/tmp/a.txt"), url ("/tmp/a.txt"));
254 url_eq (delta (url_none (), url_none ()), url_none ());
255 }
256 }
257}
258
259TEST_CASE ("url_concat") {
260 url_eq (url ("a") * url ("b"), url_system ("a/b"));
261 url_eq (url_root ("file") * url ("tmp"), url_system ("file:///tmp"));
262}
263
264TEST_CASE ("expand") {
265 if (!os_win ()) {
266 url tmp_or_usr= url ("/tmp") | url ("/usr");
268 }
269}
270
271TEST_CASE ("reroot") {
272 CHECK (!is_rooted (reroot (url_system ("tmp"), "file")));
273 CHECK (is_rooted (reroot (url_pwd (), "file")));
274}
275
276TEST_CASE ("descends") {
277 SUBCASE ("u == base") {
289 }
290 SUBCASE ("u is concat, base is atomic") {
291 CHECK (!descends (url ("./a/b/c"), url (".")));
292 CHECK (descends (url ("../a/b/c"), url ("..")));
293 CHECK (descends (url (".../a/b/c"), url ("...")));
294 }
295 SUBCASE ("u is concat, base is concat") {
296 CHECK (descends (url ("a/b/c"), url ("a/b")));
297 CHECK (!descends (url ("a/b/c"), url ("a/b/")));
298 CHECK (descends (url ("a/b"), url ("a")));
299 CHECK (!descends (url ("a/b"), url ("a/")));
300 }
301 SUBCASE ("u or base is url_or") {
302 CHECK (descends (url_unix ("a:b"), url_unix ("a:b")));
303 CHECK (descends (url_unix ("a"), url_unix ("a:b")));
304 CHECK (descends (url_unix ("b"), url_unix ("a:b")));
305 CHECK (!descends (url_unix ("a:b"), url_unix ("a")));
306 CHECK (!descends (url_unix ("a:b"), url_unix ("b")));
307 }
308}
309
310TEST_CASE ("is_atomic") {
311 CHECK (is_atomic (url_here ()));
314
315 CHECK (is_atomic (url ("")));
316
318 CHECK (!is_atomic (url_none ()));
319 SUBCASE ("unix") {
320 if (!os_win ()) {
323 }
324 }
325}
326
The list class represents a linked list.
Definition list.hpp:48
Definition url.hpp:37
string label()
Definition url.hpp:51
string protocol()
Definition url.cpp:67
url url_unix(string name)
Definition file_url.cpp:31
void url_eq(url left, url right)
#define TEST_MEMORY_LEAK_INIT
#define TEST_MEMORY_LEAK_ALL
void string_eq(string left, string right)
void url_neq(url left, url right)
void string_neq(string left, string right)
TEST_CASE("test for operator+= and advance()")
string as_string(int16_t i)
Definition string.cpp:272
bool is_empty(string s)
Definition string.cpp:316
bool os_win()
Definition sys_utils.cpp:94
void set_env(string var, string with)
Definition sys_utils.cpp:49
#define URL_SEPARATOR
Definition sys_utils.hpp:23
#define URL_CONCATER
Definition sys_utils.hpp:22
url url_none()
Definition url.cpp:91
bool is_atomic(url u)
Definition url.cpp:664
string suffix(url u, bool use_locase)
Definition url.cpp:381
url url_system(string name)
Definition url.cpp:306
string basename(url u, string suf)
Definition url.cpp:406
url url_root(string protocol)
Definition url.cpp:96
url relative(url base, url u)
Definition url.cpp:450
url reroot(url u, string protocol)
Definition url.cpp:490
bool descends(url u, url base)
Definition url.cpp:573
bool is_rooted(url u)
Definition url.cpp:686
string get_root(url u)
Definition url.cpp:477
url delta(url base, url u)
Definition url.cpp:469
static url expand(url u1, url u2)
Definition url.cpp:498
url url_pwd()
Definition url.cpp:326
bool is_root(url u, string s)
Definition url.cpp:668
url url_ancestor()
Definition url.hpp:132
bool is_or(url u)
Definition url.hpp:178
bool is_here(url u)
Definition url.hpp:154
url url_here()
Definition url.hpp:122
bool is_none(url u)
Definition url.hpp:166
url url_parent()
Definition url.hpp:127
bool is_concat(url u)
Definition url.hpp:174
url texmacs_org
Definition url_test.cpp:12
url unix_root_txt
Definition url_test.cpp:19
url unix_tmp
Definition url_test.cpp:20
url ustc_edu
Definition url_test.cpp:11
url wsl_ubuntu
Definition url_test.cpp:17
url abc_url
Definition url_test.cpp:22
url file_root
Definition url_test.cpp:14
url ftp_root
Definition url_test.cpp:16
url unix_tmp_a
Definition url_test.cpp:21
url file_tmp
Definition url_test.cpp:15
url unix_root
Definition url_test.cpp:18
url none_url
Definition url_test.cpp:13