~aleteoryx/muditaos

ref: 87ed83ca5ce1209cd4591fc58d16e441928b147c muditaos/module-utils/test/test_ParserICS.cpp -rw-r--r-- 19.9 KiB
87ed83ca — Mateusz Grzegorzek [BH-701] Use TimeSetFmtSpinner in Alarm app 4 years ago
                                                                                
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>
#include "ical/ParserICS.hpp"

TEST_CASE("Testing Duration class object", "getDurationString method")
{

    SECTION("Test regular week values")
    {
        int test_values[]             = {1, 2, 4, 52, 123};
        std::string expected_values[] = {"1W", "2W", "4W", "52W", "123W"};
        int index                     = 0;
        for (auto value : test_values) {
            auto duration = Duration(value, 0, 0, 0);
            CHECK(duration.getDurationString() == expected_values[index]);
            index++;
        }
    }

    SECTION("Test regular day values")
    {
        int test_values[]             = {1, 2, 3, 7, 30, 333};
        std::string expected_values[] = {"1D", "2D", "3D", "7D", "30D", "333D"};
        int index                     = 0;
        for (auto value : test_values) {
            auto duration = Duration(0, value, 0, 0);
            CHECK(duration.getDurationString() == expected_values[index]);
            index++;
        }
    }

    SECTION("Test regular hour values")
    {
        int test_values[]             = {1, 2, 3, 8, 24, 999};
        std::string expected_values[] = {"T1H", "T2H", "T3H", "T8H", "T24H", "T999H"};
        int index                     = 0;
        for (auto value : test_values) {
            auto duration = Duration(0, 0, value, 0);
            CHECK(duration.getDurationString() == expected_values[index]);
            index++;
        }
    }

    SECTION("Test regular minutes values")
    {
        int test_values[]             = {1, 2, 5, 8, 60, 941683};
        std::string expected_values[] = {"T1M", "T2M", "T5M", "T8M", "T60M", "T941683M"};
        int index                     = 0;
        for (auto value : test_values) {
            auto duration = Duration(0, 0, 0, value);
            CHECK(duration.getDurationString() == expected_values[index]);
            index++;
        }
    }

    SECTION("Test regular mixed week and day values")
    {
        int week_test_value        = 12;
        int day_test_value         = 34;
        std::string expected_value = "12W34D";

        auto duration = Duration(week_test_value, day_test_value, 0, 0);
        CHECK(duration.getDurationString() == expected_value);
    }

    SECTION("Test regular mixed hours and minutes values")
    {
        int hours_test_value       = 3;
        int minutes_test_value     = 5;
        std::string expected_value = "T3H5M";

        auto duration = Duration(0, 0, hours_test_value, minutes_test_value);
        CHECK(duration.getDurationString() == expected_value);
    }

    SECTION("Test regular mixed weeks and minutes values")
    {
        int week_test_value        = 4;
        int day_test_value         = 0;
        int hours_test_value       = 0;
        int minutes_test_value     = 5;
        std::string expected_value = "4WT5M";

        auto duration = Duration(week_test_value, day_test_value, hours_test_value, minutes_test_value);
        CHECK(duration.getDurationString() == expected_value);
    }

    SECTION("Test regular mixed all values")
    {
        int week_test_value        = 1;
        int day_test_value         = 0;
        int hours_test_value       = 8;
        int minutes_test_value     = 23;
        std::string expected_value = "1WT8H23M";

        auto duration = Duration(week_test_value, day_test_value, hours_test_value, minutes_test_value);
        CHECK(duration.getDurationString() == expected_value);
    }

    SECTION("Test getDurationInMinutes() method")
    {
        int week_test_value    = 2;
        int day_test_value     = 1;
        int hours_test_value   = 2;
        int minutes_test_value = 30;
        uint32_t time_in_minutes =
            week_test_value * 7 * 24 * 60 + day_test_value * 24 * 60 + hours_test_value * 60 + minutes_test_value;

        auto duration = Duration(week_test_value, day_test_value, hours_test_value, minutes_test_value);
        CHECK(duration.getDurationInMinutes() == time_in_minutes);
    }

    SECTION("Test Duration(string) constructor")
    {

        std::string duration_value = "P1WT8H23M";
        std::string expected_value = "1WT8H23M";
        auto duration              = Duration(duration_value);
        CHECK(duration.getDurationString() == expected_value);

        std::string duration_value2 = "-P1W1DT8H23M";
        std::string expected_value2 = "1W1DT8H23M";
        auto duration2              = Duration(duration_value2);
        CHECK(duration2.getDurationString() == expected_value2);

        std::string duration_value3 = "-PT8H23M";
        std::string expected_value3 = "T8H23M";
        auto duration3              = Duration(duration_value3);
        CHECK(duration3.getDurationString() == expected_value3);

        std::string duration_value4 = "PT8H23M";
        std::string expected_value4 = "T8H23M";
        auto duration4              = Duration(duration_value4);
        CHECK(duration4.getDurationString() == expected_value4);
    }

    SECTION("Test Duration(string) constructor invalid")
    {
        std::string duration_value = "";
        std::string expected_value = "T0M";
        auto duration              = Duration(duration_value);
        CHECK(duration.getDurationString() == expected_value);

        std::string duration_value1 = "DTSTART:20201020T1536";
        std::string expected_value1 = "T0M";
        auto duration1              = Duration(duration_value1);
        CHECK(duration1.getDurationString() == expected_value1);
    }
}

TEST_CASE("Testing Alarm class object", "")
{

    SECTION("Test regural trigger and action audio")
    {
        auto action                        = Action::audio;
        auto duration                      = Duration(0, 0, 0, 5);
        std::string expected_action_value  = "AUDIO";
        std::string expected_trigger_value = "-PT5M";

        auto alarm = Alarm(duration, action);
        CHECK(alarm.getActionString() == expected_action_value);
        CHECK(alarm.getTriggerString() == expected_trigger_value);
    }

    SECTION("Test regural trigger and action audio")
    {
        auto action                        = Action::procedure;
        auto duration                      = Duration(0, 0, 1, 0);
        std::string expected_action_value  = "PROCEDURE";
        std::string expected_trigger_value = "-PT1H";

        auto alarm = Alarm(duration, action);
        CHECK(alarm.getActionString() == expected_action_value);
        CHECK(alarm.getTriggerString() == expected_trigger_value);
    }

    SECTION("Test regural trigger and action display")
    {
        auto action                        = Action::display;
        auto duration                      = Duration(0, 1, 1, 0);
        std::string expected_action_value  = "DISPLAY";
        std::string expected_trigger_value = "-P1DT1H";

        auto alarm = Alarm(duration, action);
        CHECK(alarm.getActionString() == expected_action_value);
        CHECK(alarm.getTriggerString() == expected_trigger_value);
    }

    SECTION("Test invalid trigger and action")
    {
        auto invalid_action                = Action::invalid;
        auto invalid_duration              = Duration(0, 0, 0, 0);
        std::string expected_action_value  = "";
        std::string expected_trigger_value = "-PT0M";

        auto alarm = Alarm(invalid_duration, invalid_action);
        CHECK(alarm.getActionString() == expected_action_value);
        CHECK(alarm.getTriggerString() == expected_trigger_value);
    }
}

TEST_CASE("Testing RecurrenceRule class object", "")
{

    SECTION("Test frequency daily, count 7 value")
    {
        auto freq                            = Frequency::daily;
        auto rrule                           = RecurrenceRule(freq, 7, 1);
        std::string expected_frequency_value = "DAILY";
        std::string expected_count_value     = "7";
        std::string expected_interval_value  = "1";
        CHECK(rrule.getFrequencyString() == expected_frequency_value);
        CHECK(rrule.getCountString() == expected_count_value);
        CHECK(rrule.getIntervalString() == expected_interval_value);
    }

    SECTION("Test frequency weekly, count 4 value")
    {
        auto freq                            = Frequency::weekly;
        auto rrule                           = RecurrenceRule(freq, 4, 1);
        std::string expected_frequency_value = "WEEKLY";
        std::string expected_count_value     = "4";
        std::string expected_interval_value  = "1";
        CHECK(rrule.getFrequencyString() == expected_frequency_value);
        CHECK(rrule.getCountString() == expected_count_value);
        CHECK(rrule.getIntervalString() == expected_interval_value);
    }

    SECTION("Test frequency biweekly, count 4 value")
    {
        auto freq                            = Frequency::weekly;
        auto rrule                           = RecurrenceRule(freq, 4, 2);
        std::string expected_frequency_value = "WEEKLY";
        std::string expected_count_value     = "4";
        std::string expected_interval_value  = "2";
        CHECK(rrule.getFrequencyString() == expected_frequency_value);
        CHECK(rrule.getCountString() == expected_count_value);
        CHECK(rrule.getIntervalString() == expected_interval_value);
    }

    SECTION("Test frequency monthly, count 12 value")
    {
        auto freq                            = Frequency::monthly;
        auto rrule                           = RecurrenceRule(freq, 12, 1);
        std::string expected_frequency_value = "MONTHLY";
        std::string expected_count_value     = "12";
        std::string expected_interval_value  = "1";
        CHECK(rrule.getFrequencyString() == expected_frequency_value);
        CHECK(rrule.getCountString() == expected_count_value);
        CHECK(rrule.getIntervalString() == expected_interval_value);
    }

    SECTION("Test frequency yearly, count 4 value")
    {
        auto freq                            = Frequency::yearly;
        auto rrule                           = RecurrenceRule(freq, 4, 1);
        std::string expected_frequency_value = "YEARLY";
        std::string expected_count_value     = "4";
        std::string expected_interval_value  = "1";
        CHECK(rrule.getFrequencyString() == expected_frequency_value);
        CHECK(rrule.getCountString() == expected_count_value);
        CHECK(rrule.getIntervalString() == expected_interval_value);
    }

    SECTION("Test frequency invalid")
    {
        auto freq                            = Frequency::invalid;
        auto rrule                           = RecurrenceRule(freq, 1, 1);
        std::string expected_frequency_value = "";
        std::string expected_count_value     = "";
        std::string expected_interval_value  = "";
        CHECK(rrule.getFrequencyString() == expected_frequency_value);
        CHECK(rrule.getCountString() == expected_count_value);
        CHECK(rrule.getIntervalString() == expected_interval_value);
    }

    SECTION("Test count invalid")
    {
        auto freq                            = Frequency::daily;
        auto rrule                           = RecurrenceRule(freq, 1, 0);
        std::string expected_frequency_value = "";
        std::string expected_count_value     = "";
        std::string expected_interval_value  = "";
        CHECK(rrule.getFrequencyString() == expected_frequency_value);
        CHECK(rrule.getCountString() == expected_count_value);
        CHECK(rrule.getIntervalString() == expected_interval_value);
    }

    SECTION("Test interval invalid")
    {
        auto freq                            = Frequency::yearly;
        auto rrule                           = RecurrenceRule(freq, 0, 1);
        std::string expected_frequency_value = "";
        std::string expected_count_value     = "";
        std::string expected_interval_value  = "";
        CHECK(rrule.getFrequencyString() == expected_frequency_value);
        CHECK(rrule.getCountString() == expected_count_value);
        CHECK(rrule.getIntervalString() == expected_interval_value);
    }
}

TEST_CASE("Testing Event class object", "")
{

    SECTION("Test get DTStart and DTEnd in TimePoint value")
    {
        auto summary = "test";
        auto DTstart = TimePointFromString("2020-11-05 15:00:00");
        auto DTend   = TimePointFromString("2020-11-05 16:00:00");

        auto event = Event(summary, DTstart, DTend, "test_uid");

        std::string expected_summary_value = "test";
        std::string expected_DTstart_value = "20201105T150000";
        std::string expected_DTend_value   = "20201105T160000";
        CHECK(event.getSummary() == expected_summary_value);
        CHECK(event.getDTStartString() == expected_DTstart_value);
        CHECK(event.getDTEndString() == expected_DTend_value);

        auto dtstart_timepoint              = event.getDTStartTimePoint();
        auto dtend_timepoint                = event.getDTEndTimePoint();
        std::string expected_dtstart_string = "2020-11-05 15:00:00";
        std::string expected_dtend_string   = "2020-11-05 16:00:00";
        CHECK(TimePointToString(dtstart_timepoint) == expected_dtstart_string);
        CHECK(TimePointToString(dtend_timepoint) == expected_dtend_string);
    }

    SECTION("Test event with invalid timepoints values")
    {
        auto summary = "invalid";
        auto DTstart = TimePointFromString("2020-11-05 15:00");
        auto DTend   = TimePointFromString("2020-11 16:00:00");

        auto event = Event(summary, DTstart, DTend, "test_uid");

        std::string expected_summary_value = "invalid";
        std::string expected_DTstart_value = "19700101T000000";
        std::string expected_DTend_value   = "19700101T000000";
        CHECK(event.getSummary() == expected_summary_value);
        CHECK(event.getDTStartString() == expected_DTstart_value);
        CHECK(event.getDTEndString() == expected_DTend_value);

        auto dtstart_timepoint              = event.getDTStartTimePoint();
        auto dtend_timepoint                = event.getDTEndTimePoint();
        std::string expected_dtstart_string = "1970-01-01 00:00:00";
        std::string expected_dtend_string   = "1970-01-01 00:00:00";
        CHECK(TimePointToString(dtstart_timepoint) == expected_dtstart_string);
        CHECK(TimePointToString(dtend_timepoint) == expected_dtend_string);
    }
}

TEST_CASE("Testing ParserICS class methods", "")
{
    SECTION("Test importEvents and exportEvents")
    {
        /// Event1
        auto summary = "test";
        auto DTstart = TimePointFromString("2020-11-05 15:00:00");
        auto DTend   = TimePointFromString("2020-11-05 16:00:00");
        auto UID     = createUID();
        auto event   = Event(summary, DTstart, DTend, UID);

        auto freq  = Frequency::weekly;
        auto rrule = RecurrenceRule(freq, 4, 2);

        auto action     = Action::audio;
        auto duration   = Duration(0, 0, 0, 5);
        auto alarm      = Alarm(duration, action);
        auto icalEvent1 = ICalEvent{event, alarm, rrule};

        /// Event2
        auto summary2 = "test2";
        auto DTstart2 = TimePointFromString("2019-09-05 10:00:00");
        auto DTend2   = TimePointFromString("2019-09-05 14:30:00");
        auto UID2     = createUID();
        auto event2   = Event(summary2, DTstart2, DTend2, UID2);

        ICalEvent icalEvent2;
        icalEvent2.event = event2;

        /// Event3
        auto summary3 = "test3";
        auto DTstart3 = TimePointFromString("2021-01-01 00:00:00");
        auto DTend3   = TimePointFromString("2021-01-01 10:45:00");
        auto UID3     = createUID();
        auto event3   = Event(summary3, DTstart3, DTend3, UID3);

        auto freq3  = Frequency::daily;
        auto rrule3 = RecurrenceRule(freq3, 7, 1);

        ICalEvent icalEvent3;
        icalEvent3.event = event3;
        icalEvent3.rrule = rrule3;

        /// check
        auto IcalEventsINPUT = std::vector<ICalEvent>();
        IcalEventsINPUT.push_back(icalEvent1);
        IcalEventsINPUT.push_back(icalEvent2);
        IcalEventsINPUT.push_back(icalEvent3);

        auto parser = ParserICS();
        parser.importEvents(IcalEventsINPUT);

        [[maybe_unused]] auto icsFormat = parser.getIcsData();

        std::string expected_uid[]     = {UID, UID2, UID3};
        std::string expected_summary[] = {summary, summary2, summary3};
        std::string expected_DTstart[] = {"20201105T150000", "20190905T100000", "20210101T000000"};
        std::string expected_DTend[]   = {"20201105T160000", "20190905T143000", "20210101T104500"};

        auto icalEventsOUTPUT = parser.exportEvents();

        CHECK(icalEventsOUTPUT[0].event.getUID() == expected_uid[0]);
        CHECK(icalEventsOUTPUT[0].event.getSummary() == expected_summary[0]);
        CHECK(icalEventsOUTPUT[0].event.getDTStartString() == expected_DTstart[0]);
        CHECK(icalEventsOUTPUT[0].event.getDTEndString() == expected_DTend[0]);

        CHECK(icalEventsOUTPUT[1].event.getUID() == expected_uid[1]);
        CHECK(icalEventsOUTPUT[1].event.getSummary() == expected_summary[1]);
        CHECK(icalEventsOUTPUT[1].event.getDTStartString() == expected_DTstart[1]);
        CHECK(icalEventsOUTPUT[1].event.getDTEndString() == expected_DTend[1]);

        CHECK(icalEventsOUTPUT[2].event.getUID() == expected_uid[2]);
        CHECK(icalEventsOUTPUT[2].event.getSummary() == expected_summary[2]);
        CHECK(icalEventsOUTPUT[2].event.getDTStartString() == expected_DTstart[2]);
        CHECK(icalEventsOUTPUT[2].event.getDTEndString() == expected_DTend[2]);

        CHECK(icalEventsOUTPUT[0].rrule.getFrequencyString() == "WEEKLY");
        CHECK(icalEventsOUTPUT[0].rrule.getCountString() == "4");
        CHECK(icalEventsOUTPUT[0].rrule.getIntervalString() == "2");

        CHECK(icalEventsOUTPUT[2].rrule.getFrequencyString() == "DAILY");
        CHECK(icalEventsOUTPUT[2].rrule.getCountString() == "7");
        CHECK(icalEventsOUTPUT[2].rrule.getIntervalString() == "1");

        CHECK(icalEventsOUTPUT[0].alarm.getActionString() == "AUDIO");
        CHECK(icalEventsOUTPUT[0].alarm.getTriggerString() == "-PT5M");
    }

    SECTION("Test importEvents and exportEvents invalid")
    {
        /// Event1
        auto event = Event();

        auto freq  = Frequency::weekly;
        auto rrule = RecurrenceRule(freq, 4, 2);

        auto action   = Action::audio;
        auto duration = Duration(0, 0, 0, 5);
        auto alarm    = Alarm(duration, action);

        auto icalEvent1 = ICalEvent{event, alarm, rrule};

        /// Event2
        auto summary2   = "test2";
        auto DTstart2   = TimePointFromString("2019-09-05 10:00:00");
        auto DTend2     = TimePointFromString("2019-09-05 14:30:00");
        auto UID2       = createUID();
        auto event2     = Event(summary2, DTstart2, DTend2, UID2);
        auto alarm2     = Alarm();
        auto rrule2     = RecurrenceRule();
        auto icalEvent2 = ICalEvent{event2, alarm2, rrule2};

        /// check
        auto IcalEventsINPUT = std::vector<ICalEvent>();
        IcalEventsINPUT.push_back(icalEvent1);
        IcalEventsINPUT.push_back(icalEvent2);

        auto parser = ParserICS();
        parser.importEvents(std::move(IcalEventsINPUT));

        [[maybe_unused]] auto icsFormat = parser.getIcsData();

        std::string expected_uid     = UID2;
        std::string expected_summary = summary2;
        std::string expected_DTstart = "20190905T100000";
        std::string expected_DTend   = "20190905T143000";

        auto icalEventsOUTPUT = parser.exportEvents();

        CHECK(icalEventsOUTPUT[0].event.getUID() == expected_uid);
        CHECK(icalEventsOUTPUT[0].event.getSummary() == expected_summary);
        CHECK(icalEventsOUTPUT[0].event.getDTStartString() == expected_DTstart);
        CHECK(icalEventsOUTPUT[0].event.getDTEndString() == expected_DTend);

        CHECK(icalEventsOUTPUT[0].rrule.getFrequencyString() == "");
        CHECK(icalEventsOUTPUT[0].rrule.getCountString() == "");
        CHECK(icalEventsOUTPUT[0].rrule.getIntervalString() == "");

        CHECK(icalEventsOUTPUT[0].alarm.getActionString() == "");
        CHECK(icalEventsOUTPUT[0].alarm.getTriggerString() == "-PT0M");
    }
}