Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveL-MSFT committed Dec 16, 2024
1 parent 2222968 commit 82210d1
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions dsc_lib/src/functions/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 82210d1

Please sign in to comment.