2013-05-28 16:45:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "src/utils/SkOSPath.h"
|
|
|
|
#include "tests/Test.h"
|
2013-05-28 16:45:07 +00:00
|
|
|
|
|
|
|
/**
|
2014-08-07 21:28:50 +00:00
|
|
|
* Test SkOSPath::Join, SkOSPath::Basename, and SkOSPath::Dirname.
|
2014-07-29 02:26:58 +00:00
|
|
|
* Will use SkOSPath::Join to append filename to dir, test that it works correctly,
|
|
|
|
* and tests using SkOSPath::Basename on the result.
|
2013-05-28 16:45:07 +00:00
|
|
|
* @param reporter Reporter for test conditions.
|
|
|
|
* @param dir String representing the path to a folder. May or may not
|
2016-11-07 23:05:29 +00:00
|
|
|
* end with SkOSPath::SEPARATOR.
|
2013-05-28 16:45:07 +00:00
|
|
|
* @param filename String representing the basename of a file. Must NOT
|
2016-11-07 23:05:29 +00:00
|
|
|
* contain SkOSPath::SEPARATOR.
|
2013-05-28 16:45:07 +00:00
|
|
|
*/
|
|
|
|
static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir,
|
|
|
|
SkString filename) {
|
2016-11-07 23:05:29 +00:00
|
|
|
// If filename contains SkOSPath::SEPARATOR, the tests will fail.
|
|
|
|
SkASSERT(!filename.contains(SkOSPath::SEPARATOR));
|
2013-05-28 16:45:07 +00:00
|
|
|
|
2014-07-29 02:26:58 +00:00
|
|
|
// Tests for SkOSPath::Join and SkOSPath::Basename
|
2013-05-28 16:45:07 +00:00
|
|
|
|
2016-11-07 23:05:29 +00:00
|
|
|
// fullName should be "dir<SkOSPath::SEPARATOR>file"
|
2014-07-29 02:26:58 +00:00
|
|
|
SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str());
|
2013-05-28 16:45:07 +00:00
|
|
|
|
|
|
|
// fullName should be the combined size of dir and file, plus one if
|
|
|
|
// dir did not include the final path separator.
|
|
|
|
size_t expectedSize = dir.size() + filename.size();
|
2018-01-05 18:21:48 +00:00
|
|
|
if (!dir.endsWith(SkOSPath::SEPARATOR) && !dir.isEmpty()) {
|
2013-05-28 16:45:07 +00:00
|
|
|
expectedSize++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, fullName.size() == expectedSize);
|
|
|
|
|
2014-07-29 02:26:58 +00:00
|
|
|
SkString basename = SkOSPath::Basename(fullName.c_str());
|
2014-08-07 21:28:50 +00:00
|
|
|
SkString dirname = SkOSPath::Dirname(fullName.c_str());
|
2013-05-28 16:45:07 +00:00
|
|
|
|
|
|
|
// basename should be the same as filename
|
|
|
|
REPORTER_ASSERT(reporter, basename.equals(filename));
|
|
|
|
|
2014-08-07 21:28:50 +00:00
|
|
|
// dirname should be the same as dir with any trailing seperators removed.
|
|
|
|
// Except when the the string is just "/".
|
|
|
|
SkString strippedDir = dir;
|
2018-01-05 18:21:48 +00:00
|
|
|
while (strippedDir.size() > 2 && strippedDir[strippedDir.size() - 1] == SkOSPath::SEPARATOR) {
|
2014-08-07 21:28:50 +00:00
|
|
|
strippedDir.remove(strippedDir.size() - 1, 1);
|
|
|
|
}
|
|
|
|
if (!dirname.equals(strippedDir)) {
|
|
|
|
SkDebugf("OOUCH %s %s %s\n", dir.c_str(), strippedDir.c_str(), dirname.c_str());
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, dirname.equals(strippedDir));
|
|
|
|
|
2013-05-28 16:45:07 +00:00
|
|
|
// basename will not contain a path separator
|
2016-11-07 23:05:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, !basename.contains(SkOSPath::SEPARATOR));
|
2013-05-28 16:45:07 +00:00
|
|
|
|
|
|
|
// Now take the basename of filename, which should be the same as filename.
|
2014-07-29 02:26:58 +00:00
|
|
|
basename = SkOSPath::Basename(filename.c_str());
|
2013-05-28 16:45:07 +00:00
|
|
|
REPORTER_ASSERT(reporter, basename.equals(filename));
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(OSPath, reporter) {
|
2013-05-28 16:45:07 +00:00
|
|
|
SkString dir("dir");
|
|
|
|
SkString filename("file");
|
|
|
|
test_dir_with_file(reporter, dir, filename);
|
|
|
|
|
|
|
|
// Now make sure this works with a path separator at the end of dir.
|
2016-11-07 23:05:29 +00:00
|
|
|
dir.appendUnichar(SkOSPath::SEPARATOR);
|
2013-05-28 16:45:07 +00:00
|
|
|
test_dir_with_file(reporter, dir, filename);
|
|
|
|
|
2013-06-06 14:59:56 +00:00
|
|
|
// Test using no filename.
|
|
|
|
test_dir_with_file(reporter, dir, SkString());
|
|
|
|
|
|
|
|
// Testing using no directory.
|
|
|
|
test_dir_with_file(reporter, SkString(), filename);
|
|
|
|
|
2013-05-28 16:45:07 +00:00
|
|
|
// Test with a sub directory.
|
|
|
|
dir.append("subDir");
|
|
|
|
test_dir_with_file(reporter, dir, filename);
|
|
|
|
|
|
|
|
// Basename of a directory with a path separator at the end is empty.
|
2016-11-07 23:05:29 +00:00
|
|
|
dir.appendUnichar(SkOSPath::SEPARATOR);
|
2014-07-29 02:26:58 +00:00
|
|
|
SkString baseOfDir = SkOSPath::Basename(dir.c_str());
|
2013-05-28 16:45:07 +00:00
|
|
|
REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
// Basename of nullptr is an empty string.
|
|
|
|
SkString empty = SkOSPath::Basename(nullptr);
|
2013-05-28 16:45:07 +00:00
|
|
|
REPORTER_ASSERT(reporter, empty.size() == 0);
|
2013-06-06 14:59:56 +00:00
|
|
|
|
2014-08-07 21:28:50 +00:00
|
|
|
// File in root dir
|
2016-11-07 23:05:29 +00:00
|
|
|
dir.printf("%c", SkOSPath::SEPARATOR);
|
2014-08-07 21:28:50 +00:00
|
|
|
filename.set("file");
|
|
|
|
test_dir_with_file(reporter, dir, filename);
|
|
|
|
|
|
|
|
// Just the root dir
|
|
|
|
filename.reset();
|
|
|
|
test_dir_with_file(reporter, dir, filename);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
// Test that nullptr can be used for the directory and filename.
|
|
|
|
SkString emptyPath = SkOSPath::Join(nullptr, nullptr);
|
2014-08-07 21:28:50 +00:00
|
|
|
REPORTER_ASSERT(reporter, emptyPath.isEmpty());
|
2013-05-28 16:45:07 +00:00
|
|
|
}
|