2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
13 * Test SkOSPath::Join, SkOSPath::Basename, and SkOSPath::Dirname.
14 * Will use SkOSPath::Join to append filename to dir, test that it works correctly,
15 * and tests using SkOSPath::Basename on the result.
16 * @param reporter Reporter for test conditions.
17 * @param dir String representing the path to a folder. May or may not
18 * end with SkPATH_SEPARATOR.
19 * @param filename String representing the basename of a file. Must NOT
20 * contain SkPATH_SEPARATOR.
22 static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir,
24 // If filename contains SkPATH_SEPARATOR, the tests will fail.
25 SkASSERT(!filename.contains(SkPATH_SEPARATOR));
27 // Tests for SkOSPath::Join and SkOSPath::Basename
29 // fullName should be "dir<SkPATH_SEPARATOR>file"
30 SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str());
32 // fullName should be the combined size of dir and file, plus one if
33 // dir did not include the final path separator.
34 size_t expectedSize = dir.size() + filename.size();
35 if (!dir.endsWith(SkPATH_SEPARATOR) && !dir.isEmpty()) {
38 REPORTER_ASSERT(reporter, fullName.size() == expectedSize);
40 SkString basename = SkOSPath::Basename(fullName.c_str());
41 SkString dirname = SkOSPath::Dirname(fullName.c_str());
43 // basename should be the same as filename
44 REPORTER_ASSERT(reporter, basename.equals(filename));
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] == SkPATH_SEPARATOR) {
50 strippedDir.remove(strippedDir.size() - 1, 1);
52 if (!dirname.equals(strippedDir)) {
53 SkDebugf("OOUCH %s %s %s\n", dir.c_str(), strippedDir.c_str(), dirname.c_str());
55 REPORTER_ASSERT(reporter, dirname.equals(strippedDir));
57 // basename will not contain a path separator
58 REPORTER_ASSERT(reporter, !basename.contains(SkPATH_SEPARATOR));
60 // Now take the basename of filename, which should be the same as filename.
61 basename = SkOSPath::Basename(filename.c_str());
62 REPORTER_ASSERT(reporter, basename.equals(filename));
65 DEF_TEST(OSPath, reporter) {
67 SkString filename("file");
68 test_dir_with_file(reporter, dir, filename);
70 // Now make sure this works with a path separator at the end of dir.
71 dir.appendUnichar(SkPATH_SEPARATOR);
72 test_dir_with_file(reporter, dir, filename);
74 // Test using no filename.
75 test_dir_with_file(reporter, dir, SkString());
77 // Testing using no directory.
78 test_dir_with_file(reporter, SkString(), filename);
80 // Test with a sub directory.
82 test_dir_with_file(reporter, dir, filename);
84 // Basename of a directory with a path separator at the end is empty.
85 dir.appendUnichar(SkPATH_SEPARATOR);
86 SkString baseOfDir = SkOSPath::Basename(dir.c_str());
87 REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
89 // Basename of NULL is an empty string.
90 SkString empty = SkOSPath::Basename(NULL);
91 REPORTER_ASSERT(reporter, empty.size() == 0);
94 dir.printf("%c", SkPATH_SEPARATOR);
96 test_dir_with_file(reporter, dir, filename);
100 test_dir_with_file(reporter, dir, filename);
102 // Test that NULL can be used for the directory and filename.
103 SkString emptyPath = SkOSPath::Join(NULL, NULL);
104 REPORTER_ASSERT(reporter, emptyPath.isEmpty());