summaryrefslogtreecommitdiffstats
path: root/kplato/tests/CalendarTester.cpp
blob: bf0f299f53ff88a170198ecbca642580895f69f3 (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
/* This file is part of the KDE project
   Copyright (C) 2006 Dag Andersen <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation;
   version 2 of the License.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#include "CalendarTester.h"
#include <kptcalendar.h>
#include <kptdatetime.h>
#include <kptduration.h>
#include <kptmap.h>
#include <kunittest/runner.h>
#include <kunittest/module.h>
#include <tqstring.h>

using namespace KUnitTest;

KUNITTEST_MODULE(kunittest_CalendarTester, "Calendar Tester");
KUNITTEST_MODULE_REGISTER_TESTER(CalendarTester);

void CalendarTester::allTests() {
    testSingleDay();
    testWeekdays();
    testCalendarWithParent();
}

void CalendarTester::testSingleDay() {
    KPlato::Calendar t("Test");
    TQDate wdate(2006,1,2);
    KPlato::DateTime before = KPlato::DateTime(wdate.addDays(-1));
    KPlato::DateTime after = KPlato::DateTime(wdate.addDays(1));
    TQTime t1(8,0,0);
    TQTime t2(10,0,0);
    KPlato::DateTime wdt1(wdate, t1);
    KPlato::DateTime wdt2(wdate, t2);
    KPlato::CalendarDay *day = new KPlato::CalendarDay(TQDate(2006,1,2), KPlato::Map::Working);
    day->addInterval(TQPair<TQTime, TQTime>(t1, t2));
    VERIFY(t.addDay(day));
    COMPARE(t.findDay(wdate), day);
    VERIFY((t.firstAvailableAfter(after, after.addDays(10))).isValid() == false);
    VERIFY((t.firstAvailableBefore(before, before.addDays(-10))).isValid() == false);
    
    COMPARE(t.firstAvailableAfter(before,after).toString(), wdt1.toString());
    COMPARE(t.firstAvailableBefore(after, before).toString(), wdt2.toString());
    
    VERIFY(t.hasInterval(before, after));
    VERIFY(t.hasInterval(after, before) == false);
    
    VERIFY(t.hasInterval(after, after.addDays(1)) == false);
    VERIFY(t.hasInterval(before, before.addDays(-1)) == false);
    
    KPlato::Duration e(0, 2, 0);
    COMPARE((t.effort(before, after)).toString(), e.toString());
    
}

void CalendarTester::testWeekdays() {
    KPlato::Calendar t("Test");
    TQDate wdate(2006,1,4); // wednesday
    KPlato::DateTime before = KPlato::DateTime(wdate.addDays(-2));
    KPlato::DateTime after = KPlato::DateTime(wdate.addDays(2));
    TQTime t1(8,0,0);
    TQTime t2(10,0,0);
    
    KPlato::CalendarDay *wd1 = t.weekday(2); // wednesday
    VERIFY(wd1 != 0);
    
    wd1->setState(KPlato::Map::Working);
    wd1->addInterval(TQPair<TQTime, TQTime>(t1, t2));

    COMPARE(t.firstAvailableAfter(before, after).toString(), TQDateTime(TQDate(2006, 1, 4), TQTime(8,0,0)).toString());
    COMPARE((t.firstAvailableBefore(after, before)).toString(), TQDateTime(TQDate(2006, 1, 4), TQTime(10,0,0)).toString());
    
    COMPARE(t.firstAvailableAfter(after, KPlato::DateTime(TQDate(2006,1,14))).toString(), TQDateTime(TQDate(2006, 1, 11), TQTime(8,0,0)).toString());
    COMPARE(t.firstAvailableBefore(before, KPlato::DateTime(TQDate(2005,12,25))).toString(), TQDateTime(TQDate(2005, 12, 28), TQTime(10,0,0)).toString());
    
}

void CalendarTester::testCalendarWithParent() {
    KPlato::Calendar t("Test 3");
    KPlato::Calendar p("Test 3 parent");
    t.setParent(&p);
    TQDate wdate(2006,1,2);
    KPlato::DateTime before = KPlato::DateTime(wdate.addDays(-1));
    KPlato::DateTime after = KPlato::DateTime(wdate.addDays(1));
    TQTime t1(8,0,0);
    TQTime t2(10,0,0);
    KPlato::DateTime wdt1(wdate, t1);
    KPlato::DateTime wdt2(wdate, t2);
    
    KPlato::CalendarDay *day = new KPlato::CalendarDay(TQDate(2006,1,2), KPlato::Map::Working);
    day->addInterval(TQPair<TQTime, TQTime>(t1, t2));
    COMPARE(p.addDay(day), true);
    COMPARE(p.findDay(wdate), day);
    
    // same tests as in testSingleDay()
    VERIFY((t.firstAvailableAfter(after, after.addDays(10))).isValid() == false);
    VERIFY((t.firstAvailableBefore(before, before.addDays(-10))).isValid() == false);
    
    COMPARE(t.firstAvailableAfter(before,after).toString(), wdt1.toString());
    COMPARE(t.firstAvailableBefore(after, before).toString(), wdt2.toString());
    
    VERIFY(t.hasInterval(before, after));
    VERIFY(t.hasInterval(after, before) == false);
    
    VERIFY(t.hasInterval(after, after.addDays(1)) == false);
    VERIFY(t.hasInterval(before, before.addDays(-1)) == false);
    
    KPlato::Duration e(0, 2, 0);
    COMPARE((t.effort(before, after)).toString(), e.toString());
    
}