File: xml_test.cpp

package info (click to toggle)
wf-config 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: cpp: 5,743; xml: 37; makefile: 4
file content (445 lines) | stat: -rw-r--r-- 12,763 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
#include <set>

#include <sstream>
#include <wayfire/config/types.hpp>
#include <wayfire/config/compound-option.hpp>
#include <wayfire/config/xml.hpp>
#include <wayfire/util/log.hpp>
#include <linux/input-event-codes.h>

static const std::string xml_option_int =
    R"(
<option name="IntOption" type="int">
<default>3</default>
<min>0</min>
<max>10</max>
</option>
)";

static const std::string xml_option_string =
    R"(
<option name="StringOption" type="string">
<default></default>
</option>
)";

static const std::string xml_option_key =
    R"(
<option name="KeyOption" type="key">
<default>&lt;super&gt; KEY_E</default>
</option>
)";

static const std::string xml_option_dyn_list =
    R"(
<option name="DynList" type="dynamic-list">
<entry prefix="cmd_" type="string"/>
<entry prefix="act_" type="activator"/>
<unused/>
</option>
)";

static const std::string xml_option_dyn_list_no_prefix =
    R"(
<option name="DynList" type="dynamic-list">
<entry type="string"/>
</option>
)";

static const std::string xml_option_dyn_list_no_type =
    R"(
<option name="DynList" type="dynamic-list">
<entry prefix="pre_"/>
</option>
)";

static const std::string xml_option_dyn_list_wrong_type =
    R"(
<option name="DynList" type="dynamic-list">
<entry prefix="pre_" type="invalid"/>
</option>
)";

static const std::string xml_option_bad_tag =
    R"(
<invalid name="KeyOption" type="key">
<default>&lt;super&gt; KEY_E</default>
</invalid>
)";

static const std::string xml_option_int_bad_min =
    R"(
<option name="IntOption" type="int">
<default>3</default>
<min>sfd</min>
<max>10</max>
</option>
)";

static const std::string xml_option_int_bad_max =
    R"(
<option name="IntOption" type="int">
<default>3</default>
<min>0</min>
<max>sdf</max>
</option>
)";

static const std::string xml_option_bad_type =
    R"(
<option name="KeyOption" type="unknown">
<default>&lt;super&gt; KEY_E</default>
</option>
)";

static const std::string xml_option_bad_default =
    R"(
<option name="KeyOption" type="key">
<default>&lt;super&gt; e</default>
</option>
)";


static const std::string xml_option_missing_name =
    R"(
<option type="int">
</option>
)";

static const std::string xml_option_missing_type =
    R"(
<option name="IntOption">
</option>
)";

static const std::string xml_option_missing_default_value =
    R"(
<option name="IntOption" type="int">
</option>
)";

static const std::string xml_option_dyn_list_default =
    R"(
<option name="DynListDefault" type="dynamic-list">
    <entry prefix="with_default_" type="int">
        <default>9</default>
    </entry>
    <entry prefix="no_default_" type="string"/>
</option>
)";

#include "expect_line.hpp"

TEST_CASE("wf::config::xml::create_option")
{
    std::stringstream log;
    wf::log::initialize_logging(log,
        wf::log::LOG_LEVEL_DEBUG, wf::log::LOG_COLOR_MODE_OFF);

    namespace wxml = wf::config::xml;
    namespace wc   = wf::config;
    xmlNodePtr option_node;

    auto initialize_option = [&] (std::string source)
    {
        auto node = xmlParseDoc((const xmlChar*)source.c_str());
        REQUIRE(node != nullptr);
        option_node = xmlDocGetRootElement(node);
        return wxml::create_option_from_xml_node(option_node);
    };

    SUBCASE("Not an XML option")
    {
        auto opt = std::make_shared<wf::config::option_t<int>>("Test", 1);
        CHECK(wxml::get_option_xml_node(opt) == nullptr);
    }

    SUBCASE("IntOption")
    {
        auto option = initialize_option(xml_option_int);
        REQUIRE(option != nullptr);

        CHECK(option->get_name() == "IntOption");

        auto as_int =
            std::dynamic_pointer_cast<wc::option_t<int>>(option);
        REQUIRE(as_int);
        REQUIRE(as_int->get_minimum());
        REQUIRE(as_int->get_maximum());

        CHECK(as_int->get_value() == 3);
        CHECK(as_int->get_minimum().value() == 0);
        CHECK(as_int->get_maximum().value() == 10);
        CHECK(wxml::get_option_xml_node(as_int) == option_node);
    }

    SUBCASE("StringOption")
    {
        auto option = initialize_option(xml_option_string);
        REQUIRE(option != nullptr);

        CHECK(option->get_name() == "StringOption");

        auto as_string =
            std::dynamic_pointer_cast<wc::option_t<std::string>>(option);
        REQUIRE(as_string);
        CHECK(as_string->get_value() == "");
    }

    SUBCASE("KeyOption")
    {
        auto option = initialize_option(xml_option_key);
        REQUIRE(option != nullptr);

        CHECK(option->get_name() == "KeyOption");

        auto as_key =
            std::dynamic_pointer_cast<wc::option_t<wf::keybinding_t>>(option);
        REQUIRE(as_key);

        CHECK(as_key->get_value() ==
            wf::keybinding_t{wf::KEYBOARD_MODIFIER_LOGO, KEY_E});
        CHECK(wxml::get_option_xml_node(option) == option_node);
    }

    SUBCASE("DynamicList")
    {
        auto option = initialize_option(xml_option_dyn_list);
        REQUIRE(option != nullptr);

        CHECK(option->get_name() == "DynList");

        auto as_co =
            std::dynamic_pointer_cast<wc::compound_option_t>(option);
        REQUIRE(as_co != nullptr);

        CHECK(as_co->get_value<std::string, wf::activatorbinding_t>() ==
            wf::config::compound_list_t<std::string, wf::activatorbinding_t>{});

        const auto& entries = as_co->get_entries();
        REQUIRE(entries.size() == 2);
        CHECK(
            dynamic_cast<wc::compound_option_entry_t<std::string>*>(entries[0].get()));
        CHECK(dynamic_cast<wc::compound_option_entry_t<wf::activatorbinding_t>*>(
            entries[1].get()));
    }

    SUBCASE("DynamicListDefaultOption")
    {
        auto option = initialize_option(xml_option_dyn_list_default);
        REQUIRE(option != nullptr);

        auto as_co = std::dynamic_pointer_cast<wc::compound_option_t>(option);
        REQUIRE(as_co != nullptr);

        CHECK(as_co->get_value<int, std::string>() ==
            wf::config::compound_list_t<int, std::string>{});

        const auto& entries = as_co->get_entries();
        REQUIRE(entries.size() == 2);
        CHECK(dynamic_cast<wc::compound_option_entry_t<int>*>(entries[0].get()));
        REQUIRE(entries[0]->get_default_value() == "9");
        CHECK(
            dynamic_cast<wc::compound_option_entry_t<std::string>*>(entries[1].get()));
        REQUIRE(entries[1]->get_default_value() == std::nullopt);
    }

    /* Generate a subcase where the given xml source can't be parsed to an
     * option, and check that the output in the log is as expected. */
#define SUBCASE_BAD_OPTION(subcase_name, xml_source, expected_log) \
    SUBCASE(subcase_name) \
    { \
        auto option = initialize_option(xml_source); \
        CHECK(option == nullptr); \
        EXPECT_LINE(log, expected_log); \
    }

    SUBCASE_BAD_OPTION("Invalid xml tag",
        xml_option_bad_tag, "is not an option element");

    SUBCASE_BAD_OPTION("Invalid option type",
        xml_option_bad_type, "invalid type \"unknown\"");

    SUBCASE_BAD_OPTION("Invalid default value",
        xml_option_bad_default, "invalid default value");

    SUBCASE_BAD_OPTION("Invalid minimum value",
        xml_option_int_bad_min, "invalid minimum value");

    SUBCASE_BAD_OPTION("Invalid maximum value",
        xml_option_int_bad_max, "invalid maximum value");

    SUBCASE_BAD_OPTION("Missing option name",
        xml_option_missing_name, "missing \"name\" attribute");

    SUBCASE_BAD_OPTION("Missing option type",
        xml_option_missing_type, "missing \"type\" attribute");

    SUBCASE_BAD_OPTION("Missing option default value",
        xml_option_missing_default_value, "no default value specified");

    SUBCASE_BAD_OPTION("Dynamic list without prefix",
        xml_option_dyn_list_no_prefix, "missing \"prefix\" attribute");

    SUBCASE_BAD_OPTION("Dynamic list without type",
        xml_option_dyn_list_no_type, "missing \"type\" attribute");

    SUBCASE_BAD_OPTION("Dynamic list with invalid type",
        xml_option_dyn_list_wrong_type, "invalid type");
}

/* ------------------------- create_section test ---------------------------- */
static const std::string xml_section_empty =
    R"(
<plugin name="TestPluginEmpty">
</plugin>
)";

static const std::string xml_section_no_plugins =
    R"(
<plugin name="TestPluginNoPlugins">
<description> </description>
<check> </check>
</plugin>
)";

static const std::string xml_section_full =
    R"(
<plugin name="TestPluginFull">
    <option name="KeyOption" type="key">
        <default>&lt;super&gt; KEY_E</default>
    </option>
    <option name="ButtonOption" type="button">
        <default>&lt;super&gt; BTN_LEFT</default>
    </option>
    <option name="TouchOption" type="gesture">
        <default>swipe up 3</default>
    </option>
    <option name="ActivatorOption" type="activator">
        <default>&lt;super&gt; KEY_E | swipe up 3</default>
    </option>
    <option name="IntOption" type="int">
        <default>3</default>
    </option>
    <option name="OutputModeOption" type="output::mode">
        <default>1920x1080</default>
    </option>
    <option name="OutputPositionOption" type="output::position">
        <default>0, 0</default>
    </option>
    <group>
        <option name="BoolOption" type="bool">
            <default>true</default>
        </option>
        <option name="DoubleOption" type="double">
            <default>5.0</default>
        </option>
        <subgroup>
            <option name="StringOption" type="string">
                <default>test</default>
            </option>
        </subgroup>
        <option name="KeyOption2" type="invalid">
            <default>&lt;super&gt; KEY_T</default>
        </option>
    </group>
</plugin>
)";

static const std::string xml_section_missing_name =
    R"(
<plugin>
    <option name="KeyOption" type="key">
        <default>&lt;super&gt; KEY_T</default>
    </option>
</plugin>
)";

static const std::string xml_section_bad_tag =
    R"(
<invalid>
    <option name="KeyOption" type="key">
        <default>&lt;super&gt; KEY_T</default>
    </option>
</invalid>
)";

TEST_CASE("wf::config::xml::create_section")
{
    std::stringstream log;
    wf::log::initialize_logging(log,
        wf::log::LOG_LEVEL_DEBUG, wf::log::LOG_COLOR_MODE_OFF);

    namespace wxml = wf::config::xml;
    namespace wc   = wf::config;

    xmlNodePtr section_root;
    auto initialize_section = [&] (std::string xml_source)
    {
        auto node = xmlParseDoc((const xmlChar*)xml_source.c_str());
        REQUIRE(node != nullptr);
        section_root = xmlDocGetRootElement(node);
        return wxml::create_section_from_xml_node(section_root);
    };

    SUBCASE("Section without XML")
    {
        auto section = std::make_shared<wc::section_t>("TestSection");
        CHECK(wxml::get_section_xml_node(section) == nullptr);
    }

    SUBCASE("Empty section")
    {
        auto section = initialize_section(xml_section_empty);
        REQUIRE(section != nullptr);
        CHECK(section->get_name() == "TestPluginEmpty");
        CHECK(section->get_registered_options().empty());
        CHECK(wxml::get_section_xml_node(section) == section_root);
    }

    SUBCASE("Empty section - unnecessary data")
    {
        auto section = initialize_section(xml_section_no_plugins);
        REQUIRE(section != nullptr);
        CHECK(section->get_name() == "TestPluginNoPlugins");
        CHECK(section->get_registered_options().empty());
        CHECK(wxml::get_section_xml_node(section) == section_root);
    }

    SUBCASE("Section with options")
    {
        auto section = initialize_section(xml_section_full);
        REQUIRE(section != nullptr);
        CHECK(section->get_name() == "TestPluginFull");

        auto opts = section->get_registered_options();
        std::set<std::string> opt_names;
        for (auto& opt : opts)
        {
            opt_names.insert(opt->get_name());
        }

        std::set<std::string> expected_names = {
            "KeyOption", "ButtonOption", "TouchOption", "ActivatorOption",
            "IntOption", "DoubleOption", "BoolOption", "StringOption",
            "OutputModeOption", "OutputPositionOption"};
        CHECK(opt_names == expected_names);
        CHECK(wxml::get_section_xml_node(section) == section_root);
    }

    SUBCASE("Missing section name")
    {
        auto section = initialize_section(xml_section_missing_name);
        CHECK(section == nullptr);
        EXPECT_LINE(log, "missing \"name\" attribute");
    }

    SUBCASE("Invalid section xml tag")
    {
        auto section = initialize_section(xml_section_bad_tag);
        CHECK(section == nullptr);
        EXPECT_LINE(log, "is not a plugin/object element");
    }
}