1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#include <cxxtest/TestSuite.h>
#include "common/formats/ini-file.h"
#include "common/memstream.h"
class IniFileTestSuite : public CxxTest::TestSuite {
public:
void test_blank_ini_file() {
Common::INIFile inifile;
TS_ASSERT(!inifile.hasSection("abc"));
Common::INIFile::SectionList sections = inifile.getSections();
TS_ASSERT_EQUALS(sections.size(), 0U);
}
void test_simple_ini_file() {
static const unsigned char inistr[] = "#comment\n[s]\nabc=1\ndef=xyz";
Common::MemoryReadStream ms(inistr, sizeof(inistr));
Common::INIFile inifile;
bool result = inifile.loadFromStream(ms);
TS_ASSERT(result);
Common::INIFile::SectionList sections = inifile.getSections();
TS_ASSERT_EQUALS(sections.size(), 1U);
TS_ASSERT(inifile.hasSection("s"));
TS_ASSERT(inifile.hasKey("abc", "s"));
Common::String val;
TS_ASSERT(inifile.getKey("abc", "s", val));
TS_ASSERT_EQUALS(val, "1");
TS_ASSERT(inifile.getKey("def", "s", val));
TS_ASSERT_EQUALS(val, "xyz");
inifile.setKey("abc", "s", "newval");
TS_ASSERT(inifile.getKey("abc", "s", val));
TS_ASSERT_EQUALS(val, "newval");
}
void test_multisection_ini_file() {
static const unsigned char inistr[] = "[s]\nabc=1\ndef=xyz\n#comment=no\n[empty]\n\n[s2]\n abc = 2 \n ; comment=no";
Common::MemoryReadStream ms(inistr, sizeof(inistr));
Common::INIFile inifile;
bool result = inifile.loadFromStream(ms);
TS_ASSERT(result);
Common::INIFile::SectionList sections = inifile.getSections();
TS_ASSERT_EQUALS(sections.size(), 3U);
TS_ASSERT(inifile.hasSection("s"));
TS_ASSERT(inifile.hasSection("empty"));
TS_ASSERT(inifile.hasSection("s2"));
TS_ASSERT(inifile.hasKey("abc", "s"));
TS_ASSERT(inifile.hasKey("abc", "s2"));
Common::String val;
TS_ASSERT(inifile.getKey("abc", "s", val));
TS_ASSERT_EQUALS(val, "1");
TS_ASSERT(inifile.getKey("abc", "s2", val));
TS_ASSERT_EQUALS(val, "2");
inifile.clear();
sections = inifile.getSections();
TS_ASSERT_EQUALS(sections.size(), 0U);
TS_ASSERT(!inifile.hasSection("s"));
}
void test_modify_ini_file() {
Common::INIFile inifile;
TS_ASSERT(!inifile.hasSection("s"));
inifile.addSection("s");
TS_ASSERT(inifile.hasSection("s"));
inifile.setKey("k", "s", "val");
TS_ASSERT(inifile.hasKey("k", "s"));
inifile.setKey("k2", "s", "val2");
TS_ASSERT(inifile.hasKey("k2", "s"));
inifile.removeKey("k2", "s");
TS_ASSERT(!inifile.hasKey("k2", "s"));
inifile.renameSection("s", "t");
TS_ASSERT(!inifile.hasSection("s"));
TS_ASSERT(inifile.hasSection("t"));
TS_ASSERT(inifile.hasKey("k", "t"));
inifile.removeSection("t");
TS_ASSERT(!inifile.hasSection("t"));
}
void test_name_validity() {
Common::INIFile inifile;
inifile.addSection("s*");
TS_ASSERT(!inifile.hasSection("s*"));
inifile.addSection("");
TS_ASSERT(!inifile.hasSection(""));
// Valid is alphanum plus [-_:. ]
inifile.addSection("sEcT10N -_..Name:");
TS_ASSERT(inifile.hasSection("sEcT10N -_..Name:"));
const char invalids[] = "!\"#$%&'()=~[]()+?<>\r\t\n";
for (uint i = 0; i < sizeof(invalids) - 1; i++) {
char c = invalids[i];
const Common::String s(c);
inifile.addSection(s);
TS_ASSERT(!inifile.hasSection(s));
}
inifile.clear();
inifile.allowNonEnglishCharacters();
for (uint i = 0; i < sizeof(invalids) - 1; i++) {
char c = invalids[i];
if (c == '[' || c == ']' || c == '#' || c == '=' || c == '\r' || c == '\n')
continue;
const Common::String s(c);
inifile.addSection(s);
TS_ASSERT(inifile.hasSection(s));
}
}
void test_write_simple_ini_file() {
byte buf[1024];
Common::INIFile inifile;
{
static const unsigned char inistr[] = "#comment\n[s]\nabc=1\ndef=xyz";
Common::MemoryReadStream mrs(inistr, sizeof(inistr));
TS_ASSERT(inifile.loadFromStream(mrs));
}
// A too-small write buffer (should fail)
{
Common::MemoryWriteStream mws(buf, 10);
TS_ASSERT(!inifile.saveToStream(mws));
}
// A good sized write buffer (should work)
int len;
{
Common::MemoryWriteStream mws(buf, 1024);
TS_ASSERT(inifile.saveToStream(mws));
len = mws.pos();
}
{
Common::MemoryReadStream mrs(buf, len - 1);
Common::INIFile checkinifile;
TS_ASSERT(checkinifile.loadFromStream(mrs));
TS_ASSERT(checkinifile.hasSection("s"));
const Common::INIFile::SectionList §ions = checkinifile.getSections();
const Common::INIFile::Section §ion = sections.front();
TS_ASSERT_EQUALS(section.comment, "#comment\n");
TS_ASSERT_EQUALS(section.name, "s");
TS_ASSERT(checkinifile.hasKey("abc", "s"));
TS_ASSERT(checkinifile.hasKey("def", "s"));
Common::String val;
TS_ASSERT(inifile.getKey("abc", "s", val));
TS_ASSERT_EQUALS(val, "1");
TS_ASSERT(inifile.getKey("def", "s", val));
TS_ASSERT_EQUALS(val, "xyz");
}
}
};
|