OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkString.h" |
| 9 #include "SkOSFile.h" |
| 10 #include "SkOSPathUtils.h" |
| 11 #include "Test.h" |
| 12 |
| 13 /** |
| 14 * Test SkPathJoin and SkBasename. |
| 15 * Will use SkPathJoin to append filename to dir, test that it works |
| 16 * correctly, and tests using SkBasename on the result. |
| 17 * @param reporter Reporter for test conditions. |
| 18 * @param dir String representing the path to a folder. May or may not |
| 19 * end with SkPATH_SEPARATOR. |
| 20 * @param filename String representing the basename of a file. Must NOT |
| 21 * contain SkPATH_SEPARATOR. |
| 22 */ |
| 23 static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir, |
| 24 SkString filename) { |
| 25 // If filename contains SkPATH_SEPARATOR, the tests will fail. |
| 26 SkASSERT(!filename.contains(SkPATH_SEPARATOR)); |
| 27 |
| 28 // Tests for SkPathJoin and SkBasename |
| 29 |
| 30 // fullName should be "dir<SkPATH_SEPARATOR>file" |
| 31 SkString fullName = SkPathJoin(dir.c_str(), filename.c_str()); |
| 32 |
| 33 // fullName should be the combined size of dir and file, plus one if |
| 34 // dir did not include the final path separator. |
| 35 size_t expectedSize = dir.size() + filename.size(); |
| 36 if (!dir.endsWith(SkPATH_SEPARATOR)) { |
| 37 expectedSize++; |
| 38 } |
| 39 REPORTER_ASSERT(reporter, fullName.size() == expectedSize); |
| 40 |
| 41 SkString basename = SkBasename(fullName.c_str()); |
| 42 |
| 43 // basename should be the same as filename |
| 44 REPORTER_ASSERT(reporter, basename.equals(filename)); |
| 45 |
| 46 // basename will not contain a path separator |
| 47 REPORTER_ASSERT(reporter, !basename.contains(SkPATH_SEPARATOR)); |
| 48 |
| 49 // Now take the basename of filename, which should be the same as filename. |
| 50 basename = SkBasename(filename.c_str()); |
| 51 REPORTER_ASSERT(reporter, basename.equals(filename)); |
| 52 } |
| 53 |
| 54 static void test_os_path_utils_tests(skiatest::Reporter* reporter) { |
| 55 SkString dir("dir"); |
| 56 SkString filename("file"); |
| 57 test_dir_with_file(reporter, dir, filename); |
| 58 |
| 59 // Now make sure this works with a path separator at the end of dir. |
| 60 dir.appendUnichar(SkPATH_SEPARATOR); |
| 61 test_dir_with_file(reporter, dir, filename); |
| 62 |
| 63 // Test with a sub directory. |
| 64 dir.append("subDir"); |
| 65 test_dir_with_file(reporter, dir, filename); |
| 66 |
| 67 // Basename of a directory with a path separator at the end is empty. |
| 68 dir.appendUnichar(SkPATH_SEPARATOR); |
| 69 SkString baseOfDir = SkBasename(dir.c_str()); |
| 70 REPORTER_ASSERT(reporter, baseOfDir.size() == 0); |
| 71 |
| 72 // Basename of NULL is an empty string. |
| 73 SkString empty = SkBasename(NULL); |
| 74 REPORTER_ASSERT(reporter, empty.size() == 0); |
| 75 } |
| 76 |
| 77 #include "TestClassDef.h" |
| 78 DEFINE_TESTCLASS("OSPathUtils", OSPathUtilsTestClass, test_os_path_utils_tests) |
OLD | NEW |