File: ts-casesensitivity.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 (318 lines) | stat: -rw-r--r-- 8,398 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
#include "../SimpleIni.h"
#include "gtest/gtest.h"

// Test case-insensitive mode (default)
class TestCaseInsensitive : public ::testing::Test {
protected:
	void SetUp() override {
		ini.SetUnicode();
		// Default is case-insensitive for ASCII
	}
protected:
	CSimpleIniA ini;
};

// Test case-sensitive mode
class TestCaseSensitive : public ::testing::Test {
protected:
	void SetUp() override {
		// Use CSimpleIniCaseA for case-sensitive
	}
protected:
	CSimpleIniCaseA ini;
};

// Case-insensitive section names
TEST_F(TestCaseInsensitive, TestSectionCaseInsensitive) {
	std::string input =
		"[Section]\n"
		"key = value\n";

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

	// All case variations should work
	ASSERT_TRUE(ini.SectionExists("Section"));
	ASSERT_TRUE(ini.SectionExists("SECTION"));
	ASSERT_TRUE(ini.SectionExists("section"));
	ASSERT_TRUE(ini.SectionExists("SeCTioN"));

	ASSERT_STREQ(ini.GetValue("Section", "key"), "value");
	ASSERT_STREQ(ini.GetValue("SECTION", "key"), "value");
	ASSERT_STREQ(ini.GetValue("section", "key"), "value");
}

// Case-insensitive key names
TEST_F(TestCaseInsensitive, TestKeyCaseInsensitive) {
	std::string input =
		"[section]\n"
		"Key = value\n";

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

	// All case variations should work
	ASSERT_TRUE(ini.KeyExists("section", "Key"));
	ASSERT_TRUE(ini.KeyExists("section", "KEY"));
	ASSERT_TRUE(ini.KeyExists("section", "key"));
	ASSERT_TRUE(ini.KeyExists("section", "kEy"));

	ASSERT_STREQ(ini.GetValue("section", "Key"), "value");
	ASSERT_STREQ(ini.GetValue("section", "KEY"), "value");
	ASSERT_STREQ(ini.GetValue("section", "key"), "value");
}

// Case-insensitive SetValue updates existing
TEST_F(TestCaseInsensitive, TestSetValueCaseInsensitive) {
	std::string input =
		"[Section]\n"
		"Key = value1\n";

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

	// Setting with different case should update
	rc = ini.SetValue("SECTION", "KEY", "value2");
	ASSERT_EQ(rc, SI_UPDATED);

	// Should only have one key
	ASSERT_EQ(ini.GetSectionSize("section"), 1);

	ASSERT_STREQ(ini.GetValue("section", "key"), "value2");
}

// Case-sensitive section names
TEST_F(TestCaseSensitive, TestSectionCaseSensitive) {
	ini.SetUnicode();

	std::string input =
		"[Section]\n"
		"key = value1\n"
		"\n"
		"[SECTION]\n"
		"key = value2\n"
		"\n"
		"[section]\n"
		"key = value3\n";

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

	// Each case variation is a different section
	ASSERT_TRUE(ini.SectionExists("Section"));
	ASSERT_TRUE(ini.SectionExists("SECTION"));
	ASSERT_TRUE(ini.SectionExists("section"));
	ASSERT_FALSE(ini.SectionExists("SeCTioN")); // This exact case not present

	ASSERT_STREQ(ini.GetValue("Section", "key"), "value1");
	ASSERT_STREQ(ini.GetValue("SECTION", "key"), "value2");
	ASSERT_STREQ(ini.GetValue("section", "key"), "value3");

	// Should have 3 sections
	CSimpleIniCaseA::TNamesDepend sections;
	ini.GetAllSections(sections);
	ASSERT_EQ(sections.size(), 3);
}

// Case-sensitive key names
TEST_F(TestCaseSensitive, TestKeyCaseSensitive) {
	ini.SetUnicode();

	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);

	// Each case variation is a different key
	ASSERT_TRUE(ini.KeyExists("section", "Key"));
	ASSERT_TRUE(ini.KeyExists("section", "KEY"));
	ASSERT_TRUE(ini.KeyExists("section", "key"));
	ASSERT_FALSE(ini.KeyExists("section", "kEy")); // This exact case not present

	ASSERT_STREQ(ini.GetValue("section", "Key"), "value1");
	ASSERT_STREQ(ini.GetValue("section", "KEY"), "value2");
	ASSERT_STREQ(ini.GetValue("section", "key"), "value3");

	// Should have 3 keys
	ASSERT_EQ(ini.GetSectionSize("section"), 3);
}

// Case-sensitive SetValue creates new entry
TEST_F(TestCaseSensitive, TestSetValueCaseSensitive) {
	ini.SetUnicode();

	std::string input =
		"[Section]\n"
		"Key = value1\n";

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

	// Setting with different case should insert new
	rc = ini.SetValue("SECTION", "KEY", "value2");
	ASSERT_EQ(rc, SI_INSERTED);

	// Setting original case should update
	rc = ini.SetValue("Section", "Key", "value3");
	ASSERT_EQ(rc, SI_UPDATED);

	// Should have 2 sections now
	CSimpleIniCaseA::TNamesDepend sections;
	ini.GetAllSections(sections);
	ASSERT_EQ(sections.size(), 2);

	ASSERT_STREQ(ini.GetValue("Section", "Key"), "value3");
	ASSERT_STREQ(ini.GetValue("SECTION", "KEY"), "value2");
}

// Case-insensitive delete
TEST_F(TestCaseInsensitive, TestDeleteCaseInsensitive) {
	std::string input =
		"[Section]\n"
		"Key = value\n";

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

	// Delete with different case
	bool result = ini.Delete("SECTION", "KEY");
	ASSERT_TRUE(result);

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

// Case-sensitive delete
TEST_F(TestCaseSensitive, TestDeleteCaseSensitive) {
	ini.SetUnicode();

	std::string input =
		"[Section]\n"
		"Key = value1\n"
		"KEY = value2\n";

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

	// Delete only exact match
	bool result = ini.Delete("Section", "Key");
	ASSERT_TRUE(result);

	ASSERT_FALSE(ini.KeyExists("Section", "Key"));
	ASSERT_TRUE(ini.KeyExists("Section", "KEY")); // Other case still exists
}

// Case-insensitive GetAllKeys
TEST_F(TestCaseInsensitive, TestGetAllKeysCaseInsensitive) {
	std::string input =
		"[section]\n"
		"key1 = value1\n"
		"KEY1 = value2\n"  // Should update, not create new
		"key2 = value3\n";

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

	CSimpleIniA::TNamesDepend keys;
	ini.GetAllKeys("section", keys);

	// Should have 2 unique keys (key1 and key2)
	ASSERT_EQ(keys.size(), 2);
}

// Case-sensitive GetAllKeys
TEST_F(TestCaseSensitive, TestGetAllKeysCaseSensitive) {
	ini.SetUnicode();

	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);

	CSimpleIniCaseA::TNamesDepend keys;
	ini.GetAllKeys("section", keys);

	// Should have 3 keys (key1, KEY1, key2)
	ASSERT_EQ(keys.size(), 3);
}

// Values are always case-sensitive
TEST_F(TestCaseInsensitive, TestValueCaseSensitive) {
	std::string input =
		"[section]\n"
		"key = Value\n";

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

	const char* value = ini.GetValue("section", "key");
	ASSERT_STREQ(value, "Value");
	// Value case is preserved
	ASSERT_STRNE(value, "value");
	ASSERT_STRNE(value, "VALUE");
}

// Case-insensitive with Unicode (only ASCII is case-insensitive)
TEST_F(TestCaseInsensitive, TestUnicodeCaseSensitive) {
	const char lower[] = u8"testé";
	const char upper[] = u8"TESTÉ";

	ini.SetValue("section", lower, "value1");
	ini.SetValue("section", upper, "value2");

	// Unicode characters should be case-sensitive even in case-insensitive mode
	// (Only ASCII characters are folded)
	const char* val1 = ini.GetValue("section", lower);
	const char* val2 = ini.GetValue("section", upper);

	// Depending on implementation, these might be the same or different
	// At minimum, verify both values exist
	ASSERT_NE(val1, nullptr);
	ASSERT_NE(val2, nullptr);
}

// Case-insensitive roundtrip preserves original case
TEST_F(TestCaseInsensitive, TestRoundtripPreservesCase) {
	std::string input =
		"[MixedCase]\n"
		"MixedKey = MixedValue\n";

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

	std::string output;
	rc = ini.Save(output);
	ASSERT_EQ(rc, SI_OK);

	// Original case should be preserved in output
	EXPECT_NE(output.find("[MixedCase]"), std::string::npos);
	EXPECT_NE(output.find("MixedKey"), std::string::npos);
	EXPECT_NE(output.find("MixedValue"), std::string::npos);
}

// Case-sensitive section in different cases are independent
TEST_F(TestCaseSensitive, TestIndependentSections) {
	ini.SetUnicode();

	ini.SetValue("section", "key", "value1");
	ini.SetValue("Section", "key", "value2");
	ini.SetValue("SECTION", "key", "value3");

	ASSERT_STREQ(ini.GetValue("section", "key"), "value1");
	ASSERT_STREQ(ini.GetValue("Section", "key"), "value2");
	ASSERT_STREQ(ini.GetValue("SECTION", "key"), "value3");

	// Deleting one shouldn't affect others
	ini.Delete("Section", nullptr);

	ASSERT_TRUE(ini.SectionExists("section"));
	ASSERT_FALSE(ini.SectionExists("Section"));
	ASSERT_TRUE(ini.SectionExists("SECTION"));
}