OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkOSFile.h" | 8 #include "SkOSFile.h" |
9 #include "SkString.h" | 9 #include "SkString.h" |
10 #include "Test.h" | 10 #include "Test.h" |
11 | 11 |
12 /** | 12 /** |
13 * Test SkOSPath::Join and SkOSPath::Basename. | 13 * Test SkOSPath::Join, SkOSPath::Basename, and SkOSPath::Dirname. |
14 * Will use SkOSPath::Join to append filename to dir, test that it works correc
tly, | 14 * Will use SkOSPath::Join to append filename to dir, test that it works correc
tly, |
15 * and tests using SkOSPath::Basename on the result. | 15 * and tests using SkOSPath::Basename on the result. |
16 * @param reporter Reporter for test conditions. | 16 * @param reporter Reporter for test conditions. |
17 * @param dir String representing the path to a folder. May or may not | 17 * @param dir String representing the path to a folder. May or may not |
18 * end with SkPATH_SEPARATOR. | 18 * end with SkPATH_SEPARATOR. |
19 * @param filename String representing the basename of a file. Must NOT | 19 * @param filename String representing the basename of a file. Must NOT |
20 * contain SkPATH_SEPARATOR. | 20 * contain SkPATH_SEPARATOR. |
21 */ | 21 */ |
22 static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir, | 22 static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir, |
23 SkString filename) { | 23 SkString filename) { |
24 // If filename contains SkPATH_SEPARATOR, the tests will fail. | 24 // If filename contains SkPATH_SEPARATOR, the tests will fail. |
25 SkASSERT(!filename.contains(SkPATH_SEPARATOR)); | 25 SkASSERT(!filename.contains(SkPATH_SEPARATOR)); |
26 | 26 |
27 // Tests for SkOSPath::Join and SkOSPath::Basename | 27 // Tests for SkOSPath::Join and SkOSPath::Basename |
28 | 28 |
29 // fullName should be "dir<SkPATH_SEPARATOR>file" | 29 // fullName should be "dir<SkPATH_SEPARATOR>file" |
30 SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str()); | 30 SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str()); |
31 | 31 |
32 // fullName should be the combined size of dir and file, plus one if | 32 // fullName should be the combined size of dir and file, plus one if |
33 // dir did not include the final path separator. | 33 // dir did not include the final path separator. |
34 size_t expectedSize = dir.size() + filename.size(); | 34 size_t expectedSize = dir.size() + filename.size(); |
35 if (!dir.endsWith(SkPATH_SEPARATOR)) { | 35 if (!dir.endsWith(SkPATH_SEPARATOR) && !dir.isEmpty()) { |
36 expectedSize++; | 36 expectedSize++; |
37 } | 37 } |
38 REPORTER_ASSERT(reporter, fullName.size() == expectedSize); | 38 REPORTER_ASSERT(reporter, fullName.size() == expectedSize); |
39 | 39 |
40 SkString basename = SkOSPath::Basename(fullName.c_str()); | 40 SkString basename = SkOSPath::Basename(fullName.c_str()); |
| 41 SkString dirname = SkOSPath::Dirname(fullName.c_str()); |
41 | 42 |
42 // basename should be the same as filename | 43 // basename should be the same as filename |
43 REPORTER_ASSERT(reporter, basename.equals(filename)); | 44 REPORTER_ASSERT(reporter, basename.equals(filename)); |
44 | 45 |
| 46 // dirname should be the same as dir with any trailing seperators removed. |
| 47 // Except when the the string is just "/". |
| 48 SkString strippedDir = dir; |
| 49 while (strippedDir.size() > 2 && strippedDir[strippedDir.size() - 1] == SkPA
TH_SEPARATOR) { |
| 50 strippedDir.remove(strippedDir.size() - 1, 1); |
| 51 } |
| 52 if (!dirname.equals(strippedDir)) { |
| 53 SkDebugf("OOUCH %s %s %s\n", dir.c_str(), strippedDir.c_str(), dirname.c
_str()); |
| 54 } |
| 55 REPORTER_ASSERT(reporter, dirname.equals(strippedDir)); |
| 56 |
45 // basename will not contain a path separator | 57 // basename will not contain a path separator |
46 REPORTER_ASSERT(reporter, !basename.contains(SkPATH_SEPARATOR)); | 58 REPORTER_ASSERT(reporter, !basename.contains(SkPATH_SEPARATOR)); |
47 | 59 |
48 // Now take the basename of filename, which should be the same as filename. | 60 // Now take the basename of filename, which should be the same as filename. |
49 basename = SkOSPath::Basename(filename.c_str()); | 61 basename = SkOSPath::Basename(filename.c_str()); |
50 REPORTER_ASSERT(reporter, basename.equals(filename)); | 62 REPORTER_ASSERT(reporter, basename.equals(filename)); |
51 } | 63 } |
52 | 64 |
53 DEF_TEST(OSPath, reporter) { | 65 DEF_TEST(OSPath, reporter) { |
54 SkString dir("dir"); | 66 SkString dir("dir"); |
(...skipping 16 matching lines...) Expand all Loading... |
71 | 83 |
72 // Basename of a directory with a path separator at the end is empty. | 84 // Basename of a directory with a path separator at the end is empty. |
73 dir.appendUnichar(SkPATH_SEPARATOR); | 85 dir.appendUnichar(SkPATH_SEPARATOR); |
74 SkString baseOfDir = SkOSPath::Basename(dir.c_str()); | 86 SkString baseOfDir = SkOSPath::Basename(dir.c_str()); |
75 REPORTER_ASSERT(reporter, baseOfDir.size() == 0); | 87 REPORTER_ASSERT(reporter, baseOfDir.size() == 0); |
76 | 88 |
77 // Basename of NULL is an empty string. | 89 // Basename of NULL is an empty string. |
78 SkString empty = SkOSPath::Basename(NULL); | 90 SkString empty = SkOSPath::Basename(NULL); |
79 REPORTER_ASSERT(reporter, empty.size() == 0); | 91 REPORTER_ASSERT(reporter, empty.size() == 0); |
80 | 92 |
| 93 // File in root dir |
| 94 dir.printf("%c", SkPATH_SEPARATOR); |
| 95 filename.set("file"); |
| 96 test_dir_with_file(reporter, dir, filename); |
| 97 |
| 98 // Just the root dir |
| 99 filename.reset(); |
| 100 test_dir_with_file(reporter, dir, filename); |
| 101 |
81 // Test that NULL can be used for the directory and filename. | 102 // Test that NULL can be used for the directory and filename. |
82 SkString emptyPath = SkOSPath::Join(NULL, NULL); | 103 SkString emptyPath = SkOSPath::Join(NULL, NULL); |
83 REPORTER_ASSERT(reporter, emptyPath.size() == 1); | 104 REPORTER_ASSERT(reporter, emptyPath.isEmpty()); |
84 REPORTER_ASSERT(reporter, emptyPath.contains(SkPATH_SEPARATOR)); | |
85 } | 105 } |
OLD | NEW |