From 550febac58230d84246770425e43cf7cf8387290 Mon Sep 17 00:00:00 2001 From: Alexander Batischev Date: Fri, 29 Dec 2023 07:03:47 +0300 Subject: Add tests for Filepath::file_name() --- test/filepath.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test/filepath.cpp b/test/filepath.cpp index 6a67de75..ffa2ca70 100644 --- a/test/filepath.cpp +++ b/test/filepath.cpp @@ -126,7 +126,7 @@ TEST_CASE("Can check if path is absolute", "[Filepath]") TEST_CASE("Can check if path starts with a given base path", "[Filepath]") { SECTION("Empty path") { - Filepath path; + const Filepath path; SECTION("Base path is empty") { REQUIRE(path.starts_with(Filepath{})); @@ -163,3 +163,27 @@ TEST_CASE("Can check if path starts with a given base path", "[Filepath]") } } } + +TEST_CASE("Can extract the final component of the path (file or directory name)", + "[Filepath]") +{ + SECTION("Empty path has no final component") { + const Filepath path; + REQUIRE(path.file_name() == nonstd::nullopt); + } + + SECTION("The final component of a path with single level is the path itself") { + const auto path = Filepath::from_locale_string("hello"); + REQUIRE(path.file_name().value() == path); + } + + SECTION("Multi-level path") { + const auto path = Filepath::from_locale_string("/dev/pts/0"); + REQUIRE(path.file_name().value() == Filepath::from_locale_string("0")); + } + + SECTION("Final component is not a valid UTF-8 string") { + const auto path = Filepath::from_locale_string("/whatever/one\x80two"); + REQUIRE(path.file_name().value() == Filepath::from_locale_string("one\x80two")); + } +} -- cgit v1.2.3