scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 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 | |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 8 | #include "SkOSPath.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "SkString.h" |
| 10 | #include "Test.h" |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 11 | |
| 12 | /** |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 13 | * Test SkOSPath::Join, SkOSPath::Basename, and SkOSPath::Dirname. |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 14 | * Will use SkOSPath::Join to append filename to dir, test that it works correctly, |
| 15 | * and tests using SkOSPath::Basename on the result. |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 16 | * @param reporter Reporter for test conditions. |
| 17 | * @param dir String representing the path to a folder. May or may not |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 18 | * end with SkOSPath::SEPARATOR. |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 19 | * @param filename String representing the basename of a file. Must NOT |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 20 | * contain SkOSPath::SEPARATOR. |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 21 | */ |
| 22 | static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir, |
| 23 | SkString filename) { |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 24 | // If filename contains SkOSPath::SEPARATOR, the tests will fail. |
| 25 | SkASSERT(!filename.contains(SkOSPath::SEPARATOR)); |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 26 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 27 | // Tests for SkOSPath::Join and SkOSPath::Basename |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 28 | |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 29 | // fullName should be "dir<SkOSPath::SEPARATOR>file" |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 30 | SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str()); |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 31 | |
| 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(); |
Cary Clark | 5ef4ebf | 2018-01-05 18:21:48 +0000 | [diff] [blame] | 35 | if (!dir.endsWith(SkOSPath::SEPARATOR) && !dir.isEmpty()) { |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 36 | expectedSize++; |
| 37 | } |
| 38 | REPORTER_ASSERT(reporter, fullName.size() == expectedSize); |
| 39 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 40 | SkString basename = SkOSPath::Basename(fullName.c_str()); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 41 | SkString dirname = SkOSPath::Dirname(fullName.c_str()); |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 42 | |
| 43 | // basename should be the same as filename |
| 44 | REPORTER_ASSERT(reporter, basename.equals(filename)); |
| 45 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 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; |
Cary Clark | 5ef4ebf | 2018-01-05 18:21:48 +0000 | [diff] [blame] | 49 | while (strippedDir.size() > 2 && strippedDir[strippedDir.size() - 1] == SkOSPath::SEPARATOR) { |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 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 | |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 57 | // basename will not contain a path separator |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, !basename.contains(SkOSPath::SEPARATOR)); |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 59 | |
| 60 | // Now take the basename of filename, which should be the same as filename. |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 61 | basename = SkOSPath::Basename(filename.c_str()); |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, basename.equals(filename)); |
| 63 | } |
| 64 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 65 | DEF_TEST(OSPath, reporter) { |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 66 | SkString dir("dir"); |
| 67 | SkString filename("file"); |
| 68 | test_dir_with_file(reporter, dir, filename); |
| 69 | |
| 70 | // Now make sure this works with a path separator at the end of dir. |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 71 | dir.appendUnichar(SkOSPath::SEPARATOR); |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 72 | test_dir_with_file(reporter, dir, filename); |
| 73 | |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 74 | // Test using no filename. |
| 75 | test_dir_with_file(reporter, dir, SkString()); |
| 76 | |
| 77 | // Testing using no directory. |
| 78 | test_dir_with_file(reporter, SkString(), filename); |
| 79 | |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 80 | // Test with a sub directory. |
| 81 | dir.append("subDir"); |
| 82 | test_dir_with_file(reporter, dir, filename); |
| 83 | |
| 84 | // Basename of a directory with a path separator at the end is empty. |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 85 | dir.appendUnichar(SkOSPath::SEPARATOR); |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 86 | SkString baseOfDir = SkOSPath::Basename(dir.c_str()); |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 87 | REPORTER_ASSERT(reporter, baseOfDir.size() == 0); |
| 88 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 89 | // Basename of nullptr is an empty string. |
| 90 | SkString empty = SkOSPath::Basename(nullptr); |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, empty.size() == 0); |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 92 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 93 | // File in root dir |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 94 | dir.printf("%c", SkOSPath::SEPARATOR); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 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 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 102 | // Test that nullptr can be used for the directory and filename. |
| 103 | SkString emptyPath = SkOSPath::Join(nullptr, nullptr); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, emptyPath.isEmpty()); |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 105 | } |