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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
#include "../SimpleIni.h"
#include "gtest/gtest.h"
class TestDeletion : public ::testing::Test {
protected:
void SetUp() override;
protected:
CSimpleIniA ini;
};
void TestDeletion::SetUp() {
ini.SetUnicode();
}
// Test Delete entire section
TEST_F(TestDeletion, TestDeleteSection) {
std::string input =
"[section1]\n"
"key1 = value1\n"
"key2 = value2\n"
"\n"
"[section2]\n"
"key3 = value3\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_TRUE(ini.SectionExists("section1"));
bool result = ini.Delete("section1", nullptr);
ASSERT_TRUE(result);
ASSERT_FALSE(ini.SectionExists("section1"));
ASSERT_TRUE(ini.SectionExists("section2"));
// Deleting again should return false
result = ini.Delete("section1", nullptr);
ASSERT_FALSE(result);
}
// Test Delete single key
TEST_F(TestDeletion, TestDeleteKey) {
std::string input =
"[section]\n"
"key1 = value1\n"
"key2 = value2\n"
"key3 = value3\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_TRUE(ini.KeyExists("section", "key2"));
bool result = ini.Delete("section", "key2");
ASSERT_TRUE(result);
ASSERT_FALSE(ini.KeyExists("section", "key2"));
ASSERT_TRUE(ini.KeyExists("section", "key1"));
ASSERT_TRUE(ini.KeyExists("section", "key3"));
// Section should still exist
ASSERT_TRUE(ini.SectionExists("section"));
ASSERT_EQ(ini.GetSectionSize("section"), 2);
}
// Test Delete key with removeEmpty option
TEST_F(TestDeletion, TestDeleteKeyRemoveEmpty) {
std::string input =
"[section]\n"
"key1 = value1\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_TRUE(ini.SectionExists("section"));
// Delete with removeEmpty = true
bool result = ini.Delete("section", "key1", true);
ASSERT_TRUE(result);
// Section should be removed
ASSERT_FALSE(ini.SectionExists("section"));
}
// Test Delete key without removeEmpty leaves empty section
TEST_F(TestDeletion, TestDeleteKeyKeepEmpty) {
std::string input =
"[section]\n"
"key1 = value1\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// Delete with removeEmpty = false (default)
bool result = ini.Delete("section", "key1", false);
ASSERT_TRUE(result);
// Section should still exist but be empty
ASSERT_TRUE(ini.SectionExists("section"));
ASSERT_EQ(ini.GetSectionSize("section"), 0);
}
// Test Delete non-existent key
TEST_F(TestDeletion, TestDeleteMissing) {
std::string input =
"[section]\n"
"key = value\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
bool result = ini.Delete("section", "missing");
ASSERT_FALSE(result);
result = ini.Delete("missing_section", nullptr);
ASSERT_FALSE(result);
}
// Test DeleteValue with multikey
TEST_F(TestDeletion, TestDeleteValueMultikey) {
ini.SetMultiKey(true);
std::string input =
"[section]\n"
"key = value1\n"
"key = value2\n"
"key = value3\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
CSimpleIniA::TNamesDepend values;
ini.GetAllValues("section", "key", values);
ASSERT_EQ(values.size(), 3);
// Delete specific value
bool result = ini.DeleteValue("section", "key", "value2");
ASSERT_TRUE(result);
// Should have 2 values left
values.clear();
ini.GetAllValues("section", "key", values);
ASSERT_EQ(values.size(), 2);
// Verify value2 is gone
bool found = false;
for (auto it = values.begin(); it != values.end(); ++it) {
if (strcmp(it->pItem, "value2") == 0) {
found = true;
break;
}
}
ASSERT_FALSE(found);
}
// Test DeleteValue removes all when value is NULL
TEST_F(TestDeletion, TestDeleteValueNull) {
ini.SetMultiKey(true);
std::string input =
"[section]\n"
"key = value1\n"
"key = value2\n"
"key = value3\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// Delete all values (NULL means all)
bool result = ini.DeleteValue("section", "key", nullptr);
ASSERT_TRUE(result);
ASSERT_FALSE(ini.KeyExists("section", "key"));
}
// Test DeleteValue with removeEmpty
TEST_F(TestDeletion, TestDeleteValueRemoveEmpty) {
ini.SetMultiKey(true);
std::string input =
"[section]\n"
"key = value1\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// Delete value with removeEmpty = true
bool result = ini.DeleteValue("section", "key", "value1", true);
ASSERT_TRUE(result);
// Section should be removed
ASSERT_FALSE(ini.SectionExists("section"));
}
// Test DeleteValue non-existent value
TEST_F(TestDeletion, TestDeleteValueMissing) {
ini.SetMultiKey(true);
std::string input =
"[section]\n"
"key = value1\n"
"key = value2\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
bool result = ini.DeleteValue("section", "key", "value3");
ASSERT_FALSE(result);
// Values should still be there
CSimpleIniA::TNamesDepend values;
ini.GetAllValues("section", "key", values);
ASSERT_EQ(values.size(), 2);
}
// Test Delete all keys one by one
TEST_F(TestDeletion, TestDeleteAllKeys) {
std::string input =
"[section]\n"
"key1 = value1\n"
"key2 = value2\n"
"key3 = value3\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_EQ(ini.GetSectionSize("section"), 3);
ini.Delete("section", "key1");
ASSERT_EQ(ini.GetSectionSize("section"), 2);
ini.Delete("section", "key2");
ASSERT_EQ(ini.GetSectionSize("section"), 1);
ini.Delete("section", "key3");
ASSERT_EQ(ini.GetSectionSize("section"), 0);
// Section should still exist
ASSERT_TRUE(ini.SectionExists("section"));
}
// Test deletion preserves other data
TEST_F(TestDeletion, TestDeletePreservesOthers) {
std::string input =
"[section1]\n"
"key1 = value1\n"
"key2 = value2\n"
"\n"
"[section2]\n"
"key3 = value3\n"
"key4 = value4\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ini.Delete("section1", "key1");
// Verify section1 still has key2
ASSERT_TRUE(ini.KeyExists("section1", "key2"));
const char* value = ini.GetValue("section1", "key2");
ASSERT_STREQ(value, "value2");
// Verify section2 is untouched
ASSERT_TRUE(ini.KeyExists("section2", "key3"));
ASSERT_TRUE(ini.KeyExists("section2", "key4"));
value = ini.GetValue("section2", "key3");
ASSERT_STREQ(value, "value3");
}
// Test Delete and Save roundtrip
TEST_F(TestDeletion, TestDeleteRoundtrip) {
std::string input =
"[section1]\n"
"key1 = value1\n"
"key2 = value2\n"
"\n"
"[section2]\n"
"key3 = value3\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ini.Delete("section1", "key1");
std::string output;
rc = ini.Save(output);
ASSERT_EQ(rc, SI_OK);
// Load it back
CSimpleIniA ini2;
ini2.SetUnicode();
rc = ini2.LoadData(output);
ASSERT_EQ(rc, SI_OK);
ASSERT_FALSE(ini2.KeyExists("section1", "key1"));
ASSERT_TRUE(ini2.KeyExists("section1", "key2"));
ASSERT_TRUE(ini2.KeyExists("section2", "key3"));
}
// Test DeleteValue only deletes exact match
TEST_F(TestDeletion, TestDeleteValueExactMatch) {
ini.SetMultiKey(true);
std::string input =
"[section]\n"
"key = value\n"
"key = value123\n"
"key = 123value\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
bool result = ini.DeleteValue("section", "key", "value");
ASSERT_TRUE(result);
CSimpleIniA::TNamesDepend values;
ini.GetAllValues("section", "key", values);
ASSERT_EQ(values.size(), 2);
// Verify "value" is gone but others remain
bool found_value123 = false, found_123value = false, found_value = false;
for (auto it = values.begin(); it != values.end(); ++it) {
if (strcmp(it->pItem, "value123") == 0) found_value123 = true;
if (strcmp(it->pItem, "123value") == 0) found_123value = true;
if (strcmp(it->pItem, "value") == 0) found_value = true;
}
ASSERT_TRUE(found_value123);
ASSERT_TRUE(found_123value);
ASSERT_FALSE(found_value);
}
|