File: ts-sections.cpp

package info (click to toggle)
libsimpleini 4.25%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 724 kB
  • sloc: cpp: 4,599; makefile: 75; sh: 3
file content (341 lines) | stat: -rw-r--r-- 7,869 bytes parent folder | download
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
331
332
333
334
335
336
337
338
339
340
341
#include "../SimpleIni.h"
#include "gtest/gtest.h"

class TestSections : public ::testing::Test {
protected:
	void SetUp() override;
protected:
	CSimpleIniA ini;
};

void TestSections::SetUp() {
	ini.SetUnicode();
}

// Test GetSectionSize
TEST_F(TestSections, TestGetSectionSize) {
	std::string input =
		"[section1]\n"
		"key1 = value1\n"
		"key2 = value2\n"
		"key3 = value3\n"
		"\n"
		"[section2]\n"
		"key1 = value1\n"
		"\n"
		"[empty]\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	int size = ini.GetSectionSize("section1");
	ASSERT_EQ(size, 3);

	size = ini.GetSectionSize("section2");
	ASSERT_EQ(size, 1);

	size = ini.GetSectionSize("empty");
	ASSERT_EQ(size, 0);

	// Non-existent section
	size = ini.GetSectionSize("missing");
	ASSERT_EQ(size, -1);
}

// Test GetSectionSize with multikey
TEST_F(TestSections, TestGetSectionSizeMultikey) {
	ini.SetMultiKey(true);

	std::string input =
		"[section]\n"
		"key1 = value1\n"
		"key1 = value2\n"
		"key2 = value3\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	// Should count unique keys, not total entries
	int size = ini.GetSectionSize("section");
	ASSERT_EQ(size, 2);
}

// Test GetSection
TEST_F(TestSections, TestGetSection) {
	std::string input =
		"[section1]\n"
		"key1 = value1\n"
		"key2 = value2\n"
		"key3 = value3\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	const CSimpleIniA::TKeyVal* section = ini.GetSection("section1");
	ASSERT_NE(section, nullptr);
	ASSERT_EQ(section->size(), 3);

	// Verify we can iterate the section
	int count = 0;
	for (auto it = section->begin(); it != section->end(); ++it) {
		count++;
		ASSERT_NE(it->first.pItem, nullptr);
		ASSERT_NE(it->second, nullptr);
	}
	ASSERT_EQ(count, 3);
}

// Test GetSection returns nullptr for missing section
TEST_F(TestSections, TestGetSectionMissing) {
	std::string input = "[section1]\nkey=value\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	const CSimpleIniA::TKeyVal* section = ini.GetSection("missing");
	ASSERT_EQ(section, nullptr);
}

// Test SectionExists
TEST_F(TestSections, TestSectionExists) {
	std::string input =
		"[section1]\n"
		"key = value\n"
		"\n"
		"[section2]\n"
		"\n"
		"[empty]\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	ASSERT_TRUE(ini.SectionExists("section1"));
	ASSERT_TRUE(ini.SectionExists("section2"));
	ASSERT_TRUE(ini.SectionExists("empty"));
	ASSERT_FALSE(ini.SectionExists("missing"));
	ASSERT_FALSE(ini.SectionExists(""));
}

// Test KeyExists
TEST_F(TestSections, TestKeyExists) {
	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.KeyExists("section1", "key1"));
	ASSERT_TRUE(ini.KeyExists("section1", "key2"));
	ASSERT_FALSE(ini.KeyExists("section1", "key3"));
	ASSERT_FALSE(ini.KeyExists("section1", "missing"));

	ASSERT_TRUE(ini.KeyExists("section2", "key3"));
	ASSERT_FALSE(ini.KeyExists("section2", "key1"));

	ASSERT_FALSE(ini.KeyExists("missing", "key"));
}

// Test KeyExists with empty section
TEST_F(TestSections, TestKeyExistsEmptySection) {
	ini.SetAllowKeyOnly(true);

	std::string input =
		"key1 = value1\n"
		"[section]\n"
		"key2 = value2\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	ASSERT_TRUE(ini.KeyExists("", "key1"));
	ASSERT_FALSE(ini.KeyExists("", "key2"));
	ASSERT_TRUE(ini.KeyExists("section", "key2"));
}

// Test GetAllSections
TEST_F(TestSections, TestGetAllSections) {
	std::string input =
		"[section1]\n"
		"key = value\n"
		"\n"
		"[section2]\n"
		"key = value\n"
		"\n"
		"[section3]\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	CSimpleIniA::TNamesDepend sections;
	ini.GetAllSections(sections);

	ASSERT_EQ(sections.size(), 3);

	// Verify section names
	bool found1 = false, found2 = false, found3 = false;
	for (auto it = sections.begin(); it != sections.end(); ++it) {
		if (strcmp(it->pItem, "section1") == 0) found1 = true;
		if (strcmp(it->pItem, "section2") == 0) found2 = true;
		if (strcmp(it->pItem, "section3") == 0) found3 = true;
	}
	ASSERT_TRUE(found1);
	ASSERT_TRUE(found2);
	ASSERT_TRUE(found3);
}

// Test GetAllKeys
TEST_F(TestSections, TestGetAllKeys) {
	std::string input =
		"[section1]\n"
		"key1 = value1\n"
		"key2 = value2\n"
		"key3 = value3\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	CSimpleIniA::TNamesDepend keys;
	bool result = ini.GetAllKeys("section1", keys);
	ASSERT_TRUE(result);
	ASSERT_EQ(keys.size(), 3);

	// Verify key names
	bool found1 = false, found2 = false, found3 = false;
	for (auto it = keys.begin(); it != keys.end(); ++it) {
		if (strcmp(it->pItem, "key1") == 0) found1 = true;
		if (strcmp(it->pItem, "key2") == 0) found2 = true;
		if (strcmp(it->pItem, "key3") == 0) found3 = true;
	}
	ASSERT_TRUE(found1);
	ASSERT_TRUE(found2);
	ASSERT_TRUE(found3);
}

// Test GetAllKeys for missing section
TEST_F(TestSections, TestGetAllKeysMissing) {
	std::string input = "[section1]\nkey=value\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	CSimpleIniA::TNamesDepend keys;
	bool result = ini.GetAllKeys("missing", keys);
	ASSERT_FALSE(result);
	ASSERT_EQ(keys.size(), 0);
}

// Test GetAllKeys with multikey
TEST_F(TestSections, TestGetAllKeysMultikey) {
	ini.SetMultiKey(true);

	std::string input =
		"[section]\n"
		"key1 = value1\n"
		"key1 = value2\n"
		"key2 = value3\n"
		"key2 = value4\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	CSimpleIniA::TNamesDepend keys;
	bool result = ini.GetAllKeys("section", keys);
	ASSERT_TRUE(result);

	// Should return unique keys
	ASSERT_EQ(keys.size(), 2);
}

// Test creating empty section
TEST_F(TestSections, TestCreateEmptySection) {
	SI_Error rc = ini.SetValue("newsection", nullptr, nullptr);
	ASSERT_EQ(rc, SI_INSERTED);

	ASSERT_TRUE(ini.SectionExists("newsection"));
	ASSERT_EQ(ini.GetSectionSize("newsection"), 0);

	// Adding same section again should update, not insert
	rc = ini.SetValue("newsection", nullptr, nullptr);
	ASSERT_EQ(rc, SI_UPDATED);
}

// Test section with only keys (no equals sign)
TEST_F(TestSections, TestSectionWithKeyOnly) {
	ini.SetAllowKeyOnly(true);

	std::string input =
		"[section]\n"
		"key1\n"
		"key2\n"
		"key3\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	int size = ini.GetSectionSize("section");
	ASSERT_EQ(size, 3);

	ASSERT_TRUE(ini.KeyExists("section", "key1"));
	ASSERT_TRUE(ini.KeyExists("section", "key2"));
	ASSERT_TRUE(ini.KeyExists("section", "key3"));

	// Values should be NULL or empty
	const char* value = ini.GetValue("section", "key1");
	ASSERT_TRUE(value == nullptr || strlen(value) == 0);
}

// Test GetSection with multikey returns all entries
TEST_F(TestSections, TestGetSectionMultikey) {
	ini.SetMultiKey(true);

	std::string input =
		"[section]\n"
		"key1 = value1\n"
		"key1 = value2\n"
		"key2 = value3\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	const CSimpleIniA::TKeyVal* section = ini.GetSection("section");
	ASSERT_NE(section, nullptr);

	// Should have 3 total entries (not 2)
	ASSERT_EQ(section->size(), 3);
}

// Test empty ini
TEST_F(TestSections, TestEmptyIni) {
	ASSERT_TRUE(ini.IsEmpty());

	CSimpleIniA::TNamesDepend sections;
	ini.GetAllSections(sections);
	ASSERT_EQ(sections.size(), 0);

	ASSERT_FALSE(ini.SectionExists("anything"));
	ASSERT_EQ(ini.GetSectionSize("anything"), -1);
	ASSERT_EQ(ini.GetSection("anything"), nullptr);
}

// Test Reset clears all data
TEST_F(TestSections, TestReset) {
	std::string input =
		"[section1]\n"
		"key = value\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	ASSERT_FALSE(ini.IsEmpty());
	ASSERT_TRUE(ini.SectionExists("section1"));

	ini.Reset();

	ASSERT_TRUE(ini.IsEmpty());
	ASSERT_FALSE(ini.SectionExists("section1"));
}