OSPathTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include "include/core/SkString.h"
  8. #include "src/utils/SkOSPath.h"
  9. #include "tests/Test.h"
  10. /**
  11. * Test SkOSPath::Join, SkOSPath::Basename, and SkOSPath::Dirname.
  12. * Will use SkOSPath::Join to append filename to dir, test that it works correctly,
  13. * and tests using SkOSPath::Basename on the result.
  14. * @param reporter Reporter for test conditions.
  15. * @param dir String representing the path to a folder. May or may not
  16. * end with SkOSPath::SEPARATOR.
  17. * @param filename String representing the basename of a file. Must NOT
  18. * contain SkOSPath::SEPARATOR.
  19. */
  20. static void test_dir_with_file(skiatest::Reporter* reporter, SkString dir,
  21. SkString filename) {
  22. // If filename contains SkOSPath::SEPARATOR, the tests will fail.
  23. SkASSERT(!filename.contains(SkOSPath::SEPARATOR));
  24. // Tests for SkOSPath::Join and SkOSPath::Basename
  25. // fullName should be "dir<SkOSPath::SEPARATOR>file"
  26. SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str());
  27. // fullName should be the combined size of dir and file, plus one if
  28. // dir did not include the final path separator.
  29. size_t expectedSize = dir.size() + filename.size();
  30. if (!dir.endsWith(SkOSPath::SEPARATOR) && !dir.isEmpty()) {
  31. expectedSize++;
  32. }
  33. REPORTER_ASSERT(reporter, fullName.size() == expectedSize);
  34. SkString basename = SkOSPath::Basename(fullName.c_str());
  35. SkString dirname = SkOSPath::Dirname(fullName.c_str());
  36. // basename should be the same as filename
  37. REPORTER_ASSERT(reporter, basename.equals(filename));
  38. // dirname should be the same as dir with any trailing seperators removed.
  39. // Except when the the string is just "/".
  40. SkString strippedDir = dir;
  41. while (strippedDir.size() > 2 && strippedDir[strippedDir.size() - 1] == SkOSPath::SEPARATOR) {
  42. strippedDir.remove(strippedDir.size() - 1, 1);
  43. }
  44. if (!dirname.equals(strippedDir)) {
  45. SkDebugf("OOUCH %s %s %s\n", dir.c_str(), strippedDir.c_str(), dirname.c_str());
  46. }
  47. REPORTER_ASSERT(reporter, dirname.equals(strippedDir));
  48. // basename will not contain a path separator
  49. REPORTER_ASSERT(reporter, !basename.contains(SkOSPath::SEPARATOR));
  50. // Now take the basename of filename, which should be the same as filename.
  51. basename = SkOSPath::Basename(filename.c_str());
  52. REPORTER_ASSERT(reporter, basename.equals(filename));
  53. }
  54. DEF_TEST(OSPath, reporter) {
  55. SkString dir("dir");
  56. SkString filename("file");
  57. test_dir_with_file(reporter, dir, filename);
  58. // Now make sure this works with a path separator at the end of dir.
  59. dir.appendUnichar(SkOSPath::SEPARATOR);
  60. test_dir_with_file(reporter, dir, filename);
  61. // Test using no filename.
  62. test_dir_with_file(reporter, dir, SkString());
  63. // Testing using no directory.
  64. test_dir_with_file(reporter, SkString(), filename);
  65. // Test with a sub directory.
  66. dir.append("subDir");
  67. test_dir_with_file(reporter, dir, filename);
  68. // Basename of a directory with a path separator at the end is empty.
  69. dir.appendUnichar(SkOSPath::SEPARATOR);
  70. SkString baseOfDir = SkOSPath::Basename(dir.c_str());
  71. REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
  72. // Basename of nullptr is an empty string.
  73. SkString empty = SkOSPath::Basename(nullptr);
  74. REPORTER_ASSERT(reporter, empty.size() == 0);
  75. // File in root dir
  76. dir.printf("%c", SkOSPath::SEPARATOR);
  77. filename.set("file");
  78. test_dir_with_file(reporter, dir, filename);
  79. // Just the root dir
  80. filename.reset();
  81. test_dir_with_file(reporter, dir, filename);
  82. // Test that nullptr can be used for the directory and filename.
  83. SkString emptyPath = SkOSPath::Join(nullptr, nullptr);
  84. REPORTER_ASSERT(reporter, emptyPath.isEmpty());
  85. }