Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/resource/dirloader.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: 26 окт. 2020 г.
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/resource/DirLoader.h>
24 #include <stdlib.h>
25
26 using namespace lsp;
27
28 UTEST_BEGIN("runtime.resource", dirloader)
29
30 bool match_resource(const resource::resource_t *item, const resource::resource_t *list, size_t count)
31 {
32 for (size_t i=0; i<count; ++i)
33 {
34 if (item->type != list[i].type)
35 continue;
36 if (strcmp(item->name, list[i].name) == 0)
37 return true;
38 }
39 return false;
40 }
41
42 void test_dir(resource::ILoader *loader, const char *path, const resource::resource_t *list, size_t count)
43 {
44 resource::resource_t *items = NULL;
45
46 printf("Testing directory listing of \"%s\"...\n", path);
47
48 ssize_t nitems = loader->enumerate(path, &items);
49 UTEST_ASSERT(nitems == ssize_t(count));
50 UTEST_ASSERT(items != NULL);
51
52 for (ssize_t i=0; i<nitems; ++i)
53 {
54 UTEST_ASSERT_MSG(match_resource(&items[i], list, count), "Unexpected resource: %s", items[i].name);
55 }
56
57 free(items);
58 }
59
60 void test_file(resource::ILoader *loader, const char *path, const char *data)
61 {
62 printf("Testing read of resource \"%s\"...\n", path);
63 LSPString line;
64
65 io::IInSequence *is = loader->read_sequence(path, "UTF-8");
66 UTEST_ASSERT(is != NULL);
67 UTEST_ASSERT(is->read_line(&line, true) == STATUS_OK);
68 UTEST_ASSERT(is->read_line(&line, true) == STATUS_EOF);
69 UTEST_ASSERT(is->close() == STATUS_OK);
70 delete is;
71
72 UTEST_ASSERT(line.equals_ascii(data));
73 }
74
75 void test_unexisting(resource::ILoader *loader, const char *path)
76 {
77 printf("Testing read of unexisting resource \"%s\"...\n", path);
78 io::IInSequence *is = loader->read_sequence(path, "UTF-8");
79 UTEST_ASSERT(is == NULL);
80 UTEST_ASSERT(loader->last_error() != STATUS_OK);
81 }
82
83 UTEST_MAIN
84 {
85 static const resource::resource_t list1[] =
86 {
87 { resource::RES_DIR, "dir" },
88 { resource::RES_FILE, "file.txt" },
89 { resource::RES_FILE, "test.txt" }
90 };
91
92 static const resource::resource_t list2[] =
93 {
94 { resource::RES_FILE, "item.txt" }
95 };
96
97 resource::DirLoader loader;
98 loader.set_enforce(true);
99 loader.set_path(resources());
100
101 printf("Resource directory: %s\n", resources());
102
103 test_dir(&loader, "resource", list1, sizeof(list1)/sizeof(resource::resource_t));
104 test_dir(&loader, "../resource", list1, sizeof(list1)/sizeof(resource::resource_t));
105 test_dir(&loader, "./resource", list1, sizeof(list1)/sizeof(resource::resource_t));
106 test_dir(&loader, "resource/dir", list2, sizeof(list2)/sizeof(resource::resource_t));
107
108 test_file(&loader, "resource/file.txt", "file.txt");
109 test_file(&loader, "resource/test.txt", "test.txt");
110 test_file(&loader, "resource/dir/item.txt", "item.txt");
111 test_unexisting(&loader, "resource/dir/unexisting.txt");
112 }
113
114 UTEST_END
115
116