diff --git a/dsc_lib/src/functions/path.rs b/dsc_lib/src/functions/path.rs index 4240ff5a..ee242826 100644 --- a/dsc_lib/src/functions/path.rs +++ b/dsc_lib/src/functions/path.rs @@ -55,48 +55,75 @@ mod tests { use crate::configure::context::Context; use crate::parser::Statement; + const SEPARATOR: char = std::path::MAIN_SEPARATOR; + #[test] fn start_with_drive_letter() { let mut parser = Statement::new().unwrap(); - let separator = std::path::MAIN_SEPARATOR; let result = parser.parse_and_execute("[path('C:','test')]", &Context::new()).unwrap(); - assert_eq!(result, format!("C:{separator}test")); + assert_eq!(result, format!("C:{SEPARATOR}test")); } #[test] fn drive_letter_in_middle() { let mut parser = Statement::new().unwrap(); - let separator = std::path::MAIN_SEPARATOR; let result = parser.parse_and_execute("[path('a','C:','test')]", &Context::new()).unwrap(); - // if any part of the path is absolute, it replaces it instead of appending + // if any part of the path is absolute, it replaces it instead of appending on Windows + #[cfg(target_os = "windows")] + assert_eq!(result, format!("C:{SEPARATOR}test")); + + // non-Windows, the colon is a valid character in a path + #[cfg(not(target_os = "windows"))] + assert_eq!(result, format!("a{SEPARATOR}C:{SEPARATOR}test")); + } + + #[test] + fn multiple_drive_letters() { + let mut parser = Statement::new().unwrap(); + let result = parser.parse_and_execute("[path('C:','D:','test')]", &Context::new()).unwrap(); + + // if any part of the path is absolute, it replaces it instead of appending on Windows #[cfg(target_os = "windows")] - assert_eq!(result, format!("C:{separator}test")); + assert_eq!(result, format!("D:{SEPARATOR}test")); + + // non-Windows, the colon is a valid character in a path #[cfg(not(target_os = "windows"))] - assert_eq!(result, format!("a{separator}C:{separator}test")); + assert_eq!(result, format!("C:{SEPARATOR}D:{SEPARATOR}test")); + } + + #[test] + fn relative_path() { + let mut parser = Statement::new().unwrap(); + let result = parser.parse_and_execute("[path('a','..','b')]", &Context::new()).unwrap(); + assert_eq!(result, format!("a{SEPARATOR}..{SEPARATOR}b")); + } + + #[test] + fn path_segement_with_separator() { + let mut parser = Statement::new().unwrap(); + let result = parser.parse_and_execute(format!("[path('a','b{SEPARATOR}c')]").as_str(), &Context::new()).unwrap(); + assert_eq!(result, format!("a{SEPARATOR}b{SEPARATOR}c")); } #[test] fn unix_absolute_path() { let mut parser = Statement::new().unwrap(); - let separator = std::path::MAIN_SEPARATOR; let result = parser.parse_and_execute("[path('/','a','b')]", &Context::new()).unwrap(); - assert_eq!(result, format!("/a{separator}b")); + assert_eq!(result, format!("/a{SEPARATOR}b")); } #[test] fn two_args() { let mut parser = Statement::new().unwrap(); - let separator = std::path::MAIN_SEPARATOR; let result = parser.parse_and_execute("[path('a','b')]", &Context::new()).unwrap(); - assert_eq!(result, format!("a{separator}b")); + assert_eq!(result, format!("a{SEPARATOR}b")); } #[test] fn three_args() { let mut parser = Statement::new().unwrap(); - let separator = std::path::MAIN_SEPARATOR; let result = parser.parse_and_execute("[path('a','b','c')]", &Context::new()).unwrap(); - assert_eq!(result, format!("a{separator}b{separator}c")); + assert_eq!(result, format!("a{SEPARATOR}b{SEPARATOR}c")); } }