xref: /drstd/src/std/sys/windows/args/tests.rs (revision 9670759b785600bf6315e4173e46a602f16add7a)
1 use crate::std::ffi::OsString;
2 use crate::std::sys::windows::args::*;
3 
chk(string: &str, parts: &[&str])4 fn chk(string: &str, parts: &[&str]) {
5     let mut wide: Vec<u16> = OsString::from(string).encode_wide().collect();
6     wide.push(0);
7     let parsed =
8         unsafe { parse_lp_cmd_line(WStrUnits::new(wide.as_ptr()), || OsString::from("TEST.EXE")) };
9     let expected: Vec<OsString> = parts.iter().map(|k| OsString::from(k)).collect();
10     assert_eq!(parsed.as_slice(), expected.as_slice(), "{:?}", string);
11 }
12 
13 #[test]
empty()14 fn empty() {
15     chk("", &["TEST.EXE"]);
16     chk("\0", &["TEST.EXE"]);
17 }
18 
19 #[test]
single_words()20 fn single_words() {
21     chk("EXE one_word", &["EXE", "one_word"]);
22     chk("EXE a", &["EXE", "a"]);
23     chk("EXE ��", &["EXE", "��"]);
24     chk("EXE ����", &["EXE", "����"]);
25 }
26 
27 #[test]
official_examples()28 fn official_examples() {
29     chk(r#"EXE "abc" d e"#, &["EXE", "abc", "d", "e"]);
30     chk(r#"EXE a\\\b d"e f"g h"#, &["EXE", r"a\\\b", "de fg", "h"]);
31     chk(r#"EXE a\\\"b c d"#, &["EXE", r#"a\"b"#, "c", "d"]);
32     chk(r#"EXE a\\\\"b c" d e"#, &["EXE", r"a\\b c", "d", "e"]);
33 }
34 
35 #[test]
whitespace_behavior()36 fn whitespace_behavior() {
37     chk(" test", &["", "test"]);
38     chk("  test", &["", "test"]);
39     chk(" test test2", &["", "test", "test2"]);
40     chk(" test  test2", &["", "test", "test2"]);
41     chk("test test2 ", &["test", "test2"]);
42     chk("test  test2 ", &["test", "test2"]);
43     chk("test ", &["test"]);
44 }
45 
46 #[test]
genius_quotes()47 fn genius_quotes() {
48     chk(r#"EXE "" """#, &["EXE", "", ""]);
49     chk(r#"EXE "" """"#, &["EXE", "", r#"""#]);
50     chk(
51         r#"EXE "this is """all""" in the same argument""#,
52         &["EXE", r#"this is "all" in the same argument"#],
53     );
54     chk(r#"EXE "a"""#, &["EXE", r#"a""#]);
55     chk(r#"EXE "a"" a"#, &["EXE", r#"a" a"#]);
56     // quotes cannot be escaped in command names
57     chk(r#""EXE" check"#, &["EXE", "check"]);
58     chk(r#""EXE check""#, &["EXE check"]);
59     chk(r#""EXE """for""" check"#, &["EXE for check"]);
60     chk(r#""EXE \"for\" check"#, &[r"EXE \for\ check"]);
61     chk(
62         r#""EXE \" for \" check"#,
63         &[r"EXE \", "for", r#"""#, "check"],
64     );
65     chk(r#"E"X"E test"#, &["EXE", "test"]);
66     chk(r#"EX""E test"#, &["EXE", "test"]);
67 }
68 
69 // from https://daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESEX
70 #[test]
post_2008()71 fn post_2008() {
72     chk("EXE CallMeIshmael", &["EXE", "CallMeIshmael"]);
73     chk(r#"EXE "Call Me Ishmael""#, &["EXE", "Call Me Ishmael"]);
74     chk(r#"EXE Cal"l Me I"shmael"#, &["EXE", "Call Me Ishmael"]);
75     chk(r#"EXE CallMe\"Ishmael"#, &["EXE", r#"CallMe"Ishmael"#]);
76     chk(r#"EXE "CallMe\"Ishmael""#, &["EXE", r#"CallMe"Ishmael"#]);
77     chk(r#"EXE "Call Me Ishmael\\""#, &["EXE", r"Call Me Ishmael\"]);
78     chk(r#"EXE "CallMe\\\"Ishmael""#, &["EXE", r#"CallMe\"Ishmael"#]);
79     chk(r#"EXE a\\\b"#, &["EXE", r"a\\\b"]);
80     chk(r#"EXE "a\\\b""#, &["EXE", r"a\\\b"]);
81     chk(
82         r#"EXE "\"Call Me Ishmael\"""#,
83         &["EXE", r#""Call Me Ishmael""#],
84     );
85     chk(r#"EXE "C:\TEST A\\""#, &["EXE", r"C:\TEST A\"]);
86     chk(r#"EXE "\"C:\TEST A\\\"""#, &["EXE", r#""C:\TEST A\""#]);
87     chk(r#"EXE "a b c"  d  e"#, &["EXE", "a b c", "d", "e"]);
88     chk(r#"EXE "ab\"c"  "\\"  d"#, &["EXE", r#"ab"c"#, r"\", "d"]);
89     chk(r#"EXE a\\\b d"e f"g h"#, &["EXE", r"a\\\b", "de fg", "h"]);
90     chk(r#"EXE a\\\"b c d"#, &["EXE", r#"a\"b"#, "c", "d"]);
91     chk(r#"EXE a\\\\"b c" d e"#, &["EXE", r"a\\b c", "d", "e"]);
92     // Double Double Quotes
93     chk(r#"EXE "a b c"""#, &["EXE", r#"a b c""#]);
94     chk(
95         r#"EXE """CallMeIshmael"""  b  c"#,
96         &["EXE", r#""CallMeIshmael""#, "b", "c"],
97     );
98     chk(
99         r#"EXE """Call Me Ishmael""""#,
100         &["EXE", r#""Call Me Ishmael""#],
101     );
102     chk(
103         r#"EXE """"Call Me Ishmael"" b c"#,
104         &["EXE", r#""Call"#, "Me", "Ishmael", "b", "c"],
105     );
106 }
107