Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1540)

Unified Diff: tests/OSPathUtilsTest.cpp

Issue 15747004: Add path utils, plus a test for it. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Remove unnecessary changes. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tests/OSPathUtilsTest.cpp
diff --git a/tests/OSPathUtilsTest.cpp b/tests/OSPathUtilsTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..d14ff1af9ae2cc2d191f8964ae43aa5a373d1ef4
--- /dev/null
+++ b/tests/OSPathUtilsTest.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 SkOSPathUtils::SkPathJoin and SkOSPathUtils::SkBasename
+
+ // fullName should be "dir<SkPATH_SEPARATOR>file"
+ SkString fullName = SkOSPathUtils::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 = SkOSPathUtils::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 = SkOSPathUtils::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 = SkOSPathUtils::SkBasename(dir.c_str());
+ REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
+
+ // Basename of NULL is an empty string.
+ SkString empty = SkOSPathUtils::SkBasename(NULL);
+ REPORTER_ASSERT(reporter, empty.size() == 0);
+}
+
+#include "TestClassDef.h"
+DEFINE_TESTCLASS("OSPathUtils", OSPathUtilsTestClass, test_os_path_utils_tests)
« src/utils/SkOSFile.cpp ('K') | « src/utils/SkOSFile.cpp ('k') | tools/skimage_main.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698