Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_desktopfilereader.cpp
More file actions
439 lines (340 loc) · 13.2 KB
/
Copy pathtest_desktopfilereader.cpp
File metadata and controls
439 lines (340 loc) · 13.2 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// library headers
#include <gtest/gtest.h>
// local headers
#include "../src/desktopfilereader.h"
#include "linuxdeploy/desktopfile/exceptions.h"
using namespace linuxdeploy::desktopfile;
class DesktopFileReaderTest : public ::testing::Test {
void SetUp() override {}
void TearDown() override {}
};
TEST_F(DesktopFileReaderTest, testDefaultConstructor) {
DesktopFileReader reader;
EXPECT_TRUE(reader.isEmpty());
}
TEST_F(DesktopFileReaderTest, testPathConstructor) {
const std::string path = "/dev/null";
DesktopFileReader reader(path);
EXPECT_TRUE(reader.isEmpty());
EXPECT_THROW(DesktopFileReader("/no/such/file/or/directory"), IOError);
}
TEST_F(DesktopFileReaderTest, testStreamConstructor) {
std::stringstream ss;
DesktopFileReader reader(ss);
EXPECT_TRUE(reader.isEmpty());
}
TEST_F(DesktopFileReaderTest, testPathConstructorWithEmptyPath) {
EXPECT_THROW(DesktopFileReader(""), IOError);
}
TEST_F(DesktopFileReaderTest, testPathConstructorWithNonExistingPath) {
EXPECT_THROW(DesktopFileReader("/no/such/path/42"), IOError);
}
TEST_F(DesktopFileReaderTest, testEqualityAndInequalityOperators) {
{
DesktopFileReader emptyReader;
EXPECT_TRUE(emptyReader == emptyReader);
EXPECT_FALSE(emptyReader != emptyReader);
}
{
// make sure that files with different paths are recognized as different
DesktopFile nullFile("/dev/null");
DesktopFile fileWithoutPath;
EXPECT_TRUE(nullFile != fileWithoutPath);
EXPECT_FALSE(nullFile == fileWithoutPath);
}
{
// make sure that files with different contents are recognized as different
DesktopFile fileWithContents;
fileWithContents.setEntry("Desktop Entry", DesktopFileEntry("test", "test"));
DesktopFile emptyFile;
EXPECT_TRUE(emptyFile != fileWithContents);
EXPECT_FALSE(emptyFile == fileWithContents);
}
}
TEST_F(DesktopFileReaderTest, testCopyConstructor) {
{
const std::string path = "/dev/null";
DesktopFileReader reader(path);
EXPECT_TRUE(reader.isEmpty());
DesktopFileReader copy = reader;
EXPECT_TRUE(copy.isEmpty());
EXPECT_EQ(reader, copy);
}
{
// make sure that contents are copied, too
DesktopFile file;
file.setEntry("Desktop Entry", DesktopFileEntry("test", "test"));
std::stringstream ss;
file.save(ss);
DesktopFileReader reader(ss);
DesktopFileReader copy(reader);
EXPECT_TRUE(reader.data() == copy.data());
EXPECT_TRUE(reader == copy);
}
}
TEST_F(DesktopFileReaderTest, testCopyAssignmentConstructor) {
const std::string path = "/dev/null";
DesktopFileReader reader;
EXPECT_TRUE(reader.isEmpty());
DesktopFileReader otherReader(path);
EXPECT_TRUE(otherReader.isEmpty());
reader = otherReader;
EXPECT_EQ(reader.path(), path);
// test self-assignment
reader = reader;
}
TEST_F(DesktopFileReaderTest, testMoveAssignmentConstructor) {
const std::string path = "/dev/null";
DesktopFileReader reader;
EXPECT_TRUE(reader.isEmpty());
DesktopFileReader otherReader(path);
EXPECT_TRUE(otherReader.isEmpty());
reader = std::move(otherReader);
EXPECT_EQ(reader.path(), path);
// test self-assignment
reader = std::move(reader);
}
TEST_F(DesktopFileReaderTest, testParseSimpleDesktopFile) {
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "Version=1.0" << std::endl
<< "Name=name" << std::endl
<< "Exec=exec" << std::endl
<< "Icon=icon" << std::endl
<< "Type=Application" << std::endl
<< "Categories=Utility;Multimedia;" << std::endl;
DesktopFileReader reader;
reader = DesktopFileReader(ss);
auto section = reader["Desktop Entry"];
EXPECT_FALSE(section.empty());
EXPECT_EQ(section.size(), 6);
EXPECT_NEAR(section["Version"].asDouble(), 1.0, 0.000001);
EXPECT_EQ(section["Name"].value(), "name");
EXPECT_EQ(section["Exec"].value(), "exec");
EXPECT_EQ(section["Icon"].value(), "icon");
EXPECT_EQ(section["Type"].value(), "Application");
EXPECT_EQ(section["Name"].value(), "name");
EXPECT_EQ(section["Categories"].parseStringList(), std::vector<std::string>({"Utility", "Multimedia"}));
}
TEST_F(DesktopFileReaderTest, testParseSimpleDesktopFileWithComments) {
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "Version=1.0" << std::endl
<< "Name=name" << std::endl
<< "# a comment" << std::endl
<< "Exec=exec" << std::endl
<< "Icon=icon" << std::endl
<< "Type=Application" << std::endl
<< "# another comment" << std::endl
<< "Categories=Utility;Multimedia;" << std::endl;
DesktopFileReader reader;
reader = DesktopFileReader(ss);
auto section = reader["Desktop Entry"];
EXPECT_FALSE(section.empty());
EXPECT_EQ(section.size(), 6);
// TODO: check for comments in data
// right now, they're just discarded, but they shall be preserved in the right positions in the future
EXPECT_NEAR(section["Version"].asDouble(), 1.0f, 0.000001);
EXPECT_EQ(section["Name"].value(), "name");
EXPECT_EQ(section["Exec"].value(), "exec");
EXPECT_EQ(section["Icon"].value(), "icon");
EXPECT_EQ(section["Type"].value(), "Application");
EXPECT_EQ(section["Name"].value(), "name");
EXPECT_EQ(section["Categories"].parseStringList(), std::vector<std::string>({"Utility", "Multimedia"}));
}
TEST_F(DesktopFileReaderTest, testParseFileGetNonExistingSection) {
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl;
DesktopFileReader reader;
reader = DesktopFileReader(ss);
EXPECT_THROW(reader["Non-existing Section"], UnknownSectionError);
}
TEST_F(DesktopFileReaderTest, testParseFileMissingSectionHeader) {
std::stringstream ss;
ss << "Name=name" << std::endl
<< "Exec=exec" << std::endl
<< "Icon=icon" << std::endl
<< "Type=Application" << std::endl
<< "Categories=Utility;Multimedia;" << std::endl;
DesktopFileReader reader;
EXPECT_THROW(reader = DesktopFileReader(ss), ParseError);
}
TEST_F(DesktopFileReaderTest, testParseFileEmptyKey) {
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "=name" << std::endl
<< "Exec=exec" << std::endl
<< "Icon=icon" << std::endl
<< "Type=Application" << std::endl
<< "Categories=Utility;Multimedia;" << std::endl;
EXPECT_THROW(DesktopFileReader reader = DesktopFileReader(ss), ParseError);
}
TEST_F(DesktopFileReaderTest, testParseFileMissingDelimiterInLine) {
{
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "Exec" << std::endl;
DesktopFileReader reader;
EXPECT_THROW(reader = DesktopFileReader(ss), ParseError);
}
{
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "Name name" << std::endl;
DesktopFileReader reader;
EXPECT_THROW(reader = DesktopFileReader(ss), ParseError);
}
}
TEST_F(DesktopFileReaderTest, testParseFile) {
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "Name name" << std::endl
<< "Exec" << std::endl;
DesktopFileReader reader;
EXPECT_THROW(reader = DesktopFileReader(ss), ParseError);
}
TEST_F(DesktopFileReaderTest, testParseFileMultipleDelimitersInLine) {
// TODO: verify that ==Name would be a legal value according to desktop file specification
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "Name===name" << std::endl;
DesktopFileReader reader;
ASSERT_NO_THROW(reader = DesktopFileReader(ss));
}
TEST_F(DesktopFileReaderTest, testParseFileWithLeadingAndTrailingWhitespaceInLines) {
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "Name= name" << std::endl
<< "Exec =exec" << std::endl;
DesktopFileReader reader(ss);
auto section = reader["Desktop Entry"];
EXPECT_FALSE(section.empty());
EXPECT_EQ(section["Name"].value(), "name");
EXPECT_EQ(section["Exec"].value(), "exec");
}
TEST_F(DesktopFileReaderTest, testDataGetter) {
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "Name= name" << std::endl
<< "Exec =exec" << std::endl;
DesktopFileReader reader(ss);
auto section = reader["Desktop Entry"];
EXPECT_FALSE(section.empty());
auto data = reader.data();
auto expected = DesktopFile::section_t({
{"Name", DesktopFileEntry("Name", "name")},
{"Exec", DesktopFileEntry("Exec", "exec")},
});
EXPECT_EQ(data["Desktop Entry"], expected);
}
TEST_F(DesktopFileReaderTest, testParseLinesWithMultipleSpaces) {
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "Name= What a great name " << std::endl;
DesktopFileReader reader(ss);
auto section = reader["Desktop Entry"];
EXPECT_FALSE(section.empty());
EXPECT_EQ(section["Name"].value(), "What a great name");
}
TEST_F(DesktopFileReaderTest, testReadBrokenSectionHeaderMissingClosingBracket) {
{
std::stringstream ins;
ins << "[Desktop Entry" << std::endl
<< "test=test" << std::endl;
EXPECT_THROW(DesktopFileReader reader(ins), ParseError);
}
// also test for brokenness in a later section, as the first section is normally treated specially
{
std::stringstream ins;
ins << "[Desktop Entry]" << std::endl
<< "test=test" << std::endl
<< "[Another Section" << std::endl;
EXPECT_THROW(DesktopFileReader reader(ins), ParseError);
}
}
TEST_F(DesktopFileReaderTest, testReadBrokenSectionHeaderTooManyClosingBrackets) {
{
std::stringstream ins;
ins << "[Desktop Entry]]" << std::endl
<< "test=test" << std::endl;
EXPECT_THROW(DesktopFileReader reader(ins), ParseError);
}
// also test for brokenness in a later section, as the first section is normally treated specially
{
std::stringstream ins;
ins << "[Desktop Entry]" << std::endl
<< "test=test" << std::endl
<< "[Another Section]]" << std::endl;
EXPECT_THROW(DesktopFileReader reader(ins), ParseError);
}
}
TEST_F(DesktopFileReaderTest, testReadBrokenSectionHeaderTooManyOpeningBrackets) {
{
std::stringstream ins;
ins << "[[Desktop Entry]" << std::endl
<< "test=test" << std::endl;
EXPECT_THROW(DesktopFileReader reader(ins), ParseError);
}
// also test for brokenness in a later section, as the first section is normally treated specially
{
std::stringstream ins;
ins << "[Desktop Entry]" << std::endl
<< "test=test" << std::endl
<< "[[Another Section]";
EXPECT_THROW(DesktopFileReader reader(ins), ParseError);
}
}
TEST_F(DesktopFileReaderTest, testReadBrokenSectionMissingOpeningBracket) {
{
std::stringstream ins;
ins << "Desktop Entry]" << std::endl
<< "test=test" << std::endl;
EXPECT_THROW(DesktopFileReader reader(ins), ParseError);
}
// also test for brokenness in a later section, as the first section is normally treated specially
{
std::stringstream ins;
ins << "[Desktop Entry]" << std::endl
<< "test=test" << std::endl
<< "Another Section]" << std::endl;
EXPECT_THROW(DesktopFileReader reader(ins), ParseError);
}
}
// FIXME: introduce proper localization support
TEST_F(DesktopFileReaderTest, testReadLocalizedEntriesWithoutProperLocalizationSupport) {
// lang_COUNTRY.ENCODING@MODIFIER
// https://standards.freedesktop.org/desktop-entry-spec/latest/ar01s05.html
for (std::string locale : {"de", "de_DE", "ca@valencia", "de_DE.UTF-8"}) {
std::stringstream ss;
ss << "[Desktop Entry]" << std::endl
<< "Name=name" << std::endl
<< "Name[" << locale << "]=name" << std::endl
<< "Exec=exec" << std::endl;
DesktopFileReader reader;
EXPECT_NO_THROW(reader = DesktopFileReader(ss)) << "tested locale string: " << locale;
auto section = reader["Desktop Entry"];
EXPECT_FALSE(section.empty());
auto data = reader.data();
auto expected = DesktopFile::section_t({
{"Name", DesktopFileEntry("Name", "name")},
// FIXME: revise after introduction of localization support
{"Name[" + locale + "]", DesktopFileEntry("Name[" + locale + "]", "name")},
{"Exec", DesktopFileEntry("Exec", "exec")},
});
EXPECT_EQ(data["Desktop Entry"], expected) << "tested locale string: " << locale;
}
}
TEST_F(DesktopFileReaderTest, testBrokenLocalizedKeys) {
for (std::string key : {"test]de[", "test[de]]", "test[[de]", "test[]de", "test[de", "testde]"}) {
std::stringstream ins;
ins << "[Desktop Entry]" << std::endl
<< key << "=test" << std::endl;
EXPECT_THROW(DesktopFileReader reader(ins), ParseError) << "key: " << key;
}
}
TEST_F(DesktopFileReaderTest, testKeyWithDashesAndLocaleWithUnderscore) {
std::stringstream ins;
ins << "[Desktop Entry]" << std::endl
<< "X-GNOME-FullName[fr_FR]=test" << std::endl;
EXPECT_NO_THROW(DesktopFileReader reader(ins));
}
You can’t perform that action at this time.
