From 90a2f4468abf2e25d4e58c6fbacb40b3dbe32b1f Mon Sep 17 00:00:00 2001 From: StunxFS Date: Tue, 14 Nov 2023 21:35:54 -0400 Subject: [PATCH] add test for `std.fs.Path.file_name()` --- lib/std/src/fs/Path.ri | 10 +++++++--- lib/std/tests/fs_test.ri | 7 +++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/std/src/fs/Path.ri b/lib/std/src/fs/Path.ri index aebe3baa6..129c06b26 100644 --- a/lib/std/src/fs/Path.ri +++ b/lib/std/src/fs/Path.ri @@ -85,7 +85,7 @@ public struct Path { if path.is_empty() { return none; } - + mut end_index: uint := path.len - 1; while path[end_index] == SEPARATOR : end_index -= 1 { if end_index == 0 { @@ -143,9 +143,13 @@ public struct Path { /// Files that end with `.`, or that start with `.` and have no other `.` in their /// name, are considered to have no extension. public func extension(path: string) -> string { - filename := Self.file_name(path); + filename := Self.base_name(path); index := filename.last_index_of_byte(b'.') ?? return ""; - return if index == 0 or index + 1 >= filename.len { "" } else { filename[index..] }; + return if index == 0 or index + 1 >= filename.len { + "" + } else { + filename[index..] + }; } /// This function is like a series of `cd` statements executed one after another. diff --git a/lib/std/tests/fs_test.ri b/lib/std/tests/fs_test.ri index 4d976e416..14ecabb3d 100644 --- a/lib/std/tests/fs_test.ri +++ b/lib/std/tests/fs_test.ri @@ -86,6 +86,13 @@ test "std.fs.Path.base_name()" { @assert(Path.base_name("foo") == "foo"); } +test "std.fs.Path.file_name()" { + @assert(Path.file_name("rivet/lib/std/project.json") == "project.json"); + @assert(Path.file_name("rivet/lib/std/") == ""); + @assert(Path.file_name("rivet/lib/std") == "std"); + @assert(Path.file_name("filename") == "filename"); +} + test "std.fs.Path.extension()" { @assert(Path.extension("") == ""); @assert(Path.extension(".") == "");