diff --git a/tests/calendaringtest.cpp b/tests/calendaringtest.cpp index 44440dc..cc65605 100644 --- a/tests/calendaringtest.cpp +++ b/tests/calendaringtest.cpp @@ -1,133 +1,135 @@ /* * Copyright (C) 2012 Christian Mollekopf * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "calendaringtest.h" #include #include #include #include #include #include "testhelpers.h" #include "testutils.h" void compareEvents(const std::vector &list1, const std::vector &list2) { QCOMPARE(list1.size(), list2.size()); for (std::size_t i = 0; i < list1.size(); i++) { const Kolab::Event &e1 = list1.at(i); const Kolab::Event &e2 = list2.at(i); // qDebug() << i; // QCOMPARE(e1.uid(), e2.uid()); QCOMPARE(e1.start(), e2.start()); QCOMPARE(e1.end(), e2.end()); } } void CalendaringTest::initTestCase() { } void CalendaringTest::testCalendaringEvent() { Kolab::Event event; event.setUid("uid"); event.setStart(Kolab::cDateTime(2011,10,10,12,1,1,true)); event.setEnd(Kolab::cDateTime(2011,10,11,12,1,1,true)); Kolab::Calendaring::Event calEvent(event); QCOMPARE(event.start(), calEvent.start()); QCOMPARE(event.uid(), calEvent.uid()); Kolab::Calendaring::Event calEvent2; Kolab::Calendaring::Event calEvent3 = calEvent2; QVERIFY(!calEvent2.uid().empty()); QCOMPARE(calEvent2.uid(), calEvent3.uid()); } void CalendaringTest::delegationTest() { Kolab::Calendaring::Event event; event.setStart(Kolab::cDateTime("Europe/Zurich",2012,5,5,3,4,4)); Kolab::Attendee att1(Kolab::ContactReference("email1", "name1", "uid1")); att1.setCutype(Kolab::CutypeIndividual); Kolab::Attendee att2(Kolab::ContactReference("email2", "name2", "uid2")); Kolab::Attendee att3(Kolab::ContactReference("email3", "name3", "uid3")); Kolab::Attendee att4(Kolab::ContactReference("email4", "name4", "uid4")); std::vector attendees; attendees.push_back(att1); attendees.push_back(att2); attendees.push_back(att3); event.setAttendees(attendees); std::vector delegators; delegators.push_back(att1); delegators.push_back(att2); std::vector delegatees; delegatees.push_back(att3); delegatees.push_back(att4); event.delegate(delegators, delegatees); std::cout << event.write(); //TODO write an actual test } void CalendaringTest::testRecurrence() { //FIXME this is currently broken for floating or utc, //because KCalendarCore::RecurrenceRule::getNextDate implicitly //converts to the timezone of dtstart, which defaults to the local //timezone if none is set. Kolab::Calendaring::Event event; event.setStart(Kolab::cDateTime("Europe/Zurich", 2011,1,1,1,1,1)); event.setEnd(Kolab::cDateTime("Europe/Zurich", 2011,1,1,2,1,1)); Kolab::RecurrenceRule rrule; rrule.setFrequency(Kolab::RecurrenceRule::Daily); rrule.setInterval(1); rrule.setCount(10); event.setRecurrenceRule(rrule); Kolab::cDateTime previousDate = event.start(); for (int i = 0; i < 9; i++) { const Kolab::cDateTime nextDate = event.getNextOccurence(previousDate); - qDebug() << QTest::toString(nextDate); + const char *nextDateString = QTest::toString(nextDate); + qDebug() << nextDateString; + delete[] nextDateString; QCOMPARE(nextDate, Kolab::cDateTime("Europe/Zurich", previousDate.year(), previousDate.month(), previousDate.day()+1, previousDate.hour(), previousDate.minute(), previousDate.second())); const Kolab::cDateTime endDate = event.getOccurenceEndDate(nextDate); // qDebug() << QTest::toString(endDate); QCOMPARE(endDate, Kolab::cDateTime("Europe/Zurich", nextDate.year(), nextDate.month(), nextDate.day(), event.end().hour(), event.end().minute(), event.end().second())); previousDate = nextDate; } Kolab::cDateTime outOfScopeDate = event.getNextOccurence(previousDate); QVERIFY(!outOfScopeDate.isValid()); } void CalendaringTest::testDateTimeUtils() { std::cout << Kolab::DateTimeUtils::getLocalTimezone() << std::endl; } QTEST_MAIN( CalendaringTest ) #include "calendaringtest.moc" diff --git a/tests/testhelpers.h b/tests/testhelpers.h index e09dd3a..8a443de 100644 --- a/tests/testhelpers.h +++ b/tests/testhelpers.h @@ -1,323 +1,336 @@ /* * Copyright (C) 2012 Christian Mollekopf * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #ifndef TESTHELPERS_H #define TESTHELPERS_H // #include #include #include #include #include #include #include #include Q_DECLARE_METATYPE(Kolab::Duration); Q_DECLARE_METATYPE(Kolab::cDateTime); Q_DECLARE_METATYPE(std::vector); Q_DECLARE_METATYPE(Kolab::Event); Q_DECLARE_METATYPE(std::vector); Q_DECLARE_METATYPE(Kolab::Todo); Q_DECLARE_METATYPE(Kolab::Journal); Q_DECLARE_METATYPE(Kolab::Contact); Q_DECLARE_METATYPE(Kolab::Period); Q_DECLARE_METATYPE(std::vector); Q_DECLARE_METATYPE(KCalCore::Event); Q_DECLARE_METATYPE(KCalCore::Todo); Q_DECLARE_METATYPE(KCalCore::Journal); +namespace { + + // Appends a C string to a QByteArray and delete[]s the string argument. + // Returns a reference to the QByteArray. + QByteArray &append(QByteArray &ba, const char *string) + { + ba += QByteArray(string); + delete[] string; + return ba; + } + +} + namespace QTest { template<> char *toString(const Kolab::cDateTime &dt) { QByteArray ba = "Kolab::cDateTime("; ba += QByteArray::number(dt.year()) + ", " + QByteArray::number(dt.month())+ ", " + QByteArray::number(dt.day()) + ", "; ba += QByteArray::number(dt.hour()) + ", " + QByteArray::number(dt.minute()) + ", " + QByteArray::number(dt.second())+ ", "; ba += QByteArray(dt.isUTC()?QByteArray("UTC"):QByteArray("TZ: "+QByteArray::fromStdString(dt.timezone()))); ba += ")"; return qstrdup(ba.data()); } // template<> // char *toString(const QDateTime &dt) // { // QByteArray ba = "QDateTime("; // ba += dt.toString().toAscii(); // ba += dt.timeZone().name().toAscii(); // ba += ")"; // return qstrdup(ba.data()); // } template<> char *toString(const KCalCore::Attendee &at) { QByteArray ba = "Attendee("; ba += at.name().toLatin1() + ", "; ba += at.email().toLatin1() + ", "; ba += QByteArray::number(at.role()) + ", "; ba += QByteArray::number(at.status()) + ", "; ba += QByteArray::number(at.RSVP()) + ", "; ba += at.delegate().toLatin1() + ", "; ba += at.delegator().toLatin1() + ", "; ba += at.uid().toLatin1() + ", "; ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const QList &l) { QByteArray ba = "QList("; foreach(int i, l) { ba += QByteArray::number(i) + ", "; } ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const QList &l) { QByteArray ba = "QList("; foreach(const KCalCore::RecurrenceRule::WDayPos &i, l) { ba += QByteArray::number(i.pos()) + " "; ba += QByteArray::number(i.day()) + ", "; } ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const KCalCore::DateList &l) { QByteArray ba = "KCalCore::DateList("; foreach(const QDate &i, l) { ba += i.toString().toLatin1(); } ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const KCalCore::DateTimeList &l) { QByteArray ba = "KCalCore::DateTimeList("; foreach(const QDateTime &i, l) { - ba += toString(i); + append(ba, toString(i)); } ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const KCalCore::Recurrence &at) { // at.dump(); KCalCore::RecurrenceRule *r = at.defaultRRule(); QByteArray ba; if (!r) { ba += "Recurrence( )"; } else { Q_ASSERT(r); Q_ASSERT(at.rRules().size() == 1); ba += "Recurrence("; ba += QByteArray::number(r->recurrenceType()) + "\n"; ba += QByteArray::number(r->frequency()) + "\n"; ba += QByteArray::number(r->duration()) + "\n"; - ba += QByteArray(toString(r->startDt())) + "\n"; - ba += QByteArray(toString(r->endDt())) + "\n"; - ba += QByteArray(toString(r->bySeconds())) + "\n"; - ba += QByteArray(toString(r->byMinutes())) + "\n"; - ba += QByteArray(toString(r->byHours())) + "\n"; - ba += QByteArray(toString(r->byDays())) + "\n"; - ba += QByteArray(toString(r->byMonthDays())) + "\n"; - ba += QByteArray(toString(r->byYearDays())) + "\n"; - ba += QByteArray(toString(r->byMonths())) + "\n"; + append(ba, toString(r->startDt())) += "\n"; + append(ba, toString(r->endDt())) += "\n"; + append(ba, toString(r->bySeconds())) += "\n"; + append(ba, toString(r->byMinutes())) += "\n"; + append(ba, toString(r->byHours())) += "\n"; + append(ba, toString(r->byDays())) += "\n"; + append(ba, toString(r->byMonthDays())) += "\n"; + append(ba, toString(r->byYearDays())) += "\n"; + append(ba, toString(r->byMonths())) += "\n"; ba += ")\n"; - ba += QByteArray(toString(at.exDates())) + "\n"; - ba += QByteArray(toString(at.exDateTimes())) + "\n"; - ba += QByteArray(toString(at.rDates())) + "\n"; - ba += QByteArray(toString(at.rDateTimes())) + "\n"; + append(ba, toString(at.exDates())) += "\n"; + append(ba, toString(at.exDateTimes())) += "\n"; + append(ba, toString(at.rDates())) += "\n"; + append(ba, toString(at.rDateTimes())) += "\n"; } return qstrdup(ba.data()); } template<> char *toString(const Kolab::RecurrenceRule &at) { QByteArray ba; ba += "KolabRecurrenceRule("; ba += QByteArray::number(at.weekStart()) + "\n"; ba += QByteArray::number(at.frequency()) + "\n"; ba += QByteArray::number(at.interval()) + "\n"; ba += QByteArray::number(at.count()) + "\n"; - ba += QByteArray(toString(at.end())) + "\n"; - ba += QByteArray(toString(at.bysecond())) + "\n"; - ba += QByteArray(toString(at.byminute())) + "\n"; - ba += QByteArray(toString(at.byhour())) + "\n"; - ba += QByteArray(toString(at.byday())) + "\n"; - ba += QByteArray(toString(at.bymonthday())) + "\n"; - ba += QByteArray(toString(at.byyearday())) + "\n"; - ba += QByteArray(toString(at.byweekno())) + "\n"; - ba += QByteArray(toString(at.bymonth())) + "\n"; + append(ba, toString(at.end())) += "\n"; + append(ba, toString(at.bysecond())) += "\n"; + append(ba, toString(at.byminute())) += "\n"; + append(ba, toString(at.byhour())) += "\n"; + append(ba, toString(at.byday())) += "\n"; + append(ba, toString(at.bymonthday())) += "\n"; + append(ba, toString(at.byyearday())) += "\n"; + append(ba, toString(at.byweekno())) += "\n"; + append(ba, toString(at.bymonth())) += "\n"; ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const KCalCore::Duration &d) { QByteArray ba; ba += "KCalCore::Duration("; ba += QByteArray::number(d.isDaily()) + ", "; ba += QByteArray::number(d.value()) + " "; ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const Kolab::ContactReference &a) { QByteArray ba = "Kolab::ContactReference("; ba += QByteArray::fromStdString(a.email()) + ", "; ba += QByteArray::fromStdString(a.name()) + ", "; ba += QByteArray::fromStdString(a.uid()); ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const std::vector &v) { QByteArray ba = "vector("; for (std::size_t i = 0; i < v.size(); i++) { - ba += QByteArray(toString(v.at(i)))+ "\n"; + append(ba, toString(v.at(i))) += "\n"; } ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const Kolab::Attendee &a) { QByteArray ba = "Kolab::Attendee("; ba += QByteArray::fromStdString(a.contact().email()) + "\n"; ba += QByteArray::fromStdString(a.contact().name()) + "\n"; ba += QByteArray::number(a.partStat()) + "\n"; ba += QByteArray::number(a.role()) + "\n"; ba += QByteArray::number(a.rsvp()) + "\n"; ba += QByteArray::fromStdString(a.contact().uid())+"\n"; - ba += QByteArray(toString(a.delegatedTo()))+"\n"; - ba += QByteArray(toString(a.delegatedFrom()))+ "\n"; + append(ba, toString(a.delegatedTo())) += "\n"; + append(ba, toString(a.delegatedFrom())) += "\n"; ba += QByteArray::number(a.cutype())+ "\n"; ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const std::vector &v) { QByteArray ba = "vector("; for (std::size_t i = 0; i < v.size(); i++) { - ba += QByteArray(toString(v.at(i)))+ "\n"; + append(ba, toString(v.at(i))) += "\n"; ba += QByteArray("#######################")+ "\n"; } ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const Kolab::CustomProperty &a) { QByteArray ba = "Kolab::CustomProperty("; ba += QByteArray::fromStdString(a.identifier) + ", "; ba += QByteArray::fromStdString(a.value); ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const std::vector &v) { QByteArray ba = "vector("; for (std::size_t i = 0; i < v.size(); i++) { - ba += QByteArray(toString(v.at(i)))+ "\n"; + append(ba, toString(v.at(i))) += "\n"; } ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const Kolab::Period &p) { QByteArray ba = "Kolab::Period("; - ba += QByteArray(toString(p.start))+ "\n"; - ba += QByteArray(toString(p.end))+ "\n"; + append(ba, toString(p.start)) += "\n"; + append(ba, toString(p.end)) += "\n"; ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const std::vector &v) { QByteArray ba = "vector("; for (std::size_t i = 0; i < v.size(); i++) { - ba += QByteArray(toString(v.at(i)))+ "\n"; + append(ba, toString(v.at(i))) += "\n"; } ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const Kolab::FreebusyPeriod &p) { QByteArray ba = "Kolab::FreebusyPeriod("; ba += QByteArray::number(p.type())+ "\n"; ba += QByteArray::fromStdString(p.eventUid())+ "\n"; ba += QByteArray::fromStdString(p.eventLocation())+ "\n"; ba += QByteArray::fromStdString(p.eventSummary())+ "\n"; - ba += QByteArray(toString(p.periods()))+ "\n"; + append(ba, toString(p.periods())) += "\n"; ba += ")"; return qstrdup(ba.data()); } template<> char *toString(const Kolab::Duration &p) { QByteArray ba = "Kolab::Duration"; ba += p.isNegative() ? "-": "+"; ba += "("; ba += QByteArray::number(p.weeks())+ ", "; ba += QByteArray::number(p.days())+ ", "; ba += QByteArray::number(p.hours())+ ", "; ba += QByteArray::number(p.minutes())+ ", "; ba += QByteArray::number(p.seconds()); ba += ")"; return qstrdup(ba.data()); } } #endif