- Replace `Default::default()` with `std::ptr::null_mut()` or `std::ptr::null()` for FFI calls - Change `WideCStr::from_ptr` to `WideStr::from_ptr` for better safety - Remove redundant error variants in `LoadStrRcError` and `ExpandEnvVarError` - Reorder imports for better readability - Add test cases for `StrRc` and `ExpandString` utilities - Improve documentation comments for icon loading functions
167 lines
4.3 KiB
Rust
167 lines
4.3 KiB
Rust
use std::path::Path;
|
|
use wfassoc::extra::windows::*;
|
|
|
|
#[test]
|
|
fn test_icon_ref_str() {
|
|
fn ok_tester(s: &str, probe: (&str, u32)) {
|
|
let rv = s.parse::<IconRefStr>();
|
|
assert!(rv.is_ok());
|
|
let rv = rv.unwrap();
|
|
assert_eq!(rv.get_path(), probe.0);
|
|
assert_eq!(rv.get_index(), probe.1);
|
|
}
|
|
fn err_tester(s: &str) {
|
|
let rv = s.parse::<IconRefStr>();
|
|
assert!(rv.is_err());
|
|
}
|
|
|
|
ok_tester(r#"%SystemRoot%\System32\imageres.dll,-72"#, (r#"%SystemRoot%\System32\imageres.dll"#, 72));
|
|
err_tester(r#"C:\Windows\Cursors\aero_arrow.cur"#);
|
|
err_tester(r#"@%SystemRoot%\System32\shell32.dll,-30596"#);
|
|
}
|
|
|
|
#[test]
|
|
fn test_str_ref_str() {
|
|
fn ok_tester(s: &str, probe: (&str, u32)) {
|
|
let rv = s.parse::<StrRefStr>();
|
|
assert!(rv.is_ok());
|
|
let rv = rv.unwrap();
|
|
assert_eq!(rv.get_path(), probe.0);
|
|
assert_eq!(rv.get_index(), probe.1);
|
|
}
|
|
fn err_tester(s: &str) {
|
|
let rv = s.parse::<StrRefStr>();
|
|
assert!(rv.is_err());
|
|
}
|
|
|
|
ok_tester(r#"@%SystemRoot%\System32\shell32.dll,-30596"#, (r#"%SystemRoot%\System32\shell32.dll"#, 30596));
|
|
err_tester(r#"This is my application, OK?"#);
|
|
err_tester(r#"%SystemRoot%\System32\imageres.dll,-72"#);
|
|
}
|
|
|
|
#[test]
|
|
fn test_icon_rc() {
|
|
fn tester(file: &str, index: u32) {
|
|
let icon = IconRc::new(Path::new(file), index, IconSizeKind::Small);
|
|
assert!(icon.is_ok())
|
|
}
|
|
|
|
// We pick it from "jpegfile" ProgId
|
|
tester("imageres.dll", 72);
|
|
tester("notepad.exe", 0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_str_rc() {
|
|
fn tester(file: &str, index: u32) {
|
|
let rv = StrRc::new(Path::new(file), index);
|
|
assert!(rv.is_ok());
|
|
}
|
|
|
|
// We pick from "jpegfile" ProgId
|
|
tester("shell32.dll", 30596);
|
|
}
|
|
|
|
#[test]
|
|
fn test_expand_string() {
|
|
fn tester(s: &str) {
|
|
let rv = ExpandString::new(s);
|
|
assert!(rv.is_ok());
|
|
let rv = rv.unwrap();
|
|
|
|
let rv = rv.expand_string();
|
|
assert!(rv.is_ok());
|
|
}
|
|
|
|
tester(r#"%SystemRoot%\System32\shell32.dll"#);
|
|
}
|
|
|
|
#[test]
|
|
fn test_cmd_args() {
|
|
// Declare tester
|
|
fn tester(s: &str, probe: &[&'static str]) {
|
|
let rv = CmdArgs::new(s);
|
|
let inner = rv.get_inner();
|
|
assert_eq!(inner.len(), probe.len());
|
|
|
|
let n = inner.len();
|
|
for i in 0..n {
|
|
assert_eq!(inner[i].get_inner(), probe[i]);
|
|
}
|
|
}
|
|
|
|
// Normal cases
|
|
tester(
|
|
"MyApp.exe --config ppic.toml",
|
|
&["MyApp.exe", "--config", "ppic.toml"],
|
|
);
|
|
tester(
|
|
r#""C:\Program Files\MyApp\MyApp.exe" --config ppic.toml"#,
|
|
&[
|
|
r#"C:\Program Files\MyApp\MyApp.exe"#,
|
|
"--config",
|
|
"ppic.toml",
|
|
],
|
|
);
|
|
|
|
// Microsoft shitty cases.
|
|
tester(r#""abc" d e"#, &[r#"abc"#, r#"d"#, r#"e"#]);
|
|
tester(r#"a\\b d"e f"g h"#, &[r#"a\\b"#, r#"de fg"#, r#"h"#]);
|
|
tester(r#"a\\\"b c d"#, &[r#"a\"b"#, r#"c"#, r#"d"#]);
|
|
tester(r#"a\\\\"b c" d e"#, &[r#"a\\b c"#, r#"d"#, r#"e"#]);
|
|
tester(r#"a"b"" c d"#, &[r#"ab" c d"#]);
|
|
}
|
|
|
|
#[test]
|
|
fn test_cmd_arg() {
|
|
// Declare tester
|
|
fn ok_tester(s: &str, probe: &str) {
|
|
let rv = CmdArg::new(s);
|
|
assert!(rv.is_ok());
|
|
let rv = rv.unwrap();
|
|
assert_eq!(rv.get_inner(), probe);
|
|
}
|
|
fn err_tester(s: &str) {
|
|
let rv = CmdArg::new(s);
|
|
assert!(rv.is_err());
|
|
}
|
|
|
|
// Normal test
|
|
ok_tester(
|
|
r#""C:\Program Files\MyApp\MyApp.exe""#,
|
|
r#"C:\Program Files\MyApp\MyApp.exe"#,
|
|
);
|
|
err_tester("MyApp.exe --config ppic.toml");
|
|
err_tester("");
|
|
}
|
|
|
|
#[test]
|
|
fn test_cmd_arg_quoted_string() {
|
|
fn tester(s: &str, probe: &str) {
|
|
let rv = CmdArg::with_inner(s);
|
|
assert_eq!(rv.to_quoted_string(true), probe);
|
|
}
|
|
|
|
tester(
|
|
r#"C:\Program Files\MyApp\MyApp.exe"#,
|
|
r#""C:\Program Files\MyApp\MyApp.exe""#,
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_cmd_args_quoted_string() {
|
|
fn tester(args: &[&str], probe: &str) {
|
|
let rv = CmdArgs::with_inner(args.iter().map(|s| CmdArg::with_inner(s)));
|
|
assert_eq!(rv.to_quoted_string(), probe);
|
|
}
|
|
|
|
tester(
|
|
&[
|
|
r#"C:\Program Files\MyApp\MyApp.exe"#,
|
|
"--config",
|
|
"ppic.toml",
|
|
],
|
|
r#""C:\Program Files\MyApp\MyApp.exe" --config ppic.toml"#,
|
|
);
|
|
}
|