File indexing completed on 2024-05-12 17:21:31

0001 /*
0002     Copyright (C) 2015 by Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     This file is part of Kronometer.
0005 
0006     Kronometer is free software: you can redistribute it and/or modify
0007     it under the terms of the GNU General Public License as published by
0008     the Free Software Foundation, either version 2 of the License, or
0009     (at your option) any later version.
0010 
0011     Kronometer is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014     GNU General Public License for more details.
0015 
0016     You should have received a copy of the GNU General Public License
0017     along with Kronometer.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #include "testsession.h"
0021 #include "session.h"
0022 
0023 void TestSession::testDefaultSession()
0024 {
0025     Session session;
0026 
0027     QCOMPARE(session.time(), 0);
0028     QVERIFY(session.isEmpty());
0029     QVERIFY(not session.isOutdated());
0030     QVERIFY(session.laps().isEmpty());
0031     QVERIFY(session.name().isNull());
0032     QVERIFY(session.note().isEmpty());
0033 }
0034 
0035 void TestSession::testTime()
0036 {
0037     int time = 1000;
0038     Session session {time};
0039 
0040     QCOMPARE(session.time(), time);
0041 }
0042 
0043 void TestSession::testName()
0044 {
0045     Session session;
0046     auto test = QLatin1String("test");
0047 
0048     session.setName(test);
0049 
0050     QCOMPARE(session.name(), test);
0051 }
0052 
0053 void TestSession::testNote()
0054 {
0055     Session session;
0056     auto test = QLatin1String("test");
0057 
0058     session.setNote(test);
0059 
0060     QCOMPARE(session.note(), test);
0061 }
0062 
0063 void TestSession::testDate()
0064 {
0065     auto date = QDateTime::currentDateTime();
0066     Session session {0, date};
0067 
0068     QCOMPARE(session.date(), date);
0069 }
0070 
0071 void TestSession::testLaps()
0072 {
0073     Session session;
0074 
0075     const auto laps = QVector<Lap> {Lap {{1, 30}}, Lap {{2, 0}}, Lap {{1, 45}}};
0076     for (const auto& lap : laps) {
0077         session.addLap(lap);
0078     }
0079 
0080     QCOMPARE(session.laps().size(), laps.size());
0081 }
0082 
0083 void TestSession::testEquality()
0084 {
0085     auto date = QDateTime::currentDateTime();
0086 
0087     Session session1 {0, date};
0088     Session session2 {1000, date};
0089 
0090     QCOMPARE(session1, session2);
0091 }
0092 
0093 void TestSession::testInequality()
0094 {
0095     Session session1 {0, QDateTime::currentDateTime()};
0096     QTest::qSleep(100);
0097     Session session2 {0, QDateTime::currentDateTime()};
0098 
0099     QVERIFY(session1 != session2);
0100 }
0101 
0102 void TestSession::testJson()
0103 {
0104     auto date = QDateTime::currentDateTime();
0105     Session session1 {1000, QDateTime::fromString(date.toString(Qt::ISODate), Qt::ISODate)};
0106     session1.setName(QStringLiteral("test-name"));
0107     session1.setNote(QStringLiteral("test-note"));
0108 
0109     const auto laps = QVector<Lap> {Lap {{1, 30}}, Lap {{2, 0}}, Lap {{1, 45}}};
0110     for (const auto& lap : laps) {
0111         session1.addLap(lap);
0112     }
0113 
0114     QJsonObject json;
0115     session1.write(json);
0116 
0117     Session session2 = Session::fromJson(json);
0118 
0119     QCOMPARE(session1, session2);
0120     QCOMPARE(session1.time(), session2.time());
0121     QCOMPARE(session1.name(), session2.name());
0122     QCOMPARE(session1.note(), session2.note());
0123     QCOMPARE(session1.laps().size(), session2.laps().size());
0124 }
0125 
0126 QTEST_MAIN(TestSession)