Skip to content

Commit 82210d1

Browse files
committed
add more tests
1 parent 2222968 commit 82210d1

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

dsc_lib/src/functions/path.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,48 +55,75 @@ mod tests {
5555
use crate::configure::context::Context;
5656
use crate::parser::Statement;
5757

58+
const SEPARATOR: char = std::path::MAIN_SEPARATOR;
59+
5860
#[test]
5961
fn start_with_drive_letter() {
6062
let mut parser = Statement::new().unwrap();
61-
let separator = std::path::MAIN_SEPARATOR;
6263
let result = parser.parse_and_execute("[path('C:','test')]", &Context::new()).unwrap();
63-
assert_eq!(result, format!("C:{separator}test"));
64+
assert_eq!(result, format!("C:{SEPARATOR}test"));
6465
}
6566

6667
#[test]
6768
fn drive_letter_in_middle() {
6869
let mut parser = Statement::new().unwrap();
69-
let separator = std::path::MAIN_SEPARATOR;
7070
let result = parser.parse_and_execute("[path('a','C:','test')]", &Context::new()).unwrap();
7171

72-
// if any part of the path is absolute, it replaces it instead of appending
72+
// if any part of the path is absolute, it replaces it instead of appending on Windows
73+
#[cfg(target_os = "windows")]
74+
assert_eq!(result, format!("C:{SEPARATOR}test"));
75+
76+
// non-Windows, the colon is a valid character in a path
77+
#[cfg(not(target_os = "windows"))]
78+
assert_eq!(result, format!("a{SEPARATOR}C:{SEPARATOR}test"));
79+
}
80+
81+
#[test]
82+
fn multiple_drive_letters() {
83+
let mut parser = Statement::new().unwrap();
84+
let result = parser.parse_and_execute("[path('C:','D:','test')]", &Context::new()).unwrap();
85+
86+
// if any part of the path is absolute, it replaces it instead of appending on Windows
7387
#[cfg(target_os = "windows")]
74-
assert_eq!(result, format!("C:{separator}test"));
88+
assert_eq!(result, format!("D:{SEPARATOR}test"));
89+
90+
// non-Windows, the colon is a valid character in a path
7591
#[cfg(not(target_os = "windows"))]
76-
assert_eq!(result, format!("a{separator}C:{separator}test"));
92+
assert_eq!(result, format!("C:{SEPARATOR}D:{SEPARATOR}test"));
93+
}
94+
95+
#[test]
96+
fn relative_path() {
97+
let mut parser = Statement::new().unwrap();
98+
let result = parser.parse_and_execute("[path('a','..','b')]", &Context::new()).unwrap();
99+
assert_eq!(result, format!("a{SEPARATOR}..{SEPARATOR}b"));
100+
}
101+
102+
#[test]
103+
fn path_segement_with_separator() {
104+
let mut parser = Statement::new().unwrap();
105+
let result = parser.parse_and_execute(format!("[path('a','b{SEPARATOR}c')]").as_str(), &Context::new()).unwrap();
106+
assert_eq!(result, format!("a{SEPARATOR}b{SEPARATOR}c"));
77107
}
78108

79109
#[test]
80110
fn unix_absolute_path() {
81111
let mut parser = Statement::new().unwrap();
82-
let separator = std::path::MAIN_SEPARATOR;
83112
let result = parser.parse_and_execute("[path('/','a','b')]", &Context::new()).unwrap();
84-
assert_eq!(result, format!("/a{separator}b"));
113+
assert_eq!(result, format!("/a{SEPARATOR}b"));
85114
}
86115

87116
#[test]
88117
fn two_args() {
89118
let mut parser = Statement::new().unwrap();
90-
let separator = std::path::MAIN_SEPARATOR;
91119
let result = parser.parse_and_execute("[path('a','b')]", &Context::new()).unwrap();
92-
assert_eq!(result, format!("a{separator}b"));
120+
assert_eq!(result, format!("a{SEPARATOR}b"));
93121
}
94122

95123
#[test]
96124
fn three_args() {
97125
let mut parser = Statement::new().unwrap();
98-
let separator = std::path::MAIN_SEPARATOR;
99126
let result = parser.parse_and_execute("[path('a','b','c')]", &Context::new()).unwrap();
100-
assert_eq!(result, format!("a{separator}b{separator}c"));
127+
assert_eq!(result, format!("a{SEPARATOR}b{SEPARATOR}c"));
101128
}
102129
}

0 commit comments

Comments
 (0)