FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppcheck/test/testpath.cpp at main · cppcheck-opensource/cppcheck · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
cppcheck-opensource
/
cppcheck
Public
Notifications
You must be signed in to change notification settings
Fork
1.6k
Star
6.7k
Code
Pull requests
201
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
cppcheck
/
test
/
testpath.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
654 lines (562 loc) · 28.5 KB
Breadcrumbs
cppcheck
/
test
/
testpath.cpp
Copy path
File metadata and controls
654 lines (562 loc) · 28.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#
include
"
path.h
"
#
include
"
fixture.h
"
#
include
"
helpers.h
"
#
include
"
standards.h
"
#
include
<
initializer_list
>
#
include
<
list
>
#
include
<
set
>
#
include
<
string
>
#
include
<
vector
>
#
ifndef
_WIN32
#
include
<
stdexcept
>
#
endif
class
TestPath
:
public
TestFixture
{
public:
TestPath
() : TestFixture(
"
TestPath
"
) {}
private:
void
run
()
override
{
TEST_CASE
(removeQuotationMarks);
TEST_CASE
(acceptFile);
TEST_CASE
(getCurrentPath);
TEST_CASE
(getCurrentExecutablePath);
TEST_CASE
(isAbsolute);
TEST_CASE
(getRelative);
TEST_CASE
(get_path_from_filename);
TEST_CASE
(join);
TEST_CASE
(isDirectory);
TEST_CASE
(isFile);
TEST_CASE
(sameFileName);
TEST_CASE
(getFilenameExtension);
TEST_CASE
(identify);
TEST_CASE
(identifyWithCppProbe);
TEST_CASE
(is_header);
TEST_CASE
(simplifyPath);
TEST_CASE
(getAbsolutePath);
TEST_CASE
(exists);
TEST_CASE
(fromNativeSeparators);
TEST_CASE
(isRelative);
}
void
removeQuotationMarks
()
const
{
//
Path::removeQuotationMarks()
ASSERT_EQUALS
(
"
index.cpp
"
,
Path::removeQuotationMarks
(
"
index.cpp
"
));
ASSERT_EQUALS
(
"
index.cpp
"
,
Path::removeQuotationMarks
(
"
\"
index.cpp
"
));
ASSERT_EQUALS
(
"
index.cpp
"
,
Path::removeQuotationMarks
(
"
index.cpp
\"
"
));
ASSERT_EQUALS
(
"
index.cpp
"
,
Path::removeQuotationMarks
(
"
\"
index.cpp
\"
"
));
ASSERT_EQUALS
(
"
path to/index.cpp
"
,
Path::removeQuotationMarks
(
"
\"
path to
\"
/index.cpp
"
));
ASSERT_EQUALS
(
"
path to/index.cpp
"
,
Path::removeQuotationMarks
(
"
\"
path to/index.cpp
\"
"
));
ASSERT_EQUALS
(
"
the/path to/index.cpp
"
,
Path::removeQuotationMarks
(
"
the/
\"
path to
\"
/index.cpp
"
));
ASSERT_EQUALS
(
"
the/path to/index.cpp
"
,
Path::removeQuotationMarks
(
"
\"
the/path to/index.cpp
\"
"
));
}
void
acceptFile
()
const
{
ASSERT
(
Path::acceptFile
(
"
index.c
"
));
ASSERT
(
Path::acceptFile
(
"
index.cpp
"
));
ASSERT
(
Path::acceptFile
(
"
index.invalid.cpp
"
));
ASSERT
(
Path::acceptFile
(
"
index.invalid.Cpp
"
));
ASSERT
(
Path::acceptFile
(
"
index.invalid.C
"
));
ASSERT
(
Path::acceptFile
(
"
index.invalid.C++
"
));
ASSERT
(
Path::acceptFile
(
"
index.
"
)==
false
);
ASSERT
(
Path::acceptFile
(
"
index
"
)==
false
);
ASSERT
(
Path::acceptFile
(
"
"
)==
false
);
ASSERT
(
Path::acceptFile
(
"
C
"
)==
false
);
{
Standards::Language lang;
ASSERT
(
Path::acceptFile
(
"
index.c
"
, &lang));
ASSERT_EQUALS_ENUM
(Standards::Language::C, lang);
}
{
Standards::Language lang;
ASSERT
(
Path::acceptFile
(
"
index.cpp
"
, &lang));
ASSERT_EQUALS_ENUM
(Standards::Language::
CPP
, lang);
}
//
don't accept any headers
ASSERT_EQUALS
(
false
,
Path::acceptFile
(
"
index.h
"
));
ASSERT_EQUALS
(
false
,
Path::acceptFile
(
"
index.hpp
"
));
const
std::set<std::string> extra = {
"
.extra
"
,
"
.header
"
};
ASSERT
(
Path::acceptFile
(
"
index.c
"
, extra));
ASSERT
(
Path::acceptFile
(
"
index.cpp
"
, extra));
ASSERT
(
Path::acceptFile
(
"
index.extra
"
, extra));
ASSERT
(
Path::acceptFile
(
"
index.header
"
, extra));
ASSERT
(
Path::acceptFile
(
"
index.h
"
, extra)==
false
);
ASSERT
(
Path::acceptFile
(
"
index.hpp
"
, extra)==
false
);
{
Standards::Language lang;
ASSERT
(
Path::acceptFile
(
"
index.c
"
, extra, &lang));
ASSERT_EQUALS_ENUM
(Standards::Language::C, lang);
}
{
Standards::Language lang;
ASSERT
(
Path::acceptFile
(
"
index.cpp
"
, extra, &lang));
ASSERT_EQUALS_ENUM
(Standards::Language::
CPP
, lang);
}
{
Standards::Language lang;
ASSERT
(
Path::acceptFile
(
"
index.extra
"
, extra, &lang));
ASSERT_EQUALS_ENUM
(Standards::Language::None, lang);
}
}
void
getCurrentPath
()
const
{
ASSERT_EQUALS
(
true
,
Path::isAbsolute
(
Path::getCurrentPath
()));
}
void
getCurrentExecutablePath
()
const
{
ASSERT_EQUALS
(
false
,
Path::getCurrentExecutablePath
(
"
"
).
empty
());
}
void
isAbsolute
()
const
{
#
ifdef
_WIN32
ASSERT_EQUALS
(
true
,
Path::isAbsolute
(
"
C:
\\
foo
\\
bar
"
));
ASSERT_EQUALS
(
true
,
Path::isAbsolute
(
"
C:/foo/bar
"
));
ASSERT_EQUALS
(
true
,
Path::isAbsolute
(
"
\\\\
foo
\\
bar
"
));
ASSERT_EQUALS
(
false
,
Path::isAbsolute
(
"
foo
\\
bar
"
));
ASSERT_EQUALS
(
false
,
Path::isAbsolute
(
"
foo/bar
"
));
ASSERT_EQUALS
(
false
,
Path::isAbsolute
(
"
foo.cpp
"
));
ASSERT_EQUALS
(
false
,
Path::isAbsolute
(
"
C:foo.cpp
"
));
ASSERT_EQUALS
(
false
,
Path::isAbsolute
(
"
C:foo
\\
bar.cpp
"
));
ASSERT_EQUALS
(
false
,
Path::isAbsolute
(
"
bar.cpp
"
));
TODO_ASSERT_EQUALS
(
true
,
false
,
Path::isAbsolute
(
"
\\
"
));
#
else
ASSERT_EQUALS
(
true
,
Path::isAbsolute
(
"
/foo/bar
"
));
ASSERT_EQUALS
(
true
,
Path::isAbsolute
(
"
/
"
));
ASSERT_EQUALS
(
false
,
Path::isAbsolute
(
"
foo/bar
"
));
ASSERT_EQUALS
(
false
,
Path::isAbsolute
(
"
foo.cpp
"
));
#
endif
}
void
getRelative
()
const
{
const
std::vector<std::string> basePaths = {
"
"
,
//
Don't crash with empty paths
"
C:/foo
"
,
"
C:/bar/
"
,
"
C:/test.cpp
"
};
ASSERT_EQUALS
(
"
x.c
"
,
Path::getRelativePath
(
"
C:/foo/x.c
"
, basePaths));
ASSERT_EQUALS
(
"
y.c
"
,
Path::getRelativePath
(
"
C:/bar/y.c
"
, basePaths));
ASSERT_EQUALS
(
"
foo/y.c
"
,
Path::getRelativePath
(
"
C:/bar/foo/y.c
"
, basePaths));
ASSERT_EQUALS
(
"
C:/test.cpp
"
,
Path::getRelativePath
(
"
C:/test.cpp
"
, basePaths));
ASSERT_EQUALS
(
"
C:/foobar/test.cpp
"
,
Path::getRelativePath
(
"
C:/foobar/test.cpp
"
, basePaths));
}
void
get_path_from_filename
()
const
{
ASSERT_EQUALS
(
"
"
,
Path::getPathFromFilename
(
"
index.h
"
));
ASSERT_EQUALS
(
"
/tmp/
"
,
Path::getPathFromFilename
(
"
/tmp/index.h
"
));
ASSERT_EQUALS
(
"
a/b/c/
"
,
Path::getPathFromFilename
(
"
a/b/c/index.h
"
));
ASSERT_EQUALS
(
"
a/b/c/
"
,
Path::getPathFromFilename
(
"
a/b/c/
"
));
}
void
join
()
const
{
ASSERT_EQUALS
(
"
"
,
Path::join
(
"
"
,
"
"
));
ASSERT_EQUALS
(
"
b
"
,
Path::join
(
"
"
,
"
b
"
));
ASSERT_EQUALS
(
"
/b
"
,
Path::join
(
"
"
,
"
/b
"
));
ASSERT_EQUALS
(
"
/b
"
,
Path::join
(
"
"
,
"
\\
b
"
));
ASSERT_EQUALS
(
"
a
"
,
Path::join
(
"
a
"
,
"
"
));
ASSERT_EQUALS
(
"
a/b
"
,
Path::join
(
"
a
"
,
"
b
"
));
ASSERT_EQUALS
(
"
/b
"
,
Path::join
(
"
a
"
,
"
/b
"
));
ASSERT_EQUALS
(
"
/b
"
,
Path::join
(
"
a
"
,
"
\\
b
"
));
ASSERT_EQUALS
(
"
a/
"
,
Path::join
(
"
a/
"
,
"
"
));
ASSERT_EQUALS
(
"
a/b
"
,
Path::join
(
"
a/
"
,
"
b
"
));
ASSERT_EQUALS
(
"
/b
"
,
Path::join
(
"
a/
"
,
"
/b
"
));
ASSERT_EQUALS
(
"
/b
"
,
Path::join
(
"
a/
"
,
"
\\
b
"
));
ASSERT_EQUALS
(
"
a/
"
,
Path::join
(
"
a
\\
"
,
"
"
));
ASSERT_EQUALS
(
"
a/b
"
,
Path::join
(
"
a
\\
"
,
"
b
"
));
ASSERT_EQUALS
(
"
/b
"
,
Path::join
(
"
a
\\
"
,
"
/b
"
));
ASSERT_EQUALS
(
"
/b
"
,
Path::join
(
"
a
\\
"
,
"
\\
b
"
));
//
TODO: how to absolute Windows path in path2?
//
ASSERT_EQUALS("", Path::join("a", "s:/b"));
//
ASSERT_EQUALS("", Path::join("S:\\a", "S:/b"));
//
ASSERT_EQUALS("", Path::join("S:\\a", "S:\\b"));
//
ASSERT_EQUALS("", Path::join("S:\\a", "/b"));
//
ASSERT_EQUALS("", Path::join("S:/a", "S:/b"));
//
ASSERT_EQUALS("", Path::join("S:/a", "S:\\b"));
//
ASSERT_EQUALS("", Path::join("S:/a", "/b"));
ASSERT_EQUALS
(
"
a/b/c
"
,
Path::join
(
"
a
"
,
"
b
"
,
"
c
"
));
}
void
isDirectory
()
const
{
ScopedFile
file
(
"
testpath.txt
"
,
"
"
,
"
testpath
"
);
ScopedFile
file2
(
"
testpath2.txt
"
,
"
"
);
ASSERT_EQUALS
(
false
,
Path::isDirectory
(
"
testpath.txt
"
));
ASSERT_EQUALS
(
true
,
Path::isDirectory
(
"
testpath
"
));
ASSERT_EQUALS
(
false
,
Path::isDirectory
(
"
testpath/testpath.txt
"
));
ASSERT_EQUALS
(
false
,
Path::isDirectory
(
"
testpath2.txt
"
));
}
void
isFile
()
const
{
ScopedFile
file
(
"
testpath.txt
"
,
"
"
,
"
testpath
"
);
ScopedFile
file2
(
"
testpath2.txt
"
,
"
"
);
ASSERT_EQUALS
(
false
,
Path::isFile
(
"
testpath
"
));
ASSERT_EQUALS
(
false
,
Path::isFile
(
"
testpath.txt
"
));
ASSERT_EQUALS
(
true
,
Path::isFile
(
"
testpath/testpath.txt
"
));
ASSERT_EQUALS
(
true
,
Path::isFile
(
"
testpath2.txt
"
));
}
void
sameFileName
()
const
{
ASSERT
(
Path::sameFileName
(
"
test
"
,
"
test
"
));
//
case sensitivity cases
#
if
defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__))
ASSERT
(
Path::sameFileName
(
"
test
"
,
"
Test
"
));
ASSERT
(
Path::sameFileName
(
"
test
"
,
"
TesT
"
));
ASSERT
(
Path::sameFileName
(
"
test.h
"
,
"
test.H
"
));
ASSERT
(
Path::sameFileName
(
"
test.hh
"
,
"
test.Hh
"
));
ASSERT
(
Path::sameFileName
(
"
test.hh
"
,
"
test.hH
"
));
#
else
ASSERT
(!
Path::sameFileName
(
"
test
"
,
"
Test
"
));
ASSERT
(!
Path::sameFileName
(
"
test
"
,
"
TesT
"
));
ASSERT
(!
Path::sameFileName
(
"
test.h
"
,
"
test.H
"
));
ASSERT
(!
Path::sameFileName
(
"
test.hh
"
,
"
test.Hh
"
));
ASSERT
(!
Path::sameFileName
(
"
test.hh
"
,
"
test.hH
"
));
#
endif
}
void
getFilenameExtension
()
const
{
ASSERT_EQUALS
(
"
"
,
Path::getFilenameExtension
(
"
test
"
));
ASSERT_EQUALS
(
"
"
,
Path::getFilenameExtension
(
"
Test
"
));
ASSERT_EQUALS
(
"
.h
"
,
Path::getFilenameExtension
(
"
test.h
"
));
ASSERT_EQUALS
(
"
.h
"
,
Path::getFilenameExtension
(
"
Test.h
"
));
ASSERT_EQUALS
(
"
"
,
Path::getFilenameExtension
(
"
test
"
,
true
));
ASSERT_EQUALS
(
"
"
,
Path::getFilenameExtension
(
"
Test
"
,
true
));
ASSERT_EQUALS
(
"
.h
"
,
Path::getFilenameExtension
(
"
test.h
"
,
true
));
ASSERT_EQUALS
(
"
.h
"
,
Path::getFilenameExtension
(
"
Test.h
"
,
true
));
//
case sensitivity cases
#
if
defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__))
ASSERT_EQUALS
(
"
.h
"
,
Path::getFilenameExtension
(
"
test.H
"
));
ASSERT_EQUALS
(
"
.hh
"
,
Path::getFilenameExtension
(
"
test.Hh
"
));
ASSERT_EQUALS
(
"
.hh
"
,
Path::getFilenameExtension
(
"
test.hH
"
));
ASSERT_EQUALS
(
"
.h
"
,
Path::getFilenameExtension
(
"
test.H
"
,
true
));
ASSERT_EQUALS
(
"
.hh
"
,
Path::getFilenameExtension
(
"
test.Hh
"
,
true
));
ASSERT_EQUALS
(
"
.hh
"
,
Path::getFilenameExtension
(
"
test.hH
"
,
true
));
#
else
ASSERT_EQUALS
(
"
.H
"
,
Path::getFilenameExtension
(
"
test.H
"
));
ASSERT_EQUALS
(
"
.Hh
"
,
Path::getFilenameExtension
(
"
test.Hh
"
));
ASSERT_EQUALS
(
"
.hH
"
,
Path::getFilenameExtension
(
"
test.hH
"
));
ASSERT_EQUALS
(
"
.h
"
,
Path::getFilenameExtension
(
"
test.H
"
,
true
));
ASSERT_EQUALS
(
"
.hh
"
,
Path::getFilenameExtension
(
"
test.Hh
"
,
true
));
ASSERT_EQUALS
(
"
.hh
"
,
Path::getFilenameExtension
(
"
test.hH
"
,
true
));
#
endif
}
void
identify
()
const
{
Standards::Language lang;
bool
header;
ASSERT_EQUALS
(Standards::Language::None,
Path::identify
(
"
"
,
false
));
ASSERT_EQUALS
(Standards::Language::None,
Path::identify
(
"
c
"
,
false
));
ASSERT_EQUALS
(Standards::Language::None,
Path::identify
(
"
cpp
"
,
false
));
ASSERT_EQUALS
(Standards::Language::None,
Path::identify
(
"
h
"
,
false
));
ASSERT_EQUALS
(Standards::Language::None,
Path::identify
(
"
hpp
"
,
false
));
//
TODO: what about files starting with a "."?
//
ASSERT_EQUALS(Standards::Language::None, Path::identify(".c", false));
//
ASSERT_EQUALS(Standards::Language::None, Path::identify(".cpp", false));
//
ASSERT_EQUALS(Standards::Language::None, Path::identify(".h", false));
//
ASSERT_EQUALS(Standards::Language::None, Path::identify(".hpp", false));
//
C
ASSERT_EQUALS
(Standards::Language::C,
Path::identify
(
"
index.c
"
,
false
));
ASSERT_EQUALS
(Standards::Language::C,
Path::identify
(
"
index.cl
"
,
false
));
ASSERT_EQUALS
(Standards::Language::C,
Path::identify
(
"
C:
\\
foo
\\
index.c
"
,
false
));
ASSERT_EQUALS
(Standards::Language::C,
Path::identify
(
"
/mnt/c/foo/index.c
"
,
false
));
//
In unix .C is considered C++
#
ifdef
_WIN32
ASSERT_EQUALS
(Standards::Language::C,
Path::identify
(
"
C:
\\
foo
\\
index.C
"
,
false
));
#
endif
lang =
Path::identify
(
"
index.c
"
,
false
, &header);
ASSERT_EQUALS
(Standards::Language::C, lang);
ASSERT_EQUALS
(
false
, header);
//
C++
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
index.cpp
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
index.cxx
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
index.cc
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
index.c++
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
index.tpp
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
index.txx
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
index.ipp
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
index.ixx
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
C:
\\
foo
\\
index.cpp
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
C:
\\
foo
\\
index.Cpp
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
/mnt/c/foo/index.cpp
"
,
false
));
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
/mnt/c/foo/index.Cpp
"
,
false
));
//
TODO: check for case-insensitive filesystem instead
//
In unix .C is considered C++
#
if
!defined(_WIN32) && !(defined(__APPLE__) && defined(__MACH__))
ASSERT_EQUALS
(Standards::Language::
CPP
,
Path::identify
(
"
index.C
"
,
false
));
#
else
ASSERT_EQUALS
(Standards::Language::C,
Path::identify
(
"
index.C
"
,
false
));
#
endif
lang =
Path::identify
(
"
index.cpp
"
,
false
, &header);
ASSERT_EQUALS
(Standards::Language::
CPP
, lang);
ASSERT_EQUALS
(
false
, header);
//
headers
lang =
Path::identify
(
"
index.h
"
,
false
, &header);
ASSERT_EQUALS
(Standards::Language::C, lang);
ASSERT_EQUALS
(
true
, header);
lang =
Path::identify
(
"
index.hpp
"
,
false
, &header);
ASSERT_EQUALS
(Standards::Language::
CPP
, lang);
ASSERT_EQUALS
(
true
, header);
lang =
Path::identify
(
"
index.hxx
"
,
false
, &header);
ASSERT_EQUALS
(Standards::Language::
CPP
, lang);
ASSERT_EQUALS
(
true
, header);
lang =
Path::identify
(
"
index.h++
"
,
false
, &header);
ASSERT_EQUALS
(Standards::Language::
CPP
, lang);
ASSERT_EQUALS
(
true
, header);
lang =
Path::identify
(
"
index.hh
"
,
false
, &header);
ASSERT_EQUALS
(Standards::Language::
CPP
, lang);
ASSERT_EQUALS
(
true
, header);
ASSERT_EQUALS
(Standards::Language::None,
Path::identify
(
"
index.header
"
,
false
));
ASSERT_EQUALS
(Standards::Language::None,
Path::identify
(
"
index.htm
"
,
false
));
ASSERT_EQUALS
(Standards::Language::None,
Path::identify
(
"
index.html
"
,
false
));
}
#
define
identifyWithCppProbeInternal
(...) identifyWithCppProbeInternal_(__FILE__, __LINE__, __VA_ARGS__)
void
identifyWithCppProbeInternal_
(
const
char
* file,
int
line,
const
std::string& filename,
const
std::string& marker, Standards::Language std)
const
{
const
ScopedFile
f
(filename, marker);
ASSERT_EQUALS_LOC_MSG
(std,
Path::identify
(f.
path
(),
true
), filename +
"
\n
"
+ marker, file, line);
}
void
identifyWithCppProbe
()
const
{
const
std::list<std::string> markers_cpp = {
"
// -*- C++ -*-
"
,
"
// -*-C++-*-
"
,
"
// -*- Mode: C++; -*-
"
,
"
// -*-Mode: C++;-*-
"
,
"
// -*- Mode:C++; -*-
"
,
"
// -*-Mode:C++;-*-
"
,
"
// -*- Mode: C++ -*-
"
,
"
// -*-Mode: C++-*-
"
,
"
// -*- Mode:C++ -*-
"
,
"
// -*-Mode:C++-*-
"
,
"
// -*- Mode: C++; c-basic-offset: 8 -*-
"
,
"
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
"
,
"
// -*- c++ -*-
"
,
"
// -*- mode: c++; -*-
"
,
"
/* -*- C++ -*- */
"
,
"
/* -*- C++ -*-
"
,
"
//-*- C++ -*-
"
,
"
//-*- C++ -*-
"
,
"
\t
//-*- C++ -*-
"
,
"
\t
//-*- C++ -*-
"
,
"
\t
//-*- C++ -*-
"
,
"
//-*- C++ -*-
"
,
"
//-*- C++ -*-
\n
"
,
"
// -----*- C++ -*-----
"
,
"
// comment-*- C++ -*-comment
"
,
"
// -*- C++ -*-
\n
"
,
"
//-*- C++ -*-
\r
// comment
"
,
"
//-*- C++ -*-
\n
// comment
"
,
"
//-*- C++ -*-
\r\n
// comment
"
,
"
/* -*-C++-*- */
"
,
"
/*-*-C++-*-*/
"
,
"
/*-*-C++-*-*/
"
,
};
for
(
const
auto
& f : {
"
cppprobe.h
"
,
"
cppprobe
"
}) {
for
(
const
auto
& m : markers_cpp) {
identifyWithCppProbeInternal
(f, m, Standards::Language::
CPP
);
}
}
const
std::list<std::string> markers_c = {
"
-*- C++ -*-
"
,
//
needs to be in comment
"
// -*- C++
"
,
//
no end marker
"
// -*- C++ --*-
"
,
//
incorrect end marker
"
// -*- C++/-*-
"
,
//
unexpected character
"
// comment
\n
// -*- C++ -*-
"
,
//
not on the first line
"
// comment
\r
// -*- C++ -*-
"
,
//
not on the first line
"
// comment
\r\n
// -*- C++ -*-
"
,
//
not on the first line
"
// -*- C -*-
"
,
"
// -*- Mode: C; -*-
"
,
"
// -*- f90 -*-
"
,
"
// -*- fortran -*-
"
,
"
// -*- c-basic-offset: 2 -*-
"
,
"
// -*- c-basic-offset:4; indent-tabs-mode:nil -*-
"
,
"
//
"
,
//
no marker
"
// -*-
"
,
//
incomplete marker
"
/*
"
,
//
no marker
"
/**/
"
,
//
no marker
"
/*
\n
*/
"
,
//
no marker
"
/* */
"
,
//
no marker
"
/*
\n
*/
"
,
//
no marker
"
/* -*-
"
,
//
incomplete marker
"
/*
\n
-*-
"
,
//
incomplete marker
"
/*
\n
-*- C++ -*-
"
,
//
not on the first line
"
/*
\n
-*- C++ -*- */
"
//
not on the first line
};
for
(
const
auto
& m : markers_c) {
identifyWithCppProbeInternal
(
"
cppprobe.h
"
, m, Standards::Language::C);
}
for
(
const
auto
& m : markers_c) {
identifyWithCppProbeInternal
(
"
cppprobe
"
, m, Standards::Language::None);
}
}
void
is_header
()
const
{
ASSERT
(
Path::isHeader
(
"
index.h
"
));
ASSERT
(
Path::isHeader
(
"
index.hpp
"
));
ASSERT
(
Path::isHeader
(
"
index.hxx
"
));
ASSERT
(
Path::isHeader
(
"
index.h++
"
));
ASSERT
(
Path::isHeader
(
"
index.hh
"
));
ASSERT
(
Path::isHeader
(
"
index.c
"
)==
false
);
ASSERT
(
Path::isHeader
(
"
index.cpp
"
)==
false
);
ASSERT
(
Path::isHeader
(
"
index.header
"
)==
false
);
ASSERT
(
Path::isHeader
(
"
index.htm
"
)==
false
);
ASSERT
(
Path::isHeader
(
"
index.html
"
)==
false
);
}
void
simplifyPath
()
const
{
ASSERT_EQUALS
(
"
file.cpp
"
,
Path::simplifyPath
(
"
file.cpp
"
));
ASSERT_EQUALS
(
"
../file.cpp
"
,
Path::simplifyPath
(
"
../file.cpp
"
));
ASSERT_EQUALS
(
"
file.cpp
"
,
Path::simplifyPath
(
"
test/../file.cpp
"
));
ASSERT_EQUALS
(
"
../file.cpp
"
,
Path::simplifyPath
(
"
../test/../file.cpp
"
));
ASSERT_EQUALS
(
"
file.cpp
"
,
Path::simplifyPath
(
"
./file.cpp
"
));
ASSERT_EQUALS
(
"
../file.cpp
"
,
Path::simplifyPath
(
"
./../file.cpp
"
));
ASSERT_EQUALS
(
"
file.cpp
"
,
Path::simplifyPath
(
"
./test/../file.cpp
"
));
ASSERT_EQUALS
(
"
../file.cpp
"
,
Path::simplifyPath
(
"
./../test/../file.cpp
"
));
ASSERT_EQUALS
(
"
test/
"
,
Path::simplifyPath
(
"
test/
"
));
ASSERT_EQUALS
(
"
../test/
"
,
Path::simplifyPath
(
"
../test/
"
));
ASSERT_EQUALS
(
"
../
"
,
Path::simplifyPath
(
"
../test/..
"
));
ASSERT_EQUALS
(
"
../
"
,
Path::simplifyPath
(
"
../test/../
"
));
ASSERT_EQUALS
(
"
/home/file.cpp
"
,
Path::simplifyPath
(
"
/home/test/../file.cpp
"
));
ASSERT_EQUALS
(
"
/file.cpp
"
,
Path::simplifyPath
(
"
/home/../test/../file.cpp
"
));
ASSERT_EQUALS
(
"
C:/home/file.cpp
"
,
Path::simplifyPath
(
"
C:/home/test/../file.cpp
"
));
ASSERT_EQUALS
(
"
C:/file.cpp
"
,
Path::simplifyPath
(
"
C:/home/../test/../file.cpp
"
));
ASSERT_EQUALS
(
"
../file.cpp
"
,
Path::simplifyPath
(
"
..
\\
file.cpp
"
));
ASSERT_EQUALS
(
"
file.cpp
"
,
Path::simplifyPath
(
"
test
\\
..
\\
file.cpp
"
));
ASSERT_EQUALS
(
"
../file.cpp
"
,
Path::simplifyPath
(
"
..
\\
test
\\
..
\\
file.cpp
"
));
ASSERT_EQUALS
(
"
file.cpp
"
,
Path::simplifyPath
(
"
.
\\
file.cpp
"
));
ASSERT_EQUALS
(
"
../file.cpp
"
,
Path::simplifyPath
(
"
.
\\
..
\\
file.cpp
"
));
ASSERT_EQUALS
(
"
file.cpp
"
,
Path::simplifyPath
(
"
.
\\
test
\\
..
\\
file.cpp
"
));
ASSERT_EQUALS
(
"
../file.cpp
"
,
Path::simplifyPath
(
"
.
\\
..
\\
test
\\
..
\\
file.cpp
"
));
ASSERT_EQUALS
(
"
test/
"
,
Path::simplifyPath
(
"
test
\\
"
));
ASSERT_EQUALS
(
"
../test/
"
,
Path::simplifyPath
(
"
..
\\
test
\\
"
));
ASSERT_EQUALS
(
"
../
"
,
Path::simplifyPath
(
"
..
\\
test
\\
..
"
));
ASSERT_EQUALS
(
"
../
"
,
Path::simplifyPath
(
"
..
\\
test
\\
..
\\
"
));
ASSERT_EQUALS
(
"
C:/home/file.cpp
"
,
Path::simplifyPath
(
"
C:
\\
home
\\
test
\\
..
\\
file.cpp
"
));
ASSERT_EQUALS
(
"
C:/file.cpp
"
,
Path::simplifyPath
(
"
C:
\\
home
\\
..
\\
test
\\
..
\\
file.cpp
"
));
ASSERT_EQUALS
(
"
//home/file.cpp
"
,
Path::simplifyPath
(
"
\\\\
home
\\
test
\\
..
\\
file.cpp
"
));
ASSERT_EQUALS
(
"
//file.cpp
"
,
Path::simplifyPath
(
"
\\\\
home
\\
..
\\
test
\\
..
\\
file.cpp
"
));
}
void
getAbsolutePath
()
const
{
const
std::string cwd =
Path::getCurrentPath
();
ScopedFile
file
(
"
testabspath.txt
"
,
"
"
);
std::string expected =
Path::toNativeSeparators
(
Path::join
(cwd,
"
testabspath.txt
"
));
ASSERT_EQUALS
(expected,
Path::getAbsoluteFilePath
(
"
testabspath.txt
"
));
ASSERT_EQUALS
(expected,
Path::getAbsoluteFilePath
(
"
./testabspath.txt
"
));
ASSERT_EQUALS
(expected,
Path::getAbsoluteFilePath
(
"
.
\\
testabspath.txt
"
));
ASSERT_EQUALS
(expected,
Path::getAbsoluteFilePath
(
"
test/../testabspath.txt
"
));
ASSERT_EQUALS
(expected,
Path::getAbsoluteFilePath
(
"
test
\\
..
\\
testabspath.txt
"
));
ASSERT_EQUALS
(expected,
Path::getAbsoluteFilePath
(
"
./test/../testabspath.txt
"
));
ASSERT_EQUALS
(expected,
Path::getAbsoluteFilePath
(
"
.
\\
test
\\
../testabspath.txt
"
));
ASSERT_EQUALS
(expected,
Path::getAbsoluteFilePath
(
Path::join
(cwd,
"
testabspath.txt
"
)));
std::string cwd_up =
Path::getPathFromFilename
(cwd);
if
(cwd_up !=
"
/
"
)
cwd_up.
pop_back
();
//
remove trailing slash
ASSERT_EQUALS
(cwd_up,
Path::getAbsoluteFilePath
(
Path::join
(cwd,
"
..
"
)));
ASSERT_EQUALS
(cwd_up,
Path::getAbsoluteFilePath
(
Path::join
(cwd,
"
../
"
)));
ASSERT_EQUALS
(cwd_up,
Path::getAbsoluteFilePath
(
Path::join
(cwd,
"
..
\\
"
)));
ASSERT_EQUALS
(cwd_up,
Path::getAbsoluteFilePath
(
Path::join
(cwd,
"
./../
"
)));
ASSERT_EQUALS
(cwd_up,
Path::getAbsoluteFilePath
(
Path::join
(cwd,
"
.
\\
..
\\
"
)));
ASSERT_EQUALS
(cwd,
Path::getAbsoluteFilePath
(
"
.
"
));
#
ifndef
_WIN32
TODO_ASSERT_EQUALS
(cwd,
"
"
,
Path::getAbsoluteFilePath
(
"
./
"
));
TODO_ASSERT_EQUALS
(cwd,
"
"
,
Path::getAbsoluteFilePath
(
"
.
\\
"
));
#
else
ASSERT_EQUALS
(cwd,
Path::getAbsoluteFilePath
(
"
./
"
));
ASSERT_EQUALS
(cwd,
Path::getAbsoluteFilePath
(
"
.
\\
"
));
#
endif
ASSERT_EQUALS
(
"
"
,
Path::getAbsoluteFilePath
(
"
"
));
#
ifndef
_WIN32
//
the underlying realpath() call only returns something if the path actually exists
ASSERT_THROW_EQUALS
(
Path::getAbsoluteFilePath
(
"
testabspath2.txt
"
), std::runtime_error,
"
path 'testabspath2.txt' does not exist
"
);
#
else
ASSERT_EQUALS
(
Path::toNativeSeparators
(
Path::join
(cwd,
"
testabspath2.txt
"
)),
Path::getAbsoluteFilePath
(
"
testabspath2.txt
"
));
#
endif
#
ifdef
_WIN32
//
determine an existing drive letter
std::string drive =
Path::getCurrentPath
().
substr
(
0
,
2
);
ASSERT_EQUALS
(drive +
"
"
,
Path::getAbsoluteFilePath
(drive +
"
\\
"
));
ASSERT_EQUALS
(drive +
"
\\
path
"
,
Path::getAbsoluteFilePath
(drive +
"
\\
path
"
));
ASSERT_EQUALS
(drive +
"
\\
path
"
,
Path::getAbsoluteFilePath
(drive +
"
\\
path
\\
"
));
ASSERT_EQUALS
(drive +
"
\\
path
\\
files.txt
"
,
Path::getAbsoluteFilePath
(drive +
"
\\
path
\\
files.txt
"
));
ASSERT_EQUALS
(drive +
"
"
,
Path::getAbsoluteFilePath
(drive +
"
//
"
));
ASSERT_EQUALS
(drive +
"
\\
path
"
,
Path::getAbsoluteFilePath
(drive +
"
//path
"
));
ASSERT_EQUALS
(drive +
"
\\
path
"
,
Path::getAbsoluteFilePath
(drive +
"
//path/
"
));
ASSERT_EQUALS
(drive +
"
\\
path
\\
files.txt
"
,
Path::getAbsoluteFilePath
(drive +
"
//path/files.txt
"
));
ASSERT_EQUALS
(drive +
"
\\
path
"
,
Path::getAbsoluteFilePath
(drive +
"
\\\\
path
"
));
ASSERT_EQUALS
(drive +
"
"
,
Path::getAbsoluteFilePath
(drive +
"
/
"
));
ASSERT_EQUALS
(drive +
"
\\
path
"
,
Path::getAbsoluteFilePath
(drive +
"
/path
"
));
drive[
0
] =
static_cast
<
char
>(
toupper
(drive[
0
]));
ASSERT_EQUALS
(drive +
"
\\
path
"
,
Path::getAbsoluteFilePath
(drive +
"
\\
path
"
));
ASSERT_EQUALS
(drive +
"
\\
path
"
,
Path::getAbsoluteFilePath
(drive +
"
/path
"
));
drive[
0
] =
static_cast
<
char
>(
tolower
(drive[
0
]));
ASSERT_EQUALS
(drive +
"
\\
path
"
,
Path::getAbsoluteFilePath
(drive +
"
\\
path
"
));
ASSERT_EQUALS
(drive +
"
\\
path
"
,
Path::getAbsoluteFilePath
(drive +
"
/path
"
));
ASSERT_EQUALS
(
"
1:
\\
path
\\
files.txt
"
,
Path::getAbsoluteFilePath
(
"
1:
\\
path
\\
files.txt
"
));
//
treated as valid drive
ASSERT_EQUALS
(
Path::toNativeSeparators
(
Path::join
(
Path::getCurrentPath
(),
"
CC:
\\
path
\\
files.txt
"
)),
Path::getAbsoluteFilePath
(
"
CC:
\\
path
\\
files.txt
"
));
//
treated as filename
ASSERT_EQUALS
(
"
1:
\\
path
\\
files.txt
"
,
Path::getAbsoluteFilePath
(
"
1:/path/files.txt
"
));
//
treated as valid drive
ASSERT_EQUALS
(
Path::toNativeSeparators
(
Path::join
(
Path::getCurrentPath
(),
"
CC:
\\
path
\\
files.txt
"
)),
Path::getAbsoluteFilePath
(
"
CC:/path/files.txt
"
));
//
treated as filename
#
endif
#
ifndef
_WIN32
ASSERT_THROW_EQUALS
(
Path::getAbsoluteFilePath
(
"
C:
\\
path
\\
files.txt
"
), std::runtime_error,
"
path 'C:
\\
path
\\
files.txt' does not exist
"
);
#
endif
//
TODO: test UNC paths
//
TODO: test with symlinks
}
void
exists
()
const
{
ScopedFile
file
(
"
testpath.txt
"
,
"
"
,
"
testpath
"
);
ScopedFile
file2
(
"
testpath2.txt
"
,
"
"
);
ASSERT_EQUALS
(
true
,
Path::exists
(
"
testpath
"
));
ASSERT_EQUALS
(
true
,
Path::exists
(
"
testpath2.txt
"
));
ASSERT_EQUALS
(
false
,
Path::exists
(
"
testpath2
"
));
bool
b =
false
;
ASSERT_EQUALS
(
true
,
Path::exists
(
"
testpath
"
, &b));
ASSERT_EQUALS
(
true
, b);
ASSERT_EQUALS
(
true
,
Path::exists
(
"
testpath/testpath.txt
"
, &b));
ASSERT_EQUALS
(
false
, b);
ASSERT_EQUALS
(
true
,
Path::exists
(
"
testpath2.txt
"
, &b));
ASSERT_EQUALS
(
false
, b);
ASSERT_EQUALS
(
false
,
Path::exists
(
"
testpath2
"
, &b));
ASSERT_EQUALS
(
false
, b);
ASSERT_EQUALS
(
false
,
Path::exists
(
"
testpath/testpath2.txt
"
, &b));
ASSERT_EQUALS
(
false
, b);
ASSERT_EQUALS
(
false
,
Path::exists
(
"
testpath.txt
"
, &b));
ASSERT_EQUALS
(
false
, b);
}
void
fromNativeSeparators
()
const
{
ASSERT_EQUALS
(
"
lib/file.c
"
,
Path::fromNativeSeparators
(
"
lib/file.c
"
));
ASSERT_EQUALS
(
"
lib//file.c
"
,
Path::fromNativeSeparators
(
"
lib//file.c
"
));
ASSERT_EQUALS
(
"
/lib/file.c
"
,
Path::fromNativeSeparators
(
"
/lib/file.c
"
));
ASSERT_EQUALS
(
"
//lib/file.c
"
,
Path::fromNativeSeparators
(
"
//lib/file.c
"
));
ASSERT_EQUALS
(
"
./lib/file.c
"
,
Path::fromNativeSeparators
(
"
./lib/file.c
"
));
ASSERT_EQUALS
(
"
lib/file.c
"
,
Path::fromNativeSeparators
(
"
lib
\\
file.c
"
));
ASSERT_EQUALS
(
"
lib//file.c
"
,
Path::fromNativeSeparators
(
"
lib
\\\\
file.c
"
));
ASSERT_EQUALS
(
"
/lib/file.c
"
,
Path::fromNativeSeparators
(
"
\\
lib
\\
file.c
"
));
ASSERT_EQUALS
(
"
//lib/file.c
"
,
Path::fromNativeSeparators
(
"
\\\\
lib
\\
file.c
"
));
ASSERT_EQUALS
(
"
./lib/file.c
"
,
Path::fromNativeSeparators
(
"
.
\\
lib
\\
file.c
"
));
}
void
isRelative
()
const
{
ASSERT_EQUALS
(
true
,
Path::isRelative
(
"
dir/file
"
));
ASSERT_EQUALS
(
true
,
Path::isRelative
(
"
dir
\\
file
"
));
//
TODO: is this expected?
ASSERT_EQUALS
(
true
,
Path::isRelative
(
"
file/
"
));
ASSERT_EQUALS
(
true
,
Path::isRelative
(
"
file
\\
"
));
ASSERT_EQUALS
(
false
,
Path::isRelative
(
"
file
"
));
#
ifdef
_WIN32
//
this is a relative path on Windows
ASSERT_EQUALS
(
true
,
Path::isRelative
(
"
/dir/file
"
));
#
else
ASSERT_EQUALS
(
false
,
Path::isRelative
(
"
/dir/file
"
));
#
endif
#
ifdef
_WIN32
//
TODO: this is not detected as absolute path in _WIN32 builds
ASSERT_EQUALS
(
false
,
Path::isRelative
(
"
c:
\\
dir
\\
file
"
));
ASSERT_EQUALS
(
false
,
Path::isRelative
(
"
c:/dir/file
"
));
#
else
ASSERT_EQUALS
(
true
,
Path::isRelative
(
"
c:
\\
dir
\\
file
"
));
ASSERT_EQUALS
(
true
,
Path::isRelative
(
"
c:/dir/file
"
));
#
endif
}
};
REGISTER_TEST
(TestPath)
Back
|
FazBrowse Home
|
New Git URL