PrismLauncher/tests/INIFile_test.cpp

171 lines
5.5 KiB
C++
Raw Normal View History

#include <QTest>
#include <settings/INIFile.h>
#include <QList>
#include <QSettings>
#include <QTemporaryFile>
#include <QVariant>
#include "FileSystem.h"
#include <QVariantUtils.h>
class IniFileTest : public QObject {
2018-07-15 13:51:05 +01:00
Q_OBJECT
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\"";
QTest::newRow("Hashtags") << "some data#something";
2018-07-15 13:51:05 +01:00
}
void test_SaveLoad()
{
QString a = "a";
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);
QCOMPARE(f2.get("a", "NOT SET").toString(), a);
QCOMPARE(f2.get("b", "NOT SET").toString(), b);
}
void test_SaveLoadLists()
{
QString slist_strings = "(\"a\",\"b\",\"c\")";
QStringList list_strings = { "a", "b", "c" };
QString slist_numbers = "(1,2,3,10)";
QList<int> list_numbers = { 1, 2, 3, 10 };
QString filename = "test_SaveLoadLists.ini";
INIFile f;
f.set("list_strings", list_strings);
f.set("list_numbers", QVariantUtils::fromList(list_numbers));
f.saveFile(filename);
// load
INIFile f2;
f2.loadFile(filename);
QStringList out_list_strings = f2.get("list_strings", QStringList()).toStringList();
qDebug() << "OutStringList" << out_list_strings;
QList<int> out_list_numbers = QVariantUtils::toList<int>(f2.get("list_numbers", QVariantUtils::fromList(QList<int>())));
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
}
void test_SaveAlreadyExistingFile()
{
QString fileContent = R"(InstanceType=OneSix
iconKey=vanillia_icon
name=Minecraft Vanillia
OverrideCommands=true
PreLaunchCommand="$INST_JAVA" -jar packwiz-installer-bootstrap.jar link
Wrapperommand=)";
fileContent += "\"";
fileContent += +R"(\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =)";
fileContent += "\"\n";
QTemporaryFile file;
// QFile file(fileName);
if (file.open()) {
QTextStream stream(&file);
stream << fileContent;
file.close();
}
// load
INIFile f1;
f1.loadFile(file.fileName());
QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link");
QCOMPARE(f1.get("Wrapperommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =");
f1.saveFile(file.fileName());
INIFile f2;
f2.loadFile(file.fileName());
QCOMPARE(f2.get("PreLaunchCommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link");
QCOMPARE(f2.get("Wrapperommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =");
QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.2");
}
void test_SaveAlreadyExistingFileWithSpecialChars()
{
QTemporaryFile file;
file.open();
file.close();
QSettings settings{ file.fileName(), QSettings::Format::IniFormat };
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;
f1.loadFile(file.fileName());
for (auto key : settings.allKeys())
QCOMPARE(f1.get(key, "NOT SET").toString(), settings.value(key).toString());
f1.saveFile(file.fileName());
INIFile f2;
f2.loadFile(file.fileName());
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");
}
void test_SaveAlreadyExistingFileWithSpecialCharsV1()
{
QTemporaryFile file;
QString fileContent = R"(InstanceType=OneSix
ConfigVersion=1.1
iconKey=vanillia_icon
name=Minecraft Vanillia
OverrideCommands=true
PreLaunchCommand=)";
fileContent += "\"\\\"env mesa=true\\\"\"\n";
if (file.open()) {
QTextStream stream(&file);
stream << fileContent;
file.close();
}
// load
INIFile f1;
f1.loadFile(file.fileName());
QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "env mesa=true");
QCOMPARE(f1.get("ConfigVersion", "NOT SET").toString(), "1.2");
}
};
QTEST_GUILESS_MAIN(IniFileTest)
#include "INIFile_test.moc"