Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/fmt/bookmarks.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 14 окт. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/test-fw/utest.h>
23 #include <lsp-plug.in/fmt/bookmarks.h>
24
25 namespace lsp
26 {
27 using namespace lsp::bookmarks;
28
29 static ssize_t compare_bookmarks(const bookmark_t *a, const bookmark_t *b)
30 {
31 return a->name.compare_to_nocase(&b->name);
32 }
33 }
34
35 UTEST_BEGIN("runtime.fmt", bookmarks)
36
37 void test_gtk3_bookmarks()
38 {
39 lltl::parray<bookmark_t> bm;
40 lsp_finally { destroy_bookmarks(&bm); };
41
42 io::Path path;
43 UTEST_ASSERT(path.set(resources()) == STATUS_OK);
44 UTEST_ASSERT(path.append_child("fmt/bookmarks/gtk-3.0.bookmarks") == STATUS_OK);
45
46 printf("Reading GTK3 bookmarks from file %s\n", path.as_native());
47 UTEST_ASSERT(read_bookmarks_gtk3(&bm, &path) == STATUS_OK);
48
49 for (size_t i=0; i<bm.size(); ++i)
50 printf(" Read GTK3 bookmark: %s -> %s\n", bm.get(i)->path.get_utf8(), bm.get(i)->name.get_utf8());
51
52 UTEST_ASSERT(bm.size() == 4);
53 UTEST_ASSERT(bm.get(0)->path.equals_ascii("/path/to/file1"));
54 UTEST_ASSERT(bm.get(0)->name.equals_ascii("file1"));
55 UTEST_ASSERT(bm.get(1)->path.equals_ascii("path/to/file2"));
56 UTEST_ASSERT(bm.get(1)->name.equals_ascii("Some alias"));
57 UTEST_ASSERT(bm.get(2)->path.equals_ascii("/path/with spaces/for test"));
58 UTEST_ASSERT(bm.get(2)->name.equals_ascii("for test"));
59 }
60
61 void test_qt5_bookmarks()
62 {
63 lltl::parray<bookmark_t> bm;
64 lsp_finally { destroy_bookmarks(&bm); };
65
66 io::Path path;
67 UTEST_ASSERT(path.set(resources()) == STATUS_OK);
68 UTEST_ASSERT(path.append_child("fmt/bookmarks/qt5-bookmarks.xml") == STATUS_OK);
69
70 printf("Reading QT5 bookmarks from file %s\n", path.as_native());
71 UTEST_ASSERT(read_bookmarks_qt5(&bm, &path, "UTF-8") == STATUS_OK);
72
73 for (size_t i=0; i<bm.size(); ++i)
74 printf(" Read QT5 bookmark: %s -> %s\n", bm.get(i)->path.get_utf8(), bm.get(i)->name.get_utf8());
75
76 UTEST_ASSERT(bm.size() == 5);
77 UTEST_ASSERT(bm.get(0)->path.equals_utf8("/home/vsadovnikov"));
78 UTEST_ASSERT(bm.get(0)->name.equals_utf8("Home"));
79 UTEST_ASSERT(bm.get(1)->path.equals_utf8("/home/vsadovnikov/Рабочий стол"));
80 UTEST_ASSERT(bm.get(1)->name.equals_utf8("Desktop"));
81 UTEST_ASSERT(bm.get(2)->path.equals_utf8("/home/vsadovnikov/Загрузки"));
82 UTEST_ASSERT(bm.get(2)->name.equals_utf8("Downloads"));
83 UTEST_ASSERT(bm.get(3)->path.equals_utf8("/"));
84 UTEST_ASSERT(bm.get(3)->name.equals_utf8("Root"));
85 UTEST_ASSERT(bm.get(4)->path.equals_utf8("/home/vsadovnikov/eclipse"));
86 UTEST_ASSERT(bm.get(4)->name.equals_utf8("eclipse"));
87 }
88
89 void test_lnk_bookmarks()
90 {
91 lltl::parray<bookmark_t> bm;
92 lsp_finally { destroy_bookmarks(&bm); };
93
94 io::Path path;
95 UTEST_ASSERT(path.set(resources()) == STATUS_OK);
96 UTEST_ASSERT(path.append_child("fmt/bookmarks/lnk") == STATUS_OK);
97
98 printf("Reading LNK bookmarks from path %s\n", path.as_native());
99 UTEST_ASSERT(read_bookmarks_lnk(&bm, &path) == STATUS_OK);
100
101 bm.qsort(compare_bookmarks);
102 for (size_t i=0; i<bm.size(); ++i)
103 printf(" Read LNK bookmark: %s -> %s\n", bm.get(i)->path.get_utf8(), bm.get(i)->name.get_utf8());
104
105 UTEST_ASSERT(bm.size() == 6);
106 UTEST_ASSERT(bm.get(0)->path.equals_utf8("C:\\cygwin\\bin"));
107 UTEST_ASSERT(bm.get(0)->name.equals_utf8("bin"));
108 UTEST_ASSERT(bm.get(1)->path.equals_utf8("C:\\cygwin"));
109 UTEST_ASSERT(bm.get(1)->name.equals_utf8("cygwin"));
110 UTEST_ASSERT(bm.get(2)->path.equals_utf8("C:\\Users\\sadko\\Desktop"));
111 UTEST_ASSERT(bm.get(2)->name.equals_utf8("Desktop"));
112 UTEST_ASSERT(bm.get(3)->path.equals_utf8("C:\\Users\\sadko\\Downloads"));
113 UTEST_ASSERT(bm.get(3)->name.equals_utf8("Downloads"));
114 UTEST_ASSERT(bm.get(4)->path.equals_utf8("C:\\mingw"));
115 UTEST_ASSERT(bm.get(4)->name.equals_utf8("mingw"));
116 UTEST_ASSERT(bm.get(5)->path.equals_utf8("C:\\PerfLogs"));
117 UTEST_ASSERT(bm.get(5)->name.equals_utf8("PerfLogs"));
118 }
119
120 void test_lsp_bookmarks()
121 {
122 lltl::parray<bookmark_t> bm;
123 lsp_finally { destroy_bookmarks(&bm); };
124
125 io::Path path;
126 UTEST_ASSERT(path.set(resources()) == STATUS_OK);
127 UTEST_ASSERT(path.append_child("fmt/bookmarks/lsp-bookmarks.json") == STATUS_OK);
128
129 printf("Reading LSP bookmarks from file %s\n", path.as_native());
130 UTEST_ASSERT(read_bookmarks(&bm, &path, "UTF-8") == STATUS_OK);
131
132 for (size_t i=0; i<bm.size(); ++i)
133 {
134 bookmark_t *b = bm.get(i);
135 printf(" Read LSP bookmark: %s -> %s\n", b->path.get_utf8(), b->name.get_utf8());
136 }
137 UTEST_ASSERT(bm.size() == 4);
138 UTEST_ASSERT(bm.get(0)->path.equals_ascii("/path1"));
139 UTEST_ASSERT(bm.get(0)->name.equals_ascii("PATH1"));
140 UTEST_ASSERT(bm.get(0)->origin == BM_LSP);
141
142 UTEST_ASSERT(bm.get(1)->path.equals_ascii("/some/another/path"));
143 UTEST_ASSERT(bm.get(1)->name.equals_ascii("Another path"));
144 UTEST_ASSERT(bm.get(1)->origin == (BM_LSP | BM_GTK3 | BM_QT5));
145
146 UTEST_ASSERT(bm.get(2)->path.equals_ascii("/some/another/path2"));
147 UTEST_ASSERT(bm.get(2)->name.equals_ascii("Another path 2"));
148 UTEST_ASSERT(bm.get(2)->origin == 0);
149
150 UTEST_ASSERT(bm.get(3)->path.is_empty());
151 UTEST_ASSERT(bm.get(3)->name.is_empty());
152 UTEST_ASSERT(bm.get(3)->origin == 0);
153 }
154
155 void add_bookmark(lltl::parray<bookmark_t> &bm, const char *path, const char *name, size_t flags)
156 {
157 bookmark_t *b = new bookmark_t;
158 UTEST_ASSERT(b != NULL);
159 UTEST_ASSERT(b->path.set_utf8(path));
160 UTEST_ASSERT(b->name.set_utf8(name));
161 b->origin = flags;
162 UTEST_ASSERT(bm.add(b));
163 }
164
165 void test_save_bookmarks()
166 {
167 lltl::parray<bookmark_t> bm;
168 lsp_finally { destroy_bookmarks(&bm); };
169
170 io::Path path;
171
172 UTEST_ASSERT(path.fmt("%s/utest-%s.json", tempdir(), full_name()) >= 0);
173
174 add_bookmark(bm, "/path1", "Test path", BM_LSP);
175 add_bookmark(bm, "/home/some/path/1", "Additional path", BM_LSP | BM_GTK3 | BM_QT5);
176 add_bookmark(bm, "/home/some/path/2", "Additional path 2", 0);
177 UTEST_ASSERT(bm.size() == 3);
178
179 printf("Saving bookmarks to file %s\n", path.as_native());
180 UTEST_ASSERT(save_bookmarks(&bm, &path) == STATUS_OK);
181
182 printf("Reading bookmarks from file %s\n", path.as_native());
183 UTEST_ASSERT(read_bookmarks(&bm, &path) == STATUS_OK);
184 for (size_t i=0; i<bm.size(); ++i)
185 {
186 bookmark_t *b = bm.get(i);
187 printf(" Read LSP bookmark: %s -> %s\n", b->path.get_utf8(), b->name.get_utf8());
188 }
189
190 UTEST_ASSERT(bm.size() == 2); // 1 item should not be stored
191 UTEST_ASSERT(bm.get(0)->path.equals_ascii("/path1"));
192 UTEST_ASSERT(bm.get(0)->name.equals_ascii("Test path"));
193 UTEST_ASSERT(bm.get(0)->origin == BM_LSP);
194
195 UTEST_ASSERT(bm.get(1)->path.equals_ascii("/home/some/path/1"));
196 UTEST_ASSERT(bm.get(1)->name.equals_ascii("Additional path"));
197 UTEST_ASSERT(bm.get(1)->origin == (BM_LSP | BM_GTK3 | BM_QT5));
198 }
199
200 void test_merge_bookmarks()
201 {
202 lltl::parray<bookmark_t> dst, src;
203 lsp_finally {
204 destroy_bookmarks(&dst);
205 destroy_bookmarks(&src);
206 };
207
208 add_bookmark(dst, "/0/0", "0-0", 0); // removed
209 add_bookmark(dst, "/0/1", "0-1", BM_LSP); // + BM_GTK3 -> changed
210 add_bookmark(dst, "/1/0", "1-0", BM_GTK3); // not changed
211 add_bookmark(dst, "/1/1", "1-1", BM_LSP | BM_GTK3); // - BM_GTK3 -> changed
212 add_bookmark(dst, "/2/0", "2-0", BM_GTK3); // - BM_GTK3 -> removed
213 UTEST_ASSERT(dst.add((bookmark_t *)NULL)); // removed
214
215 add_bookmark(src, "/0/1", "0-1", BM_GTK3);
216 add_bookmark(src, "/1/0", "1-0", BM_GTK3);
217 add_bookmark(src, "/2/1", "2-1", BM_GTK3); // added
218 UTEST_ASSERT(src.add((bookmark_t *)NULL)); // ignored
219
220 size_t changes = 0;
221 UTEST_ASSERT(merge_bookmarks(&dst, &changes, &src, BM_GTK3) == STATUS_OK);
222
223 for (size_t i=0; i<dst.size(); ++i)
224 {
225 bookmark_t *b = dst.get(i);
226 printf(" Merged LSP bookmark: %s -> %s (0x%x)\n", b->path.get_utf8(), b->name.get_utf8(), int(b->origin));
227 }
228
229 UTEST_ASSERT(changes > 0);
230 UTEST_ASSERT(dst.size() == 4);
231
232 UTEST_ASSERT(dst.get(0)->path.equals_ascii("/0/1"));
233 UTEST_ASSERT(dst.get(0)->name.equals_ascii("0-1"));
234 UTEST_ASSERT(dst.get(0)->origin == (BM_LSP | BM_GTK3));
235
236 UTEST_ASSERT(dst.get(1)->path.equals_ascii("/1/0"));
237 UTEST_ASSERT(dst.get(1)->name.equals_ascii("1-0"));
238 UTEST_ASSERT(dst.get(1)->origin == (BM_GTK3));
239
240 UTEST_ASSERT(dst.get(2)->path.equals_ascii("/1/1"));
241 UTEST_ASSERT(dst.get(2)->name.equals_ascii("1-1"));
242 UTEST_ASSERT(dst.get(2)->origin == (BM_LSP));
243
244 UTEST_ASSERT(dst.get(3)->path.equals_ascii("/2/1"));
245 UTEST_ASSERT(dst.get(3)->name.equals_ascii("2-1"));
246 UTEST_ASSERT(dst.get(3)->origin == (BM_GTK3 | BM_LSP));
247 }
248
249 UTEST_MAIN
250 {
251 printf("Testing read of GTK3 bookmarks...\n");
252 test_gtk3_bookmarks();
253 printf("Testing read of QT5 bookmarks...\n");
254 test_qt5_bookmarks();
255 printf("Testing read of LNK bookmarks...\n");
256 test_lnk_bookmarks();
257 printf("Testing read of LSP bookmarks...\n");
258 test_lsp_bookmarks();
259 printf("Testing write of LSP bookmarks...\n");
260 test_save_bookmarks();
261 printf("Testing merge of LSP bookmarks...\n");
262 test_merge_bookmarks();
263 }
264
265 UTEST_END
266