Skip to content

Commit

Permalink
add test for std.fs.Path.file_name()
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Nov 15, 2023
1 parent b571ba6 commit 90a2f44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/std/src/fs/Path.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions lib/std/tests/fs_test.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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(".") == "");
Expand Down

0 comments on commit 90a2f44

Please sign in to comment.