@@ -48,19 +48,80 @@ mod tests {
4848 use crate :: configure:: context:: Context ;
4949 use crate :: parser:: Statement ;
5050
51+ const SEPARATOR : char = std:: path:: MAIN_SEPARATOR ;
52+
53+ #[ test]
54+ fn start_with_drive_letter ( ) {
55+ let mut parser = Statement :: new ( ) . unwrap ( ) ;
56+ let result = parser. parse_and_execute ( "[path('C:\\ ','test')]" , & Context :: new ( ) ) . unwrap ( ) ;
57+
58+ #[ cfg( target_os = "windows" ) ]
59+ assert_eq ! ( result, format!( "C:{SEPARATOR}test" ) ) ;
60+
61+ #[ cfg( not( target_os = "windows" ) ) ]
62+ assert_eq ! ( result, format!( "C:\\ {SEPARATOR}test" ) ) ;
63+ }
64+
65+ #[ test]
66+ fn drive_letter_in_middle ( ) {
67+ let mut parser = Statement :: new ( ) . unwrap ( ) ;
68+ let result = parser. parse_and_execute ( "[path('a','C:\\ ','test')]" , & Context :: new ( ) ) . unwrap ( ) ;
69+
70+ // if any part of the path is absolute, it replaces it instead of appending on Windows
71+ #[ cfg( target_os = "windows" ) ]
72+ assert_eq ! ( result, format!( "C:{SEPARATOR}test" ) ) ;
73+
74+ // non-Windows, the colon is a valid character in a path
75+ #[ cfg( not( target_os = "windows" ) ) ]
76+ assert_eq ! ( result, format!( "a{SEPARATOR}C:\\ {SEPARATOR}test" ) ) ;
77+ }
78+
79+ #[ test]
80+ fn multiple_drive_letters ( ) {
81+ let mut parser = Statement :: new ( ) . unwrap ( ) ;
82+ let result = parser. parse_and_execute ( "[path('C:\\ ','D:\\ ','test')]" , & Context :: new ( ) ) . unwrap ( ) ;
83+
84+ // if any part of the path is absolute, it replaces it instead of appending on Windows
85+ #[ cfg( target_os = "windows" ) ]
86+ assert_eq ! ( result, format!( "D:\\ test" ) ) ;
87+
88+ // non-Windows, the colon is a valid character in a path
89+ #[ cfg( not( target_os = "windows" ) ) ]
90+ assert_eq ! ( result, format!( "C:\\ {SEPARATOR}D:\\ {SEPARATOR}test" ) ) ;
91+ }
92+
93+ #[ test]
94+ fn relative_path ( ) {
95+ let mut parser = Statement :: new ( ) . unwrap ( ) ;
96+ let result = parser. parse_and_execute ( "[path('a','..','b')]" , & Context :: new ( ) ) . unwrap ( ) ;
97+ assert_eq ! ( result, format!( "a{SEPARATOR}..{SEPARATOR}b" ) ) ;
98+ }
99+
100+ #[ test]
101+ fn path_segement_with_separator ( ) {
102+ let mut parser = Statement :: new ( ) . unwrap ( ) ;
103+ let result = parser. parse_and_execute ( format ! ( "[path('a','b{SEPARATOR}c')]" ) . as_str ( ) , & Context :: new ( ) ) . unwrap ( ) ;
104+ assert_eq ! ( result, format!( "a{SEPARATOR}b{SEPARATOR}c" ) ) ;
105+ }
106+
107+ #[ test]
108+ fn unix_absolute_path ( ) {
109+ let mut parser = Statement :: new ( ) . unwrap ( ) ;
110+ let result = parser. parse_and_execute ( "[path('/','a','b')]" , & Context :: new ( ) ) . unwrap ( ) ;
111+ assert_eq ! ( result, format!( "/a{SEPARATOR}b" ) ) ;
112+ }
113+
51114 #[ test]
52115 fn two_args ( ) {
53116 let mut parser = Statement :: new ( ) . unwrap ( ) ;
54- let separator = std:: path:: MAIN_SEPARATOR ;
55117 let result = parser. parse_and_execute ( "[path('a','b')]" , & Context :: new ( ) ) . unwrap ( ) ;
56- assert_eq ! ( result, format!( "a{separator }b" ) ) ;
118+ assert_eq ! ( result, format!( "a{SEPARATOR }b" ) ) ;
57119 }
58120
59121 #[ test]
60122 fn three_args ( ) {
61123 let mut parser = Statement :: new ( ) . unwrap ( ) ;
62- let separator = std:: path:: MAIN_SEPARATOR ;
63124 let result = parser. parse_and_execute ( "[path('a','b','c')]" , & Context :: new ( ) ) . unwrap ( ) ;
64- assert_eq ! ( result, format!( "a{separator }b{separator }c" ) ) ;
125+ assert_eq ! ( result, format!( "a{SEPARATOR }b{SEPARATOR }c" ) ) ;
65126 }
66127}
0 commit comments