aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/OSPathTest.cpp
diff options
context:
space:
mode:
authorGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-05-28 16:45:07 +0000
committerGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-05-28 16:45:07 +0000
commitccd7afb6fb2df9774e57fb4d7f62f9504cabf03e (patch)
tree54818885b59f98ac958e6c35d67109b098f26a19 /tests/OSPathTest.cpp
parent7b06ba45b34855a5bb16f9c729813173d7dfe47d (diff)
Reland 'Add path utils, plus a test for it.'
Build SkPathJoin and SkBasename on windows also. Previous CL did not build on Windows because the two functions were accidentally placed inside an ifdef that did not include windows. Move the functions to the top of the file, and add a comment by the endif for clarity. Previously reviewed at https://codereview.chromium.org/15747004/ Review URL: https://codereview.chromium.org/15740024 git-svn-id: http://skia.googlecode.com/svn/trunk@9295 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tests/OSPathTest.cpp')
-rw-r--r--tests/OSPathTest.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/OSPathTest.cpp b/tests/OSPathTest.cpp
new file mode 100644
index 0000000000..96ff8a712d
--- /dev/null
+++ b/tests/OSPathTest.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkString.h"
+#include "SkOSFile.h"
+#include "Test.h"
+
+/**
+ * Test SkPathJoin and SkBasename.
+ * Will use SkPathJoin to append filename to dir, test that it works correctly,
+ * and tests using SkBasename on the result.
+ * @param reporter Reporter for test conditions.
+ * @param dir String representing the path to a folder. May or may not
+ * end with SkPATH_SEPARATOR.
+ * @param filename String representing the basename of a file. Must NOT
+ * contain SkPATH_SEPARATOR.
+ */
+static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir,
+ SkString filename) {
+ // If filename contains SkPATH_SEPARATOR, the tests will fail.
+ SkASSERT(!filename.contains(SkPATH_SEPARATOR));
+
+ // Tests for SkOSPath::SkPathJoin and SkOSPath::SkBasename
+
+ // fullName should be "dir<SkPATH_SEPARATOR>file"
+ SkString fullName = SkOSPath::SkPathJoin(dir.c_str(), filename.c_str());
+
+ // 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();
+ if (!dir.endsWith(SkPATH_SEPARATOR)) {
+ expectedSize++;
+ }
+ REPORTER_ASSERT(reporter, fullName.size() == expectedSize);
+
+ SkString basename = SkOSPath::SkBasename(fullName.c_str());
+
+ // basename should be the same as filename
+ REPORTER_ASSERT(reporter, basename.equals(filename));
+
+ // basename will not contain a path separator
+ REPORTER_ASSERT(reporter, !basename.contains(SkPATH_SEPARATOR));
+
+ // Now take the basename of filename, which should be the same as filename.
+ basename = SkOSPath::SkBasename(filename.c_str());
+ REPORTER_ASSERT(reporter, basename.equals(filename));
+}
+
+static void test_os_path_utils_tests(skiatest::Reporter* reporter) {
+ 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.
+ dir.appendUnichar(SkPATH_SEPARATOR);
+ test_dir_with_file(reporter, dir, filename);
+
+ // 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.
+ dir.appendUnichar(SkPATH_SEPARATOR);
+ SkString baseOfDir = SkOSPath::SkBasename(dir.c_str());
+ REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
+
+ // Basename of NULL is an empty string.
+ SkString empty = SkOSPath::SkBasename(NULL);
+ REPORTER_ASSERT(reporter, empty.size() == 0);
+}
+
+#include "TestClassDef.h"
+DEFINE_TESTCLASS("OSPath", OSPathTestClass, test_os_path_utils_tests)