1 /*
2  * Stellarium
3  * Copyright (C) 2019 Alexander Wolf
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
18  */
19 
20 #include "tests/testStelIniParser.hpp"
21 
22 #include <QObject>
23 #include <QDebug>
24 #include <QBuffer>
25 #include <QSettings>
26 #include <QRegularExpression>
27 
28 #include "StelIniParser.hpp"
29 #include "StelFileMgr.hpp"
30 
31 QTEST_GUILESS_MAIN(TestStelIniParser);
32 
initTestCase()33 void TestStelIniParser::initTestCase()
34 {
35 	QString workingDir = tempDir.path();
36 	workingDir.replace(QRegularExpression("/+$"), "");  // sometimes the temp path will have / on the end... zap it.
37 	if (!QDir::setCurrent(workingDir))
38 	{
39 		QFAIL(qPrintable("could not set the working directory to: " + workingDir));
40 	}
41 
42 	//qDebug() << "working directory: " << QDir::toNativeSeparators(QDir::currentPath());
43 
44 	tempIniFile = workingDir + "/test.ini";
45 	QFile f(tempIniFile);
46 	if (!f.open(QIODevice::ReadWrite))
47 	{
48 		QFAIL(qPrintable("could not create test file " + tempIniFile));
49 	}
50 	f.close();
51 }
52 
testBase()53 void TestStelIniParser::testBase()
54 {
55 	if (!QFile::exists(tempIniFile))
56 	{
57 		QFAIL(qPrintable("could not open test INI file " + tempIniFile));
58 	}
59 
60 	// 1: write settings
61 	QSettings settingsWrite(tempIniFile, StelIniFormat);
62 	settingsWrite.setValue("some_option", "value");
63 	settingsWrite.setValue("some_flag", true);
64 	settingsWrite.setValue("some_int_number", 10);
65 	settingsWrite.setValue("some_float_number", 10.);
66 	settingsWrite.sync();
67 
68 	// 2: read settings
69 	QSettings settingsRead(tempIniFile, StelIniFormat);
70 	QString someOption = settingsRead.value("some_option", "oops").toString();
71 	bool someFlag = settingsRead.value("some_flag", false).toBool();
72 	int someIntNumber = settingsRead.value("some_int_number", 1).toInt();
73 	double someFloatNumber = settingsRead.value("some_float_number", 1.).toDouble();
74 
75 	// 3: compare settings
76 	QVERIFY(someOption=="value");
77 	QVERIFY(someFlag==true);
78 	QVERIFY(someIntNumber==10);
79 	QVERIFY(qAbs(someFloatNumber-10.)<=1e-6);
80 }
81