diff --git a/src/tests/exportcsvtest.cpp b/src/tests/exportcsvtest.cpp index 006436f..bd27ee2 100644 --- a/src/tests/exportcsvtest.cpp +++ b/src/tests/exportcsvtest.cpp @@ -1,115 +1,119 @@ #include #include #include "taskview.h" #include "model/task.h" #include "export/totalsastext.h" #include "helpers.h" class ExportCSVTest : public QObject { Q_OBJECT +private: + ReportCriteria createRC(ReportCriteria::REPORTTYPE type, bool toClipboard); + private Q_SLOTS: void testTotalsEmpty(); void testTotalsSimpleTree(); void testTimesSimpleTree(); void testHistorySimpleTree(); }; -static ReportCriteria createRC(ReportCriteria::REPORTTYPE type, bool toClipboard) +ReportCriteria ExportCSVTest::createRC(ReportCriteria::REPORTTYPE type, bool toClipboard) { - QTemporaryFile file; - if (!file.open()) { + QTemporaryFile *file = new QTemporaryFile(this); + if (!file->open()) { + delete file; throw std::runtime_error("1"); } ReportCriteria rc; rc.reportType = type; - rc.url = QUrl::fromLocalFile(file.fileName()); + rc.url = QUrl::fromLocalFile(file->fileName()); rc.from = QDate::currentDate(); rc.to = QDate::currentDate(); rc.decimalMinutes = false; rc.sessionTimes = false; rc.allTasks = true; rc.bExPortToClipBoard = toClipboard; rc.delimiter = ";"; rc.quote = "\""; return rc; } void ExportCSVTest::testTotalsEmpty() { QLocale::setDefault(QLocale(QLocale::C)); auto *taskView = new TaskView(); const QString &timeString = QLocale().toString(QDateTime::currentDateTime()); const QString &expected = QStringLiteral( "Task Totals\n%1\n\n" " Time Task\n----------------------------------------------\nNo tasks.").arg(timeString); QCOMPARE( totalsAsText(taskView->tasksModel(), taskView->currentItem(), createRC(ReportCriteria::CSVTotalsExport, true)), expected); } void ExportCSVTest::testTotalsSimpleTree() { QLocale::setDefault(QLocale(QLocale::C)); - auto *taskView = createTaskView(true); + auto *taskView = createTaskView(this, true); const QString &timeString = QLocale().toString(QDateTime::currentDateTime()); const QString &expected = QStringLiteral( "Task Totals\n" "%1\n" "\n" " Time Task\n" "----------------------------------------------\n" " 0:08 1\n" " 0:03 2\n" " 0:07 3\n" "----------------------------------------------\n" " 0:15 Total").arg(timeString); QCOMPARE( totalsAsText(taskView->tasksModel(), taskView->currentItem(), createRC(ReportCriteria::CSVTotalsExport, true)), expected); } void ExportCSVTest::testTimesSimpleTree() { QLocale::setDefault(QLocale(QLocale::C)); - auto *taskView = createTaskView(true); + auto *taskView = createTaskView(this, true); const auto &rc = createRC(ReportCriteria::CSVTotalsExport, false); QCOMPARE(taskView->report(rc), ""); const QString &expected = QStringLiteral( "\"3\";;0:07;0:07;0:07;0:07\n" "\"1\";;0:05;0:05;0:08;0:08\n" ";\"2\";0:03;0:03;0:03;0:03\n"); QCOMPARE(readTextFile(rc.url.path()), expected); } void ExportCSVTest::testHistorySimpleTree() { QLocale::setDefault(QLocale(QLocale::C)); - auto *taskView = createTaskView(true); + auto *taskView = createTaskView(this, true); const auto &rc = createRC(ReportCriteria::CSVHistoryExport, false); QCOMPARE(taskView->report(rc), ""); const QString &expected = QStringLiteral( "\"Task name\";%1\n" "\"1\";0:05\n" "\"1->2\";0:03\n" "\"3\";0:07\n").arg(QDate::currentDate().toString()); QCOMPARE(readTextFile(rc.url.path()), expected); } QTEST_MAIN(ExportCSVTest) #include "exportcsvtest.moc" diff --git a/src/tests/helpers.cpp b/src/tests/helpers.cpp index b5e4559..cbf8ce5 100644 --- a/src/tests/helpers.cpp +++ b/src/tests/helpers.cpp @@ -1,43 +1,43 @@ #include "helpers.h" #include #include #include "taskview.h" #include "model/task.h" -TaskView *createTaskView(bool simpleTree) +TaskView *createTaskView(QObject *parent, bool simpleTree) { auto *taskView = new TaskView(); - // TODO: remove all temporary files after running test - QTemporaryFile icsFile; - if (!icsFile.open()) { + QTemporaryFile *icsFile = new QTemporaryFile(parent); + if (!icsFile->open()) { delete taskView; + delete icsFile; return nullptr; } - taskView->storage()->load(taskView, QUrl::fromLocalFile(icsFile.fileName())); + taskView->storage()->load(taskView, QUrl::fromLocalFile(icsFile->fileName())); if (simpleTree) { Task* task1 = taskView->task(taskView->addTask("1")); Task* task2 = taskView->task(taskView->addTask("2", QString(), 0, 0, QVector(0, 0), task1)); Task* task3 = taskView->task(taskView->addTask("3")); task1->changeTime(5, taskView->storage()); // add 5 minutes task2->changeTime(3, taskView->storage()); // add 3 minutes task3->changeTime(7, taskView->storage()); // add 7 minutes } return taskView; } QString readTextFile(const QString &path) { QFile file(path); if (!file.open(QFile::ReadOnly | QFile::Text)) { qFatal(QStringLiteral("failed to open file: %1").arg(path).toStdString().c_str()); } QTextStream in(&file); return in.readAll(); } diff --git a/src/tests/helpers.h b/src/tests/helpers.h index edccf36..4f3e1ed 100644 --- a/src/tests/helpers.h +++ b/src/tests/helpers.h @@ -1,11 +1,12 @@ #ifndef KTIMETRACKER_HELPERS_H #define KTIMETRACKER_HELPERS_H +#include #include class TaskView; -TaskView *createTaskView(bool simpleTree); +TaskView *createTaskView(QObject *parent, bool simpleTree); QString readTextFile(const QString &path); #endif // KTIMETRACKER_HELPERS_H diff --git a/src/tests/plannerparsertest.cpp b/src/tests/plannerparsertest.cpp index 029fd4b..24035f8 100644 --- a/src/tests/plannerparsertest.cpp +++ b/src/tests/plannerparsertest.cpp @@ -1,83 +1,83 @@ #include #include "taskview.h" #include "model/task.h" #include "helpers.h" class PlannerParserTest : public QObject { Q_OBJECT private Q_SLOTS: void testEmpty(); void testAtTopLevel(); void testAtSubTask(); }; void PlannerParserTest::testEmpty() { auto *taskView = new TaskView(); taskView->importPlanner(QFINDTESTDATA("data/kitchen.planner")); auto *model = taskView->tasksModel(); QCOMPARE(model->getAllItems().size(), 21); QCOMPARE(model->topLevelItemCount(), 4); QCOMPARE(dynamic_cast(model->topLevelItem(0))->name(), "Initiation"); auto *closureTask = dynamic_cast(model->topLevelItem(3)); QCOMPARE(closureTask->name(), "Closure"); QCOMPARE(closureTask->childCount(), 3); } void PlannerParserTest::testAtTopLevel() { - auto *taskView = createTaskView(false); + auto *taskView = createTaskView(this, false); Task* task1 = taskView->task(taskView->addTask("1")); QVERIFY(task1); taskView->setCurrentIndex(taskView->tasksModel()->index(task1, 0)); taskView->importPlanner(QFINDTESTDATA("data/kitchen.planner")); auto *model = taskView->tasksModel(); QCOMPARE(model->getAllItems().size(), 22); QCOMPARE(model->topLevelItemCount(), 5); QCOMPARE(dynamic_cast(model->topLevelItem(0))->name(), "1"); QCOMPARE(dynamic_cast(model->topLevelItem(1))->name(), "Initiation"); auto *closureTask = dynamic_cast(model->topLevelItem(4)); QCOMPARE(closureTask->name(), "Closure"); QCOMPARE(closureTask->childCount(), 3); } void PlannerParserTest::testAtSubTask() { - auto *taskView = createTaskView(false); + auto *taskView = createTaskView(this, false); Task* task1 = taskView->task(taskView->addTask("1")); QVERIFY(task1); Task* task2 = taskView->task(taskView->addTask("2", QString(), 0, 0, QVector(0, 0), task1)); QVERIFY(task2); taskView->setCurrentIndex(taskView->tasksModel()->index(task2, 0)); taskView->importPlanner(QFINDTESTDATA("data/kitchen.planner")); auto *model = taskView->tasksModel(); QCOMPARE(model->getAllItems().size(), 23); QCOMPARE(model->topLevelItemCount(), 1); QCOMPARE(task1->childCount(), 5); QCOMPARE(dynamic_cast(task1->child(0))->name(), "2"); QCOMPARE(dynamic_cast(task1->child(1))->name(), "Initiation"); auto *closureTask = dynamic_cast(task1->child(4)); QCOMPARE(closureTask->name(), "Closure"); QCOMPARE(closureTask->childCount(), 3); } QTEST_MAIN(PlannerParserTest) #include "plannerparsertest.moc" diff --git a/src/tests/storagetest.cpp b/src/tests/storagetest.cpp index 441d15f..6c14e8a 100644 --- a/src/tests/storagetest.cpp +++ b/src/tests/storagetest.cpp @@ -1,70 +1,70 @@ #include #include #include #include #include "taskview.h" #include "model/task.h" #include "helpers.h" class StorageTest : public QObject { Q_OBJECT private Q_SLOTS: void testSaveEmpty(); void testSaveSimpleTree(); }; void StorageTest::testSaveEmpty() { - auto *taskView = createTaskView(false); + auto *taskView = createTaskView(this, false); QCOMPARE(taskView->storage()->save(taskView), ""); QCOMPARE(readTextFile(taskView->storage()->fileUrl().path()), readTextFile(QFINDTESTDATA("data/empty.ics"))); } void StorageTest::testSaveSimpleTree() { - auto *taskView = createTaskView(true); + auto *taskView = createTaskView(this, true); Task* negativeTask = taskView->task(taskView->addTask("negative duration")); negativeTask->changeTime(-5, taskView->storage()); // substract 5 minutes QCOMPARE(taskView->storage()->save(taskView), ""); KCalCore::MemoryCalendar::Ptr calendar(new KCalCore::MemoryCalendar(QTimeZone::systemTimeZone())); KCalCore::FileStorage fileStorage(calendar, taskView->storage()->fileUrl().path(), new KCalCore::ICalFormat()); QVERIFY(fileStorage.load()); auto todos = calendar->rawTodos(KCalCore::TodoSortSummary); QCOMPARE(todos.size(), 4); QCOMPARE(todos[0]->summary(), "1"); QCOMPARE(todos[0]->customProperty("ktimetracker", "totalSessionTime"), "5"); QCOMPARE(todos[0]->customProperty("ktimetracker", "totalTaskTime"), "5"); QCOMPARE(todos[1]->summary(), "2"); QCOMPARE(todos[1]->relatedTo(), todos[0]->uid()); QCOMPARE(todos[3]->summary(), "negative duration"); QCOMPARE(todos[3]->customProperty("ktimetracker", "totalSessionTime"), "-5"); QCOMPARE(todos[3]->customProperty("ktimetracker", "totalTaskTime"), "-5"); auto events = calendar->rawEvents(KCalCore::EventSortSummary); QCOMPARE(events.size(), 4); QCOMPARE(events[0]->summary(), "1"); QCOMPARE(events[0]->customProperty("ktimetracker", "duration"), "300"); QCOMPARE(events[0]->relatedTo(), todos[0]->uid()); QCOMPARE(events[0]->categoriesStr(), "KTimeTracker"); QCOMPARE(events[3]->summary(), "negative duration"); QCOMPARE(events[3]->customProperty("ktimetracker", "duration"), "-300"); QCOMPARE(events[3]->relatedTo(), todos[3]->uid()); } QTEST_MAIN(StorageTest) #include "storagetest.moc" diff --git a/src/tests/tasktest.cpp b/src/tests/tasktest.cpp index ee1ac41..3f9e295 100644 --- a/src/tests/tasktest.cpp +++ b/src/tests/tasktest.cpp @@ -1,30 +1,30 @@ #include #include "taskview.h" #include "model/task.h" #include "helpers.h" class TaskTest : public QObject { Q_OBJECT private Q_SLOTS: void testIsRoot(); }; void TaskTest::testIsRoot() { - auto *taskView = createTaskView(false); + auto *taskView = createTaskView(this, false); auto *task1 = taskView->task(taskView->addTask("1")); QVERIFY(task1); QVERIFY(task1->isRoot()); auto *task2 = taskView->task(taskView->addTask("2", QString(), 0, 0, QVector(0, 0), task1)); QVERIFY(task2); QVERIFY(!task2->isRoot()); } QTEST_MAIN(TaskTest) #include "tasktest.moc"