diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2021-09-27 16:22:53 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2021-09-27 19:10:47 +0300 |
commit | aafbe111fc90876d801135f0dc5e01e9742f647a (patch) | |
tree | ecaa944881ec81c7dc0b4a134270b283238676a9 /source/blender/blenlib/tests/BLI_path_util_test.cc | |
parent | 6578db57cd878a94bdbdabe953c8819625c5a811 (diff) |
BLI Path: add function `BLI_path_contains()`
Add function `BLI_path_contains(container, containee)` that returns true
if and only `container` contains `containee`.
Paths are normalised and converted to native path separators before
comparing. Relative paths are *not* made absolute, to simplify the
function call; if this is necessary the caller has to do this conversion
first.
Diffstat (limited to 'source/blender/blenlib/tests/BLI_path_util_test.cc')
-rw-r--r-- | source/blender/blenlib/tests/BLI_path_util_test.cc | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/source/blender/blenlib/tests/BLI_path_util_test.cc b/source/blender/blenlib/tests/BLI_path_util_test.cc index cf5135731e2..fde28ebaf55 100644 --- a/source/blender/blenlib/tests/BLI_path_util_test.cc +++ b/source/blender/blenlib/tests/BLI_path_util_test.cc @@ -655,3 +655,26 @@ TEST(path_util, PathRelPath) # undef PATH_REL #endif + +/* BLI_path_contains */ +TEST(path_util, PathContains) +{ + EXPECT_TRUE(BLI_path_contains("/some/path", "/some/path")) << "A path contains itself"; + EXPECT_TRUE(BLI_path_contains("/some/path", "/some/path/inside")) + << "A path contains its subdirectory"; + EXPECT_TRUE(BLI_path_contains("/some/path", "/some/path/../path/inside")) + << "Paths should be normalised"; + EXPECT_TRUE(BLI_path_contains("C:\\some\\path", "C:\\some\\path\\inside")) + << "Windows paths should be supported as well"; + + EXPECT_FALSE(BLI_path_contains("C:\\some\\path", "C:\\some\\other\\path")) + << "Windows paths should be supported as well"; + EXPECT_FALSE(BLI_path_contains("/some/path", "/")) + << "Root directory not be contained in a subdirectory"; + EXPECT_FALSE(BLI_path_contains("/some/path", "/some/path/../outside")) + << "Paths should be normalised"; + EXPECT_FALSE(BLI_path_contains("/some/path", "/some/path_library")) + << "Just sharing a suffix is not enough, path semantics should be followed"; + EXPECT_FALSE(BLI_path_contains("/some/path", "./contents")) + << "Relative paths are not supported"; +} |