Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/i18n/json_dictionary.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: 2 мар. 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/i18n/JsonDictionary.h>
24
25 using namespace lsp;
26
27 UTEST_BEGIN("runtime.i18n", json_dictionary)
28
29 void ck_att(i18n::IDictionary *d, size_t index, const char *name, const char *value)
30 {
31 LSPString k, v;
32 printf(" fetching %s -> %s...\n", name, value);
33 UTEST_ASSERT(d->get_value(index, &k, &v) == STATUS_OK);
34 printf(" fetched %s -> %s...\n", k.get_utf8(), v.get_utf8());
35 UTEST_ASSERT(k.equals_utf8(name));
36 UTEST_ASSERT(v.equals_utf8(value));
37 }
38
39 void ck_child(i18n::IDictionary *d, size_t index, const char *name, bool special = false)
40 {
41 LSPString k;
42 i18n::IDictionary *c = NULL;
43
44 printf(" fetching child: -> %s...\n", name);
45 UTEST_ASSERT(d->get_child(index, &k, &c) == STATUS_OK);
46 UTEST_ASSERT(k.equals_utf8(name));
47
48 size_t idx = 0;
49 if (special)
50 ck_att(c, idx++, "", "special_case");
51 ck_att(c, idx++, "a1", "x1");
52 ck_att(c, idx++, "a2", "x2");
53 ck_att(c, idx++, "a3", "x3");
54 }
55
56 void ck_lookup(i18n::IDictionary *d, const char *name, const char *value)
57 {
58 LSPString v;
59 printf(" lookup %s ...\n", name, value);
60 UTEST_ASSERT(d->lookup(name, &v) == STATUS_OK);
61 printf(" got %s -> %s...\n", name, v.get_utf8());
62 UTEST_ASSERT(v.equals_utf8(value));
63 }
64
65 void validate(i18n::IDictionary *d)
66 {
67 ck_att(d, 0, "k1", "v1");
68 ck_child(d, 1, "k2", true);
69 ck_child(d, 2, "k3");
70 ck_child(d, 3, "k4");
71 ck_child(d, 4, "k5");
72 ck_child(d, 5, "k6");
73 ck_child(d, 6, "k7");
74
75 ck_lookup(d, "k1", "v1");
76 ck_lookup(d, "k2.a3", "x3");
77 ck_lookup(d, "k8.k1.k2", "z2");
78 ck_lookup(d, "k2", "special_case");
79 ck_lookup(d, "k8.k1", "special_case2");
80 }
81
82 UTEST_MAIN
83 {
84 i18n::JsonDictionary d;
85 i18n::IDictionary *xd = NULL;
86 io::Path path;
87
88 printf("Testing load of valid dictionary...\n");
89 UTEST_ASSERT(path.fmt("%s/i18n/valid.json", resources()) > 0);
90 UTEST_ASSERT(d.init(&path) == STATUS_OK);
91
92 printf("Testing lookup of dictionary node...\n");
93
94 UTEST_ASSERT(d.lookup("lalala", &xd) == STATUS_NOT_FOUND);
95 UTEST_ASSERT(d.lookup("k2", &xd) == STATUS_OK);
96 ck_att(xd, 0, "", "special_case");
97 ck_att(xd, 1, "a1", "x1");
98 ck_att(xd, 2, "a2", "x2");
99 ck_att(xd, 3, "a3", "x3");
100 UTEST_ASSERT(d.lookup("k8.k1", &xd) == STATUS_OK);
101 ck_att(xd, 0, "", "special_case2");
102 ck_att(xd, 1, "k2", "z2");
103
104 printf("Validating dictionary...\n");
105 validate(&d);
106
107 printf("Testing load of invalid dictionary...\n");
108 UTEST_ASSERT(path.fmt("%s/i18n/invalid1.json", resources()) > 0);
109 UTEST_ASSERT(d.init(&path) != STATUS_OK);
110
111 UTEST_ASSERT(path.fmt("%s/i18n/invalid2.json", resources()) > 0);
112 UTEST_ASSERT(d.init(&path) != STATUS_OK);
113
114 UTEST_ASSERT(path.fmt("%s/i18n/invalid3.json", resources()) > 0);
115 UTEST_ASSERT(d.init(&path) != STATUS_OK);
116
117 printf("Validating that state of dictionary has not changed...\n");
118 validate(&d);
119 }
120
121 UTEST_END