aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-09-08 00:42:45 +0100
committerKirill Bulatov <[email protected]>2020-09-09 23:42:20 +0100
commitf4ee885b3b3019b32d5c481bddf1b2667fba7fb3 (patch)
treef766edbf7d0a3f9fb26d743e07fceba7dc7fd41c
parenta7d75463c7d1473e4276601688baa22c10eec255 (diff)
Add VirtualPath tests
-rw-r--r--crates/vfs/src/vfs_path.rs41
1 files changed, 38 insertions, 3 deletions
diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs
index 113c2e4e6..dac8393ea 100644
--- a/crates/vfs/src/vfs_path.rs
+++ b/crates/vfs/src/vfs_path.rs
@@ -290,9 +290,10 @@ impl VirtualPath {
290 // FIXME: Currently VirtualPath does is unable to distinguish a directory from a file 290 // FIXME: Currently VirtualPath does is unable to distinguish a directory from a file
291 // hence this method will return `Some("directory_name", None)` for a directory 291 // hence this method will return `Some("directory_name", None)` for a directory
292 pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { 292 pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> {
293 let file_name = match self.0.rfind('/') { 293 let file_path = if self.0.ends_with('/') { &self.0[..&self.0.len() - 1] } else { &self.0 };
294 Some(position) => &self.0[position + 1..], 294 let file_name = match file_path.rfind('/') {
295 None => &self.0, 295 Some(position) => &file_path[position + 1..],
296 None => file_path,
296 }; 297 };
297 298
298 if file_name.is_empty() { 299 if file_name.is_empty() {
@@ -310,3 +311,37 @@ impl VirtualPath {
310 } 311 }
311 } 312 }
312} 313}
314
315#[cfg(test)]
316mod tests {
317 use super::*;
318
319 #[test]
320 fn virtual_path_extensions() {
321 assert_eq!(VirtualPath("/".to_string()).file_name_and_extension(), None);
322 assert_eq!(
323 VirtualPath("/directory".to_string()).file_name_and_extension(),
324 Some(("directory", None))
325 );
326 assert_eq!(
327 VirtualPath("/directory/".to_string()).file_name_and_extension(),
328 Some(("directory", None))
329 );
330 assert_eq!(
331 VirtualPath("/directory/file".to_string()).file_name_and_extension(),
332 Some(("file", None))
333 );
334 assert_eq!(
335 VirtualPath("/directory/.file".to_string()).file_name_and_extension(),
336 Some((".file", None))
337 );
338 assert_eq!(
339 VirtualPath("/directory/.file.rs".to_string()).file_name_and_extension(),
340 Some((".file", Some("rs")))
341 );
342 assert_eq!(
343 VirtualPath("/directory/file.rs".to_string()).file_name_and_extension(),
344 Some(("file", Some("rs")))
345 );
346 }
347}