From f44bc19ce15def3ac1e2a336cb9d4aec4a680a8f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 14 Jul 2022 17:57:34 +0200 Subject: [PATCH] test/fs/TestPath: new unit test --- test/fs/TestPath.cxx | 81 ++++++++++++++++++++++++++++++++++++++++++++ test/fs/meson.build | 1 + 2 files changed, 82 insertions(+) create mode 100644 test/fs/TestPath.cxx diff --git a/test/fs/TestPath.cxx b/test/fs/TestPath.cxx new file mode 100644 index 000000000..a0c991da2 --- /dev/null +++ b/test/fs/TestPath.cxx @@ -0,0 +1,81 @@ +/* + * Copyright 2003-2022 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "fs/Path.hxx" +#include "fs/AllocatedPath.hxx" + +#include + +TEST(Path, Basic) +{ + EXPECT_TRUE(Path{nullptr}.IsNull()); + EXPECT_FALSE(Path::FromFS(PATH_LITERAL("")).IsNull()); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("")).length(), 0U); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("foo")).length(), 3U); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("foo")).ToUTF8(), "foo"); +} + +TEST(Path, GetBase) +{ + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("")).GetBase().c_str(), ""); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("foo")).GetBase().c_str(), "foo"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("./foo")).GetBase().c_str(), "foo"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("../foo")).GetBase().c_str(), "foo"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("foo/bar")).GetBase().c_str(), "bar"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/bar")).GetBase().c_str(), "bar"); +} + +TEST(Path, GetDirectoryName) +{ + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("")).GetDirectoryName().c_str(), "."); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("foo")).GetDirectoryName().c_str(), "."); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("./foo")).GetDirectoryName().c_str(), "."); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("../foo")).GetDirectoryName().c_str(), ".."); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("foo/bar")).GetDirectoryName().c_str(), "foo"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/bar")).GetDirectoryName().c_str(), "/foo"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/")).GetDirectoryName().c_str(), "/foo"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/bar/baz")).GetDirectoryName().c_str(), "/foo/bar"); +} + +TEST(Path, Relative) +{ + EXPECT_EQ(Path::FromFS(PATH_LITERAL("foo")).Relative(Path::FromFS(PATH_LITERAL(""))), nullptr); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("/bar")).Relative(Path::FromFS(PATH_LITERAL("/foo/bar"))), nullptr); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo")).Relative(Path::FromFS(PATH_LITERAL("/foo/bar"))), "bar"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/")).Relative(Path::FromFS(PATH_LITERAL("/foo/bar"))), "bar"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo")).Relative(Path::FromFS(PATH_LITERAL("/foo///bar"))), "bar"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo")).Relative(Path::FromFS(PATH_LITERAL("/foo///"))), ""); +} + +TEST(Path, Extension) +{ + EXPECT_EQ(Path::FromFS(PATH_LITERAL("foo")).GetExtension(), nullptr); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("/foo/bar")).GetExtension(), nullptr); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("/foo/./bar")).GetExtension(), nullptr); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("/foo/.bar")).GetExtension(), nullptr); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("/foo/.")).GetExtension(), nullptr); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("/foo/..")).GetExtension(), nullptr); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("/foo.abc/bar")).GetExtension(), nullptr); + EXPECT_EQ(Path::FromFS(PATH_LITERAL("/foo.abc/")).GetExtension(), nullptr); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo.abc/bar.def")).GetExtension(), "def"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo.abc/bar.")).GetExtension(), ""); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo.abc/bar.def.ghi")).GetExtension(), "ghi"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/.bar.abc")).GetExtension(), "abc"); + EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/.bar.abc.def")).GetExtension(), "def"); +} diff --git a/test/fs/meson.build b/test/fs/meson.build index 335d788e2..ec31a9b76 100644 --- a/test/fs/meson.build +++ b/test/fs/meson.build @@ -4,6 +4,7 @@ test( 'TestFs', 'TestGlob.cxx', 'TestLookupFile.cxx', + 'TestPath.cxx', include_directories: inc, dependencies: [ fs_dep,