Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
OSPathTest.cpp
Go to the documentation of this file.
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
11#include "src/utils/SkOSPath.h"
12#include "tests/Test.h"
13
14#include <cstddef>
15
16/**
17 * Test SkOSPath::Join, SkOSPath::Basename, and SkOSPath::Dirname.
18 * Will use SkOSPath::Join to append filename to dir, test that it works correctly,
19 * and tests using SkOSPath::Basename on the result.
20 * @param reporter Reporter for test conditions.
21 * @param dir String representing the path to a folder. May or may not
22 * end with SkOSPath::SEPARATOR.
23 * @param filename String representing the basename of a file. Must NOT
24 * contain SkOSPath::SEPARATOR.
25 */
27 const SkString& filename) {
28 // If filename contains SkOSPath::SEPARATOR, the tests will fail.
30
31 // Tests for SkOSPath::Join and SkOSPath::Basename
32
33 // fullName should be "dir<SkOSPath::SEPARATOR>file"
34 SkString fullName = SkOSPath::Join(dir.c_str(), filename.c_str());
35
36 // fullName should be the combined size of dir and file, plus one if
37 // dir did not include the final path separator.
38 size_t expectedSize = dir.size() + filename.size();
39 if (!dir.endsWith(SkOSPath::SEPARATOR) && !dir.isEmpty()) {
40 expectedSize++;
41 }
42 REPORTER_ASSERT(reporter, fullName.size() == expectedSize);
43
44 SkString basename = SkOSPath::Basename(fullName.c_str());
45 SkString dirname = SkOSPath::Dirname(fullName.c_str());
46
47 // basename should be the same as filename
48 REPORTER_ASSERT(reporter, basename.equals(filename));
49
50 // dirname should be the same as dir with any trailing seperators removed.
51 // Except when the the string is just "/".
52 SkString strippedDir = dir;
53 while (strippedDir.size() > 2 && strippedDir[strippedDir.size() - 1] == SkOSPath::SEPARATOR) {
54 strippedDir.remove(strippedDir.size() - 1, 1);
55 }
56 if (!dirname.equals(strippedDir)) {
57 SkDebugf("OOUCH %s %s %s\n", dir.c_str(), strippedDir.c_str(), dirname.c_str());
58 }
59 REPORTER_ASSERT(reporter, dirname.equals(strippedDir));
60
61 // basename will not contain a path separator
62 REPORTER_ASSERT(reporter, !basename.contains(SkOSPath::SEPARATOR));
63
64 // Now take the basename of filename, which should be the same as filename.
65 basename = SkOSPath::Basename(filename.c_str());
66 REPORTER_ASSERT(reporter, basename.equals(filename));
67}
68
70 SkString dir("dir");
71 SkString filename("file");
72 test_dir_with_file(reporter, dir, filename);
73
74 // Now make sure this works with a path separator at the end of dir.
75 dir.appendUnichar(SkOSPath::SEPARATOR);
76 test_dir_with_file(reporter, dir, filename);
77
78 // Test using no filename.
80
81 // Testing using no directory.
83
84 // Test with a sub directory.
85 dir.append("subDir");
86 test_dir_with_file(reporter, dir, filename);
87
88 // Basename of a directory with a path separator at the end is empty.
89 dir.appendUnichar(SkOSPath::SEPARATOR);
90 SkString baseOfDir = SkOSPath::Basename(dir.c_str());
91 REPORTER_ASSERT(reporter, baseOfDir.size() == 0);
92
93 // Basename of nullptr is an empty string.
95 REPORTER_ASSERT(reporter, empty.size() == 0);
96
97 // File in root dir
98 dir.printf("%c", SkOSPath::SEPARATOR);
99 filename.set("file");
100 test_dir_with_file(reporter, dir, filename);
101
102 // Just the root dir
103 filename.reset();
104 test_dir_with_file(reporter, dir, filename);
105
106 // Test that nullptr can be used for the directory and filename.
107 SkString emptyPath = SkOSPath::Join(nullptr, nullptr);
108 REPORTER_ASSERT(reporter, emptyPath.isEmpty());
109}
reporter
static void test_dir_with_file(skiatest::Reporter *reporter, const SkString &dir, const SkString &filename)
#define SkASSERT(cond)
Definition SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
static constexpr char SEPARATOR
Definition SkOSPath.h:21
static SkString Join(const char *rootPath, const char *relativePath)
Definition SkOSPath.cpp:14
static SkString Basename(const char *fullPath)
Definition SkOSPath.cpp:23
static SkString Dirname(const char *fullPath)
Definition SkOSPath.cpp:36
void void void void void void void remove(size_t offset, size_t length)
Definition SkString.cpp:592
size_t size() const
Definition SkString.h:131
void set(const SkString &src)
Definition SkString.h:186
bool equals(const SkString &) const
Definition SkString.cpp:324
bool isEmpty() const
Definition SkString.h:130
bool contains(const char substring[]) const
Definition SkString.h:152
void reset()
Definition SkString.cpp:358
const char * c_str() const
Definition SkString.h:133
EMSCRIPTEN_KEEPALIVE void empty()