summaryrefslogtreecommitdiff
path: root/src/test/regression-cxx.cpp
blob: 6bb33b3ac1058feee2f485d76229eb37a2a4c1cd (plain)
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
/**
 (C) COPYRIGHT 2001, Critical Path

 This library is free software; you can redistribute it and/or modify
 it under the terms of either:

    The LGPL as published by the Free Software Foundation, version
    2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html

 Or:

    The Mozilla Public License Version 2.0. You may obtain a copy of
    the License at https://www.mozilla.org/MPL/
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

extern "C" {
#include "regression.h"
#include "libical/icalparser.h"
}

#include "icalproperty_cxx.h"
#include "vcomponent_cxx.h"
using namespace LibICal;

#include <string>
using namespace std;

char content[] = "BEGIN:VCALENDAR\n\
VERSION:2.1\n\
BEGIN:VEVENT\n\
UID:abcd12345\n\
DTSTART:20020307T180000Z\n\
DTEND:20020307T190000Z\n\
SUMMARY:Important Meeting\n\
END:VEVENT\n\
END:VCALENDAR";

void test_cxx(void)
{
    ICalProperty *summProp  = new ICalProperty(ICAL_SUMMARY_PROPERTY);
    ICalProperty *startProp = new ICalProperty(ICAL_DTSTART_PROPERTY);
    ICalProperty *endProp   = new ICalProperty(ICAL_DTEND_PROPERTY);
    ICalProperty *locationProp = new ICalProperty(ICAL_LOCATION_PROPERTY);
    ICalProperty *descProp = new ICalProperty(ICAL_DESCRIPTION_PROPERTY);

    ok("Valid SUMMARY     Property", (summProp != 0));
    ok("Valid DTSTART     Property", (startProp != 0));
    ok("Valid DTEND       Property", (endProp != 0));
    ok("Valid LOCATION    Property", (locationProp != 0));
    ok("Valid DESCRIPTION Property", (descProp != 0));

    struct icaltimetype starttime = icaltime_from_string("20011221T180000Z"); // UTC time ends in Z
    struct icaltimetype endtime = icaltime_from_string("20020101T080000Z");   // UTC time ends in Z

    summProp->set_summary(
        string("jon said: change dir to c:\\rest\\test\\nest to get the file "
               "called <foo.dat>\nthis should be in the next line."));
    startProp->set_dtstart(starttime);
    endProp->set_dtend(endtime);
    locationProp->set_location(string("SF, California; Seattle, Washington"));
    descProp->set_description(string("The best cities on the west coast, "
                                     "hit 'NO' if you don't agree!\n"));

    VEvent *vEvent = new VEvent();

    ok("Create a new VEvent", (vEvent != 0));

    vEvent->add_property(summProp);
    vEvent->add_property(startProp);
    vEvent->add_property(endProp);
    vEvent->add_property(locationProp);
    vEvent->add_property(descProp);

    //
    str_is("vEvent->get_summary()",
           vEvent->get_summary().c_str(),
           "jon said: change dir to c:\\rest\\test\\nest to get the file "
           "called <foo.dat>\nthis should be in the next line.");

    str_is("vEvent->get_dtstart()",
           icaltime_as_ical_string(vEvent->get_dtstart()),
           "20011221T180000Z");

    str_is("vEvent->get_dtend()",
           icaltime_as_ical_string(vEvent->get_dtend()),
           "20020101T080000Z");

    ok("vEvent->as_ical_string()",
       (vEvent->as_ical_string().c_str() != 0));

    if (VERBOSE != 0) {
        printf("Summary: %s\n", vEvent->get_summary().c_str());
        printf("DTSTART: %s\n", icaltime_as_ical_string(vEvent->get_dtstart()));
        printf("DTEND: %s\n", icaltime_as_ical_string(vEvent->get_dtend()));
        printf("LOCATION: %s\n", vEvent->get_location().c_str());
        printf("DESCRIPTION: %s\n", vEvent->get_description().c_str());

        printf("vcomponent: %s", vEvent->as_ical_string().c_str());
    }

    delete descProp;
    delete locationProp;
    delete endProp;
    delete startProp;
    delete summProp;
    delete vEvent;

    VComponent ic(icalparser_parse_string(const_cast<const char *>(content)));
    ok("Parsing component", (ic.is_valid()));

    if (VERBOSE != 0) {
        printf("%s\n", ic.as_ical_string().c_str());
    }

    // component is wrapped within BEGIN:VCALENDAR END:VCALENDAR
    // we need to unwrap it.

    VEvent *sub_ic = dynamic_cast<VEvent *>(ic.get_first_component(ICAL_VEVENT_COMPONENT));
    if (sub_ic != NULL) {
        int_is("Getting VEvent subcomponent",
               sub_ic->isa(),
               ICAL_VEVENT_COMPONENT);
        while (sub_ic != NULL) {
            if (VERBOSE != 0) {
                printf("subcomponent: %s\n", sub_ic->as_ical_string().c_str());
            }
            delete sub_ic;
            sub_ic = dynamic_cast<VEvent *>(ic.get_next_component(ICAL_VEVENT_COMPONENT));
        }
    }

    VCalendar *cal = new VCalendar();
    VAgenda *vAgenda = new VAgenda();

    ok("Create a new VCalendar object", (cal != 0));
    ok("Create a new VAgenda object", (vAgenda != 0));

    ICalProperty *prop = new ICalProperty(ICAL_OWNER_PROPERTY);
    prop->set_owner(string("fred@flintstone.net"));
    vAgenda->add_property(prop);
    delete prop;

    prop = new ICalProperty(ICAL_SUMMARY_PROPERTY);
    prop->set_summary(string("CPMain"));
    vAgenda->add_property(prop);
    delete prop;

    prop = new ICalProperty(ICAL_TZID_PROPERTY);
    prop->set_tzid(string("America/Los_Angeles"));
    vAgenda->add_property(prop);
    delete prop;

    cal->add_component(vAgenda);

    ok("Complex VCALENDAR/VAGENDA", (cal->as_ical_string().c_str() != 0));

    if (VERBOSE != 0) {
        printf("vAgenda: %s\n", cal->as_ical_string().c_str());
    }

    delete vAgenda;
    delete cal;

    int caughtException = 0;
    try {
        VComponent v = VComponent(string("HFHFHFHF"));
    } catch (icalerrorenum err) {
        if (err == ICAL_BADARG_ERROR) {
            caughtException = 1;
        }
    }
    int_is("Testing exception handling", caughtException, 1);
}