testini.cpp
1 //------------------------------------------------------------------------------
2 // testini.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "testini.h"
24 #include "ini/ini.h"
25 #include "ini/settingsproviderqt.h"
26 #include <QByteArray>
27 #include <QTemporaryFile>
28 
29 
30 const QByteArray EXAMPLE_INI_FILE = \
31 "\
32 \n\
33 \n\
34 [Section.SectionOne] \n\
35 Key1 = 10 \n\
36 Key2 = Value \n\
37 Key3 = \"A long text value\" \n\
38 ";
39 
40 TestIniFixture::TestIniFixture()
41 {
42  settingsFile.setAutoRemove(true);
43  settingsFile.open();
44  settingsFile.write(EXAMPLE_INI_FILE);
45  settingsFile.flush();
46  settingsQt.reset(new QSettings(settingsFile.fileName(), QSettings::IniFormat));
47  settings.reset(new SettingsProviderQt(settingsQt.data()));
48 }
49 
51 
52 TestReadINI::TestReadINI()
53 : TestUnitBase("Read INI")
54 {
55 }
56 
57 bool TestReadINI::executeTest()
58 {
59  TestIniFixture fixture;
60  Ini ini(fixture.settings.data());
61 
62  // This should disregard characters case.
63  IniSection section = ini.section("section.sectionone");
64  if (section.isNull())
65  {
66  testLog << "Section.SectionOne was not read correctly from the INI file.";
67  return false;
68  }
69 
70  return true;
71 }
72 
74 
75 bool TestReadINIVariable::executeTest()
76 {
77  TestIniFixture fixture;
78  Ini ini(fixture.settings.data());
79 
80  IniVariable variable = ini.retrieveSetting("section.sectionone", "key1");
81 
82  if (variable.isNull())
83  {
84  gLog << "Failed to obtain key.";
85  return false;
86  }
87 
88  int varValue = variable;
89 
90  if (variable.key().compare("Key1", Qt::CaseInsensitive) != 0)
91  {
92  gLog << QString("Key name incorrect, expected 'Key1', got '%1'").arg(variable.key());
93  return false;
94  }
95 
96  if (varValue != 10)
97  {
98  gLog << QString("Value incorrect, expected '10', got '%1'").arg(varValue);
99  return false;
100  }
101 
102  return true;
103 }
104 
106 
107 bool TestDeleteINIVariable::executeTest()
108 {
109  TestIniFixture fixture;
110  Ini ini(fixture.settings.data());
111 
112  if (!ini.hasSetting("section.sectionone", "key1"))
113  {
114  gLog << "Variable doesn't exist already!";
115  return false;
116  }
117 
118  // Another way of removing a variable is to delete it directly from the
119  // Ini file through Ini::deleteSetting(). Here we remove it from the
120  // section. This works the same and is provided for convenience.
121  IniSection section = ini.section("section.sectionone");
122  section.deleteSetting("key1");
123 
124  if (ini.hasSetting("section.sectionone", "key1"))
125  {
126  gLog << "Failed to delete the variable.";
127  return false;
128  }
129 
130  return true;
131 }
132 
134 
135 bool TestDeleteINISection::executeTest()
136 {
137  TestIniFixture fixture;
138  Ini ini(fixture.settings.data());
139 
140  if (!ini.hasSetting("section.sectionone", "key1"))
141  {
142  gLog << "Section doesn't exist already!";
143  return false;
144  }
145 
146  ini.deleteSection("section.sectionone");
147 
148  if (ini.hasSetting("section.sectionone", "key1") ||
149  ini.hasSetting("section.sectionone", "key3"))
150  {
151  gLog << "Failed to delete the section.";
152  return false;
153  }
154 
155  return true;
156 }
INI variable representation.
Definition: inivariable.h:41
void deleteSetting(const QString &name)
Deletes specified variable.
Definition: inisection.cpp:73
Base class for Test Units.
Definition: testbase.h:42
Configuration handler.
Definition: ini.h:69
INI section representation.
Definition: inisection.h:40