mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 04:23:25 +00:00
Add VirtualPath tests
This commit is contained in:
parent
a7d75463c7
commit
f4ee885b3b
1 changed files with 38 additions and 3 deletions
|
@ -290,9 +290,10 @@ impl VirtualPath {
|
||||||
// FIXME: Currently VirtualPath does is unable to distinguish a directory from a file
|
// 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
|
// hence this method will return `Some("directory_name", None)` for a directory
|
||||||
pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> {
|
pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> {
|
||||||
let file_name = match self.0.rfind('/') {
|
let file_path = if self.0.ends_with('/') { &self.0[..&self.0.len() - 1] } else { &self.0 };
|
||||||
Some(position) => &self.0[position + 1..],
|
let file_name = match file_path.rfind('/') {
|
||||||
None => &self.0,
|
Some(position) => &file_path[position + 1..],
|
||||||
|
None => file_path,
|
||||||
};
|
};
|
||||||
|
|
||||||
if file_name.is_empty() {
|
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")))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue