From f4ee885b3b3019b32d5c481bddf1b2667fba7fb3 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 8 Sep 2020 02:42:45 +0300 Subject: [PATCH] Add VirtualPath tests --- crates/vfs/src/vfs_path.rs | 41 +++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index 113c2e4e65..dac8393ea5 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -290,9 +290,10 @@ impl VirtualPath { // FIXME: Currently VirtualPath does is unable to distinguish a directory from a file // hence this method will return `Some("directory_name", None)` for a directory pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { - let file_name = match self.0.rfind('/') { - Some(position) => &self.0[position + 1..], - None => &self.0, + let file_path = if self.0.ends_with('/') { &self.0[..&self.0.len() - 1] } else { &self.0 }; + let file_name = match file_path.rfind('/') { + Some(position) => &file_path[position + 1..], + None => file_path, }; if file_name.is_empty() { @@ -310,3 +311,37 @@ impl VirtualPath { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn virtual_path_extensions() { + assert_eq!(VirtualPath("/".to_string()).file_name_and_extension(), None); + assert_eq!( + VirtualPath("/directory".to_string()).file_name_and_extension(), + Some(("directory", None)) + ); + assert_eq!( + VirtualPath("/directory/".to_string()).file_name_and_extension(), + Some(("directory", None)) + ); + assert_eq!( + VirtualPath("/directory/file".to_string()).file_name_and_extension(), + Some(("file", None)) + ); + assert_eq!( + VirtualPath("/directory/.file".to_string()).file_name_and_extension(), + Some((".file", None)) + ); + assert_eq!( + VirtualPath("/directory/.file.rs".to_string()).file_name_and_extension(), + Some((".file", Some("rs"))) + ); + assert_eq!( + VirtualPath("/directory/file.rs".to_string()).file_name_and_extension(), + Some(("file", Some("rs"))) + ); + } +}