Skip to content

Commit 68b1461

Browse files
committed
Add tests for the DateTime input for validity times.
Test the correct behavior of the "Midnight" and setDiff methods. Especially to keep the time when switching from hidden time (midnoght) back to shown time.
1 parent a53e0a9 commit 68b1461

File tree

5 files changed

+66
-8
lines changed

5 files changed

+66
-8
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
list(APPEND srcs PwDialogMock.h importPEM.cpp main.cpp main.h
3-
newKey.cpp pem.cpp export.cpp renewal.cpp)
3+
newKey.cpp pem.cpp export.cpp renewal.cpp validity.cpp)
44

55
list(TRANSFORM srcs PREPEND ${PROJECT_SOURCE_DIR}/test/)
66

test/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class test_main: public QObject
3535
void importPEM();
3636
void exportFormat();
3737
void revoke();
38+
void testValidity();
3839

3940
public:
4041
template <class T> static T *findWindow(const QString &name)

test/validity.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* vi: set sw=4 ts=4:
2+
*
3+
* Copyright (C) 2023 Christian Hohnstaedt.
4+
*
5+
* All rights reserved.
6+
*/
7+
8+
#include <QTest>
9+
10+
#include "widgets/MainWindow.h"
11+
#include "widgets/validity.h"
12+
13+
#include "main.h"
14+
15+
void test_main::testValidity()
16+
{
17+
try {
18+
19+
Validity *start = new Validity(mainwin);
20+
Validity *end = new Validity(mainwin);
21+
22+
QCOMPARE(start->displayFormat(), "yyyy-MM-dd hh:mm 'GMT'");
23+
QCOMPARE(end->displayFormat(), "yyyy-MM-dd hh:mm 'GMT'");
24+
end->setEndDate(true);
25+
start->hideTime(true);
26+
end->hideTime(true);
27+
QCOMPARE(start->displayFormat(), "yyyy-MM-dd 00:00 'GMT'");
28+
QCOMPARE(end->displayFormat(), "yyyy-MM-dd 23:59 'GMT'");
29+
30+
start->setDate(a1time("20130921094317Z"));
31+
end->setDiff(start, 7, 0);
32+
QCOMPARE(start->getDate().toPlain(), "20130921000000Z");
33+
QCOMPARE(end->getDate().toPlain(), "20130927235959Z");
34+
start->hideTime(false);
35+
end->hideTime(false);
36+
QCOMPARE(start->getDate().toPlain(), "20130921094300Z");
37+
QCOMPARE(end->getDate().toPlain(), "20130928094300Z");
38+
start->hideTime(false);
39+
QCOMPARE(start->getDate().toPlain(), "20130921094300Z");
40+
start->hideTime(true);
41+
end->hideTime(true);
42+
QCOMPARE(start->getDate().toPlain(), "20130921000000Z");
43+
QCOMPARE(end->getDate().toPlain(), "20130927235959Z");
44+
45+
end->setDiff(start, 2, 1);
46+
QCOMPARE(end->getDate().toPlain(), "20131120235959Z");
47+
end->hideTime(true);
48+
QCOMPARE(end->getDate().toPlain(), "20131120235959Z");
49+
end->hideTime(false);
50+
QCOMPARE(end->getDate().toPlain(), "20131121094300Z");
51+
52+
} catch (...) {
53+
QVERIFY2(false, "Exception thrown");
54+
}
55+
}

widgets/validity.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ void Validity::localTime(int state)
5757
break;
5858
}
5959
updateFormatString();
60-
setMyTime(time());
6160
}
6261

6362
void Validity::hideTimeCheck(int state)
@@ -112,6 +111,7 @@ void Validity::updateFormatString()
112111
void Validity::setDate(const a1time &a)
113112
{
114113
setDateTime(a);
114+
setMyTime(a.time());
115115
}
116116

117117
void Validity::setDiff(const Validity *start, int number, int range)
@@ -134,12 +134,16 @@ void Validity::setDiff(const Validity *start, int number, int range)
134134

135135
void Validity::setNow()
136136
{
137-
setDateTime(a1time::now());
138-
setMyTime(time());
137+
setDate(a1time());
139138
}
140139

141140
void Validity::setMyTime(const QTime &time)
142141
{
143142
mytime = time;
144143
}
145144

145+
void Validity::setEndDate(bool ed)
146+
{
147+
endDate = ed;
148+
hideTime(midnight);
149+
}

widgets/validity.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ class Validity : public QDateTimeEdit
2828
void setDate(const a1time &a);
2929
void setDiff(const Validity *start, int number, int range);
3030
void hideTime(bool hide);
31-
void setEndDate(bool ed)
32-
{
33-
endDate = ed;
34-
}
31+
void setEndDate(bool ed);
32+
3533
protected slots:
3634
void setMyTime(const QTime & time);
3735

0 commit comments

Comments
 (0)