From 07d8598638247d8dfb2fcd2e38c4498e90570f6e Mon Sep 17 00:00:00 2001 From: Trial97 Date: Sun, 27 Aug 2023 23:40:32 +0300 Subject: [PATCH 1/4] added catpacks tests Signed-off-by: Trial97 --- launcher/ui/themes/CatPack.cpp | 10 +++-- launcher/ui/themes/CatPack.h | 9 ++-- tests/CMakeLists.txt | 3 ++ tests/CatPack_test.cpp | 77 ++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 tests/CatPack_test.cpp diff --git a/launcher/ui/themes/CatPack.cpp b/launcher/ui/themes/CatPack.cpp index f0d8ddd55..bbcb58bc8 100644 --- a/launcher/ui/themes/CatPack.cpp +++ b/launcher/ui/themes/CatPack.cpp @@ -99,18 +99,22 @@ QDate ensureDay(int year, int month, int day) QString JsonCatPack::path() { - const QDate now = QDate::currentDate(); + return path(QDate::currentDate()); +} + +QString JsonCatPack::path(QDate now) +{ for (auto var : m_variants) { QDate startDate = ensureDay(now.year(), var.startTime.month, var.startTime.day); QDate endDate = ensureDay(now.year(), var.endTime.month, var.endTime.day); if (startDate > endDate) { // it's spans over multiple years - if (endDate <= now) // end date is in the past so jump one year into the future for endDate + if (endDate < now) // end date is in the past so jump one year into the future for endDate endDate = endDate.addYears(1); else // end date is in the future so jump one year into the past for startDate startDate = startDate.addYears(-1); } - if (startDate >= now && now >= endDate) + if (startDate <= now && now <= endDate) return var.path; } return m_defaultPath; diff --git a/launcher/ui/themes/CatPack.h b/launcher/ui/themes/CatPack.h index fdd117a7f..1d310e796 100644 --- a/launcher/ui/themes/CatPack.h +++ b/launcher/ui/themes/CatPack.h @@ -52,9 +52,9 @@ class BasicCatPack : public CatPack { public: BasicCatPack(QString id, QString name) : m_id(id), m_name(name) {} BasicCatPack(QString id) : BasicCatPack(id, id) {} - virtual QString id() { return m_id; } - virtual QString name() { return m_name; } - virtual QString path(); + virtual QString id() override { return m_id; } + virtual QString name() override { return m_name; } + virtual QString path() override; protected: QString m_id; @@ -83,7 +83,8 @@ class JsonCatPack : public BasicCatPack { PartialDate endTime; }; JsonCatPack(QFileInfo& manifestInfo); - virtual QString path(); + virtual QString path() override; + QString path(QDate now); private: QString m_defaultPath; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a26a49fec..1a49fd700 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -56,3 +56,6 @@ ecm_add_test(Index_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_VERSION_MAJOR}: ecm_add_test(Version_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_VERSION_MAJOR}::Test TEST_NAME Version) + +ecm_add_test(CatPack_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_VERSION_MAJOR}::Test + TEST_NAME CatPack) \ No newline at end of file diff --git a/tests/CatPack_test.cpp b/tests/CatPack_test.cpp new file mode 100644 index 000000000..a84ba6511 --- /dev/null +++ b/tests/CatPack_test.cpp @@ -0,0 +1,77 @@ +#include + +#include +#include +#include +#include +#include "FileSystem.h" +#include "ui/themes/CatPack.h" + +class CatPackTest : public QObject { + Q_OBJECT + private slots: + void test_catPack() + { + QString fileContent = R"({ + "name": "My Cute Cat", + "default": "maxwell.png", + "variants": [ + { + "startTime": { "day": 12, "month": 4 }, + "endTime": { "day": 12, "month": 4 }, + "path": "oneDay.png" + }, + { + "startTime": { "day": 20, "month": 12 }, + "endTime": { "day": 28, "month": 12 }, + "path": "christmas.png" + }, + { + "startTime": { "day": 30, "month": 12 }, + "endTime": { "day": 1, "month": 1 }, + "path": "newyear2.png" + }, + { + "startTime": { "day": 28, "month": 12 }, + "endTime": { "day": 3, "month": 1 }, + "path": "newyear.png" + } + ] +})"; +#if defined(Q_OS_WIN) + QString fileName = "test_SaveAlreadyExistingFile.ini"; + QFile file(fileName); + QCOMPARE(file.open(QFile::WriteOnly | QFile::Text), true); +#else + QTemporaryFile file; + QCOMPARE(file.open(), true); + QCOMPARE(file.fileName().isEmpty(), false); + QString fileName = file.fileName(); +#endif + QTextStream stream(&file); + stream << fileContent; + file.close(); + auto fileinfo = QFileInfo(fileName); + try { + auto cat = JsonCatPack(fileinfo); + QCOMPARE(cat.path(QDate(2023, 4, 12)), FS::PathCombine(fileinfo.path(), "oneDay.png")); + QCOMPARE(cat.path(QDate(2023, 4, 11)), FS::PathCombine(fileinfo.path(), "maxwell.png")); + QCOMPARE(cat.path(QDate(2023, 4, 13)), FS::PathCombine(fileinfo.path(), "maxwell.png")); + QCOMPARE(cat.path(QDate(2023, 12, 21)), FS::PathCombine(fileinfo.path(), "christmas.png")); + QCOMPARE(cat.path(QDate(2023, 12, 28)), FS::PathCombine(fileinfo.path(), "christmas.png")); + QCOMPARE(cat.path(QDate(2023, 12, 29)), FS::PathCombine(fileinfo.path(), "newyear.png")); + QCOMPARE(cat.path(QDate(2023, 12, 30)), FS::PathCombine(fileinfo.path(), "newyear2.png")); + QCOMPARE(cat.path(QDate(2023, 12, 31)), FS::PathCombine(fileinfo.path(), "newyear2.png")); + QCOMPARE(cat.path(QDate(2024, 1, 1)), FS::PathCombine(fileinfo.path(), "newyear2.png")); + QCOMPARE(cat.path(QDate(2024, 1, 2)), FS::PathCombine(fileinfo.path(), "newyear.png")); + QCOMPARE(cat.path(QDate(2024, 1, 3)), FS::PathCombine(fileinfo.path(), "newyear.png")); + QCOMPARE(cat.path(QDate(2024, 1, 4)), FS::PathCombine(fileinfo.path(), "maxwell.png")); + } catch (const Exception& e) { + QFAIL(e.cause().toLatin1()); + } + } +}; + +QTEST_GUILESS_MAIN(CatPackTest) + +#include "CatPack_test.moc" From 311e36b5d6dfaed449a3bbdada3d2e5b87f2b6d9 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Mon, 28 Aug 2023 11:52:26 +0300 Subject: [PATCH 2/4] added new line Signed-off-by: Trial97 --- tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1a49fd700..59e0e3144 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -58,4 +58,4 @@ ecm_add_test(Version_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_VERSION_MAJOR TEST_NAME Version) ecm_add_test(CatPack_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_VERSION_MAJOR}::Test - TEST_NAME CatPack) \ No newline at end of file + TEST_NAME CatPack) From 6cfe2dbc50d0dbe53c59012375fcd0cf3a8558a9 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 31 Aug 2023 17:34:05 +0300 Subject: [PATCH 3/4] moved catpack data to testdata Signed-off-by: Trial97 --- tests/CatPack_test.cpp | 41 ++---------------------------- tests/testdata/CatPacks/index.json | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 39 deletions(-) create mode 100644 tests/testdata/CatPacks/index.json diff --git a/tests/CatPack_test.cpp b/tests/CatPack_test.cpp index a84ba6511..330d1a814 100644 --- a/tests/CatPack_test.cpp +++ b/tests/CatPack_test.cpp @@ -12,45 +12,8 @@ class CatPackTest : public QObject { private slots: void test_catPack() { - QString fileContent = R"({ - "name": "My Cute Cat", - "default": "maxwell.png", - "variants": [ - { - "startTime": { "day": 12, "month": 4 }, - "endTime": { "day": 12, "month": 4 }, - "path": "oneDay.png" - }, - { - "startTime": { "day": 20, "month": 12 }, - "endTime": { "day": 28, "month": 12 }, - "path": "christmas.png" - }, - { - "startTime": { "day": 30, "month": 12 }, - "endTime": { "day": 1, "month": 1 }, - "path": "newyear2.png" - }, - { - "startTime": { "day": 28, "month": 12 }, - "endTime": { "day": 3, "month": 1 }, - "path": "newyear.png" - } - ] -})"; -#if defined(Q_OS_WIN) - QString fileName = "test_SaveAlreadyExistingFile.ini"; - QFile file(fileName); - QCOMPARE(file.open(QFile::WriteOnly | QFile::Text), true); -#else - QTemporaryFile file; - QCOMPARE(file.open(), true); - QCOMPARE(file.fileName().isEmpty(), false); - QString fileName = file.fileName(); -#endif - QTextStream stream(&file); - stream << fileContent; - file.close(); + auto dataDir = QDir(QFINDTESTDATA("testdata/CatPacks")).absolutePath(); + auto fileName = FS::PathCombine(dataDir, "index.json"); auto fileinfo = QFileInfo(fileName); try { auto cat = JsonCatPack(fileinfo); diff --git a/tests/testdata/CatPacks/index.json b/tests/testdata/CatPacks/index.json new file mode 100644 index 000000000..c55813dbb --- /dev/null +++ b/tests/testdata/CatPacks/index.json @@ -0,0 +1,26 @@ +{ + "name": "My Cute Cat", + "default": "maxwell.png", + "variants": [ + { + "startTime": { "day": 12, "month": 4 }, + "endTime": { "day": 12, "month": 4 }, + "path": "oneDay.png" + }, + { + "startTime": { "day": 20, "month": 12 }, + "endTime": { "day": 28, "month": 12 }, + "path": "christmas.png" + }, + { + "startTime": { "day": 30, "month": 12 }, + "endTime": { "day": 1, "month": 1 }, + "path": "newyear2.png" + }, + { + "startTime": { "day": 28, "month": 12 }, + "endTime": { "day": 3, "month": 1 }, + "path": "newyear.png" + } + ] +} From 7e65aea2ef790d8f2e424e618d9ab9acd476f045 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 31 Aug 2023 19:04:41 +0300 Subject: [PATCH 4/4] format json Signed-off-by: Trial97 --- tests/testdata/CatPacks/index.json | 72 ++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/tests/testdata/CatPacks/index.json b/tests/testdata/CatPacks/index.json index c55813dbb..b5401d230 100644 --- a/tests/testdata/CatPacks/index.json +++ b/tests/testdata/CatPacks/index.json @@ -1,26 +1,50 @@ { - "name": "My Cute Cat", - "default": "maxwell.png", - "variants": [ - { - "startTime": { "day": 12, "month": 4 }, - "endTime": { "day": 12, "month": 4 }, - "path": "oneDay.png" - }, - { - "startTime": { "day": 20, "month": 12 }, - "endTime": { "day": 28, "month": 12 }, - "path": "christmas.png" - }, - { - "startTime": { "day": 30, "month": 12 }, - "endTime": { "day": 1, "month": 1 }, - "path": "newyear2.png" - }, - { - "startTime": { "day": 28, "month": 12 }, - "endTime": { "day": 3, "month": 1 }, - "path": "newyear.png" - } - ] + "name": "My Cute Cat", + "default": "maxwell.png", + "variants": [ + { + "startTime": { + "day": 12, + "month": 4 + }, + "endTime": { + "day": 12, + "month": 4 + }, + "path": "oneDay.png" + }, + { + "startTime": { + "day": 20, + "month": 12 + }, + "endTime": { + "day": 28, + "month": 12 + }, + "path": "christmas.png" + }, + { + "startTime": { + "day": 30, + "month": 12 + }, + "endTime": { + "day": 1, + "month": 1 + }, + "path": "newyear2.png" + }, + { + "startTime": { + "day": 28, + "month": 12 + }, + "endTime": { + "day": 3, + "month": 1 + }, + "path": "newyear.png" + } + ] }