2014-03-29 20:16:54 +00:00
|
|
|
#include <QTest>
|
|
|
|
|
2023-06-13 19:07:05 +01:00
|
|
|
#include <settings/INIFile.h>
|
2023-02-16 05:01:27 +00:00
|
|
|
#include <QList>
|
2023-06-14 22:27:20 +01:00
|
|
|
#include <QSettings>
|
2023-06-15 12:25:58 +01:00
|
|
|
#include <QTemporaryFile>
|
2023-02-16 05:01:27 +00:00
|
|
|
#include <QVariant>
|
2023-06-14 22:27:20 +01:00
|
|
|
#include "FileSystem.h"
|
2014-03-29 20:16:54 +00:00
|
|
|
|
2023-04-30 02:05:48 +01:00
|
|
|
#include <QVariantUtils.h>
|
2023-02-16 05:01:27 +00:00
|
|
|
|
2023-06-13 19:07:05 +01:00
|
|
|
class IniFileTest : public QObject {
|
2018-07-15 13:51:05 +01:00
|
|
|
Q_OBJECT
|
2023-06-13 19:07:05 +01:00
|
|
|
private slots:
|
|
|
|
void initTestCase() {}
|
|
|
|
void cleanupTestCase() {}
|
2018-07-15 13:51:05 +01:00
|
|
|
|
|
|
|
void test_Escape_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("through");
|
|
|
|
|
|
|
|
QTest::newRow("unix path") << "/abc/def/ghi/jkl";
|
|
|
|
QTest::newRow("windows path") << "C:\\Program files\\terrible\\name\\of something\\";
|
|
|
|
QTest::newRow("Plain text") << "Lorem ipsum dolor sit amet.";
|
|
|
|
QTest::newRow("Escape sequences") << "Lorem\n\t\n\\n\\tAAZ\nipsum dolor\n\nsit amet.";
|
|
|
|
QTest::newRow("Escape sequences 2") << "\"\n\n\"";
|
2019-06-18 12:18:51 +01:00
|
|
|
QTest::newRow("Hashtags") << "some data#something";
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_SaveLoad()
|
|
|
|
{
|
|
|
|
QString a = "a";
|
2019-06-18 12:18:51 +01:00
|
|
|
QString b = "a\nb\t\n\\\\\\C:\\Program files\\terrible\\name\\of something\\#thisIsNotAComment";
|
2018-07-15 13:51:05 +01:00
|
|
|
QString filename = "test_SaveLoad.ini";
|
|
|
|
|
|
|
|
// save
|
|
|
|
INIFile f;
|
|
|
|
f.set("a", a);
|
|
|
|
f.set("b", b);
|
|
|
|
f.saveFile(filename);
|
|
|
|
|
|
|
|
// load
|
|
|
|
INIFile f2;
|
|
|
|
f2.loadFile(filename);
|
2023-06-13 19:07:05 +01:00
|
|
|
QCOMPARE(f2.get("a", "NOT SET").toString(), a);
|
|
|
|
QCOMPARE(f2.get("b", "NOT SET").toString(), b);
|
2023-02-16 05:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_SaveLoadLists()
|
|
|
|
{
|
2023-04-30 02:05:48 +01:00
|
|
|
QString slist_strings = "(\"a\",\"b\",\"c\")";
|
2023-06-13 19:07:05 +01:00
|
|
|
QStringList list_strings = { "a", "b", "c" };
|
2023-02-16 05:01:27 +00:00
|
|
|
|
2023-04-30 02:05:48 +01:00
|
|
|
QString slist_numbers = "(1,2,3,10)";
|
2023-06-13 19:07:05 +01:00
|
|
|
QList<int> list_numbers = { 1, 2, 3, 10 };
|
2023-02-16 05:01:27 +00:00
|
|
|
|
|
|
|
QString filename = "test_SaveLoadLists.ini";
|
|
|
|
|
|
|
|
INIFile f;
|
2023-04-30 02:05:48 +01:00
|
|
|
f.set("list_strings", list_strings);
|
|
|
|
f.set("list_numbers", QVariantUtils::fromList(list_numbers));
|
2023-02-16 05:01:27 +00:00
|
|
|
f.saveFile(filename);
|
|
|
|
|
|
|
|
// load
|
|
|
|
INIFile f2;
|
|
|
|
f2.loadFile(filename);
|
|
|
|
|
2023-04-30 02:05:48 +01:00
|
|
|
QStringList out_list_strings = f2.get("list_strings", QStringList()).toStringList();
|
2023-02-16 05:01:27 +00:00
|
|
|
qDebug() << "OutStringList" << out_list_strings;
|
2023-06-13 19:07:05 +01:00
|
|
|
|
2023-04-30 02:05:48 +01:00
|
|
|
QList<int> out_list_numbers = QVariantUtils::toList<int>(f2.get("list_numbers", QVariantUtils::fromList(QList<int>())));
|
2023-02-16 05:01:27 +00:00
|
|
|
qDebug() << "OutNumbersList" << out_list_numbers;
|
|
|
|
|
|
|
|
QCOMPARE(out_list_strings, list_strings);
|
|
|
|
QCOMPARE(out_list_numbers, list_numbers);
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2023-06-13 19:07:05 +01:00
|
|
|
|
2023-06-15 12:25:58 +01:00
|
|
|
void test_SaveAlreadyExistingFile()
|
2023-06-13 19:07:05 +01:00
|
|
|
{
|
|
|
|
QString fileContent = R"(InstanceType=OneSix
|
|
|
|
iconKey=vanillia_icon
|
|
|
|
name=Minecraft Vanillia
|
|
|
|
OverrideCommands=true
|
|
|
|
PreLaunchCommand="$INST_JAVA" -jar packwiz-installer-bootstrap.jar link
|
2023-06-14 22:27:20 +01:00
|
|
|
Wrapperommand=)";
|
|
|
|
fileContent += "\"";
|
|
|
|
fileContent += +R"(\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =)";
|
|
|
|
fileContent += "\"\n";
|
2023-06-15 12:25:58 +01:00
|
|
|
QTemporaryFile file;
|
2023-06-13 19:07:05 +01:00
|
|
|
|
2023-06-15 12:25:58 +01:00
|
|
|
// QFile file(fileName);
|
|
|
|
if (file.open()) {
|
2023-06-13 19:07:05 +01:00
|
|
|
QTextStream stream(&file);
|
|
|
|
stream << fileContent;
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// load
|
|
|
|
INIFile f1;
|
2023-06-15 12:25:58 +01:00
|
|
|
f1.loadFile(file.fileName());
|
2023-06-13 19:07:05 +01:00
|
|
|
QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link");
|
2023-06-14 22:27:20 +01:00
|
|
|
QCOMPARE(f1.get("Wrapperommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =");
|
2023-06-15 12:25:58 +01:00
|
|
|
f1.saveFile(file.fileName());
|
2023-06-13 19:07:05 +01:00
|
|
|
INIFile f2;
|
2023-06-15 12:25:58 +01:00
|
|
|
f2.loadFile(file.fileName());
|
2023-06-13 19:07:05 +01:00
|
|
|
QCOMPARE(f2.get("PreLaunchCommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link");
|
2023-06-14 22:27:20 +01:00
|
|
|
QCOMPARE(f2.get("Wrapperommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =");
|
|
|
|
QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.2");
|
|
|
|
}
|
|
|
|
|
2023-06-15 12:25:58 +01:00
|
|
|
void test_SaveAlreadyExistingFileWithSpecialChars()
|
2023-06-14 22:27:20 +01:00
|
|
|
{
|
2023-06-15 12:25:58 +01:00
|
|
|
QTemporaryFile file;
|
|
|
|
file.open();
|
|
|
|
file.close();
|
|
|
|
QSettings settings{ file.fileName(), QSettings::Format::IniFormat };
|
2023-06-14 22:27:20 +01:00
|
|
|
settings.setFallbacksEnabled(false);
|
|
|
|
|
|
|
|
settings.setValue("simple", "value1");
|
|
|
|
settings.setValue("withQuotes", R"("value2" with quotes)");
|
|
|
|
settings.setValue("withSpecialCharacters", "env mesa=true");
|
|
|
|
settings.setValue("withSpecialCharacters2", "1,2,3,4");
|
|
|
|
settings.setValue("withSpecialCharacters2", "1;2;3;4");
|
|
|
|
settings.setValue("withAll", "val=\"$INST_JAVA\" -jar; ls ");
|
|
|
|
|
|
|
|
settings.sync();
|
|
|
|
|
|
|
|
QCOMPARE(settings.status(), QSettings::Status::NoError);
|
|
|
|
|
|
|
|
// load
|
|
|
|
INIFile f1;
|
2023-06-15 12:25:58 +01:00
|
|
|
f1.loadFile(file.fileName());
|
2023-06-14 22:27:20 +01:00
|
|
|
for (auto key : settings.allKeys())
|
|
|
|
QCOMPARE(f1.get(key, "NOT SET").toString(), settings.value(key).toString());
|
2023-06-15 12:25:58 +01:00
|
|
|
f1.saveFile(file.fileName());
|
2023-06-14 22:27:20 +01:00
|
|
|
INIFile f2;
|
2023-06-15 12:25:58 +01:00
|
|
|
f2.loadFile(file.fileName());
|
2023-06-14 22:27:20 +01:00
|
|
|
for (auto key : settings.allKeys())
|
|
|
|
QCOMPARE(f2.get(key, "NOT SET").toString(), settings.value(key).toString());
|
|
|
|
QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.2");
|
|
|
|
}
|
|
|
|
|
2023-06-15 12:25:58 +01:00
|
|
|
void test_SaveAlreadyExistingFileWithSpecialCharsV1()
|
2023-06-14 22:27:20 +01:00
|
|
|
{
|
2023-06-15 12:25:58 +01:00
|
|
|
QTemporaryFile file;
|
2023-06-14 22:27:20 +01:00
|
|
|
QString fileContent = R"(InstanceType=OneSix
|
|
|
|
ConfigVersion=1.1
|
|
|
|
iconKey=vanillia_icon
|
|
|
|
name=Minecraft Vanillia
|
|
|
|
OverrideCommands=true
|
|
|
|
PreLaunchCommand=)";
|
|
|
|
fileContent += "\"\\\"env mesa=true\\\"\"\n";
|
|
|
|
|
2023-06-15 12:25:58 +01:00
|
|
|
if (file.open()) {
|
2023-06-14 22:27:20 +01:00
|
|
|
QTextStream stream(&file);
|
|
|
|
stream << fileContent;
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// load
|
|
|
|
INIFile f1;
|
2023-06-15 12:25:58 +01:00
|
|
|
f1.loadFile(file.fileName());
|
2023-06-14 22:27:20 +01:00
|
|
|
QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "env mesa=true");
|
|
|
|
QCOMPARE(f1.get("ConfigVersion", "NOT SET").toString(), "1.2");
|
2023-06-13 19:07:05 +01:00
|
|
|
}
|
2014-03-29 20:16:54 +00:00
|
|
|
};
|
|
|
|
|
2015-01-31 18:21:47 +00:00
|
|
|
QTEST_GUILESS_MAIN(IniFileTest)
|
2014-03-29 20:16:54 +00:00
|
|
|
|
2016-04-14 00:23:54 +01:00
|
|
|
#include "INIFile_test.moc"
|