Fixed tests

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-06-15 14:25:58 +03:00
parent 535fb2c4d6
commit 1b42b9a08e
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
2 changed files with 23 additions and 22 deletions

View File

@ -98,7 +98,7 @@ QString unescape(QString orig)
return out; return out;
} }
QString unquete(QString str) QString unquote(QString str)
{ {
if ((str.contains(QChar(';')) || str.contains(QChar('=')) || str.contains(QChar(','))) && str.endsWith("\"") && str.startsWith("\"")) { if ((str.contains(QChar(';')) || str.contains(QChar('=')) || str.contains(QChar(','))) && str.endsWith("\"") && str.startsWith("\"")) {
#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
@ -138,7 +138,7 @@ bool parseOldFileFormat(QIODevice& device, QSettings::SettingsMap& map)
QString key = line.left(eqPos).trimmed(); QString key = line.left(eqPos).trimmed();
QString valueStr = line.right(line.length() - eqPos - 1).trimmed(); QString valueStr = line.right(line.length() - eqPos - 1).trimmed();
valueStr = unquete(unescape(valueStr)); valueStr = unquote(unescape(valueStr));
QVariant value(valueStr); QVariant value(valueStr);
map.insert(key, value); map.insert(key, value);
@ -174,7 +174,7 @@ bool INIFile::loadFile(QString fileName)
if (auto valueStr = _settings_obj.value(key).toString(); if (auto valueStr = _settings_obj.value(key).toString();
(valueStr.contains(QChar(';')) || valueStr.contains(QChar('=')) || valueStr.contains(QChar(','))) && (valueStr.contains(QChar(';')) || valueStr.contains(QChar('=')) || valueStr.contains(QChar(','))) &&
valueStr.endsWith("\"") && valueStr.startsWith("\"")) { valueStr.endsWith("\"") && valueStr.startsWith("\"")) {
insert(key, unquete(valueStr)); insert(key, unquote(valueStr));
} else } else
insert(key, _settings_obj.value(key)); insert(key, _settings_obj.value(key));
} }

View File

@ -3,6 +3,7 @@
#include <settings/INIFile.h> #include <settings/INIFile.h>
#include <QList> #include <QList>
#include <QSettings> #include <QSettings>
#include <QTemporaryFile>
#include <QVariant> #include <QVariant>
#include "FileSystem.h" #include "FileSystem.h"
@ -74,9 +75,8 @@ class IniFileTest : public QObject {
QCOMPARE(out_list_numbers, list_numbers); QCOMPARE(out_list_numbers, list_numbers);
} }
void test_SaveAleardyExistingFile() void test_SaveAlreadyExistingFile()
{ {
QString fileName = "test_SaveAleardyExistingFile.ini";
QString fileContent = R"(InstanceType=OneSix QString fileContent = R"(InstanceType=OneSix
iconKey=vanillia_icon iconKey=vanillia_icon
name=Minecraft Vanillia name=Minecraft Vanillia
@ -86,9 +86,10 @@ Wrapperommand=)";
fileContent += "\""; fileContent += "\"";
fileContent += +R"(\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =)"; fileContent += +R"(\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =)";
fileContent += "\"\n"; fileContent += "\"\n";
QFile file(fileName); QTemporaryFile file;
if (file.open(QFile::WriteOnly | QFile::Text)) { // QFile file(fileName);
if (file.open()) {
QTextStream stream(&file); QTextStream stream(&file);
stream << fileContent; stream << fileContent;
file.close(); file.close();
@ -96,22 +97,23 @@ Wrapperommand=)";
// load // load
INIFile f1; INIFile f1;
f1.loadFile(fileName); f1.loadFile(file.fileName());
QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link"); 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 ="); QCOMPARE(f1.get("Wrapperommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =");
f1.saveFile(fileName); f1.saveFile(file.fileName());
INIFile f2; INIFile f2;
f2.loadFile(fileName); f2.loadFile(file.fileName());
QCOMPARE(f2.get("PreLaunchCommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link"); 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("Wrapperommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =");
QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.2"); QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.2");
} }
void test_SaveAleardyExistingFileWithSpecialChars() void test_SaveAlreadyExistingFileWithSpecialChars()
{ {
QString fileName = "test_SaveAleardyExistingFileWithSpecialChars.ini"; QTemporaryFile file;
FS::deletePath(fileName); // just to clean the previous test run file.open();
QSettings settings{ fileName, QSettings::Format::IniFormat }; file.close();
QSettings settings{ file.fileName(), QSettings::Format::IniFormat };
settings.setFallbacksEnabled(false); settings.setFallbacksEnabled(false);
settings.setValue("simple", "value1"); settings.setValue("simple", "value1");
@ -127,20 +129,20 @@ Wrapperommand=)";
// load // load
INIFile f1; INIFile f1;
f1.loadFile(fileName); f1.loadFile(file.fileName());
for (auto key : settings.allKeys()) for (auto key : settings.allKeys())
QCOMPARE(f1.get(key, "NOT SET").toString(), settings.value(key).toString()); QCOMPARE(f1.get(key, "NOT SET").toString(), settings.value(key).toString());
f1.saveFile(fileName); f1.saveFile(file.fileName());
INIFile f2; INIFile f2;
f2.loadFile(fileName); f2.loadFile(file.fileName());
for (auto key : settings.allKeys()) for (auto key : settings.allKeys())
QCOMPARE(f2.get(key, "NOT SET").toString(), settings.value(key).toString()); QCOMPARE(f2.get(key, "NOT SET").toString(), settings.value(key).toString());
QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.2"); QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.2");
} }
void test_SaveAleardyExistingFileWithSpecialCharsV1() void test_SaveAlreadyExistingFileWithSpecialCharsV1()
{ {
QString fileName = "test_SaveAleardyExistingFileWithSpecialCharsV1.ini"; QTemporaryFile file;
QString fileContent = R"(InstanceType=OneSix QString fileContent = R"(InstanceType=OneSix
ConfigVersion=1.1 ConfigVersion=1.1
iconKey=vanillia_icon iconKey=vanillia_icon
@ -148,9 +150,8 @@ name=Minecraft Vanillia
OverrideCommands=true OverrideCommands=true
PreLaunchCommand=)"; PreLaunchCommand=)";
fileContent += "\"\\\"env mesa=true\\\"\"\n"; fileContent += "\"\\\"env mesa=true\\\"\"\n";
QFile file(fileName);
if (file.open(QFile::WriteOnly | QFile::Text)) { if (file.open()) {
QTextStream stream(&file); QTextStream stream(&file);
stream << fileContent; stream << fileContent;
file.close(); file.close();
@ -158,7 +159,7 @@ PreLaunchCommand=)";
// load // load
INIFile f1; INIFile f1;
f1.loadFile(fileName); f1.loadFile(file.fileName());
QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "env mesa=true"); QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "env mesa=true");
QCOMPARE(f1.get("ConfigVersion", "NOT SET").toString(), "1.2"); QCOMPARE(f1.get("ConfigVersion", "NOT SET").toString(), "1.2");
} }