wdext 0.1.0

A DbgEng wrapper framework
// SPDX-FileCopyrightText: 2026 takubokudori
// SPDX-License-Identifier: MIT OR Apache-2.0
#[cfg(test)]
pub mod tests {
    use crate::tests::util::local_debug2;
    use wdext::*;
    use windows::Win32::Foundation::{E_INVALIDARG, E_UNEXPECTED, S_OK};

    mod util;

    #[test]
    fn test_register_command() {
        #[derive(clap::Parser)]
        struct TestArgs {
            #[arg(long, default_value_t = 50)]
            arg1: u32,
            #[arg(long)]
            arg2: String,
            #[arg(default_value = "foo")]
            arg3: String,
        }

        register_command!(test1, test1_command);
        register_command!(test2, test2_command, TestArgs);
        register_command!(test3, test3_command, TestArgs, my_tokenize_args);
        register_command!(test4, test4_command);

        let ctx = local_debug2();
        let client = ctx.client();

        fn test1_command(_client: &WdClient, args: String) -> WdResult<()> {
            assert_eq!(args, "This is args");
            Ok(())
        }

        fn test2_command(_client: &WdClient, args: TestArgs) -> WdResult<()> {
            assert_eq!(args.arg1, 50);
            assert_eq!(args.arg2, "100");
            assert_eq!(args.arg3, "foo");
            Ok(())
        }

        fn my_tokenize_args(
            command_name: &str,
            args: &str,
        ) -> Option<Vec<String>> {
            let args = args.trim();
            let mut ret = Vec::with_capacity(16);
            ret.push(format!("!{}", command_name));
            ret.extend(args.split_whitespace().map(str::to_owned));
            Some(ret)
        }

        fn test3_command(_client: &WdClient, args: TestArgs) -> WdResult<()> {
            assert_eq!(args.arg1, 50);
            assert_eq!(args.arg2, "\"10");
            assert_eq!(args.arg3, "0\"");
            Ok(())
        }

        fn test4_command(_client: &WdClient, _args: String) -> WdResult<()> {
            panic!("Test");
        }

        unsafe {
            assert_eq!(
                test1(client.0.as_ptr(), c"This is args".as_ptr() as *const _),
                S_OK
            );
            assert_eq!(
                test2(
                    client.0.as_ptr(),
                    c"--arg2 \"100\"".as_ptr() as *const _
                ),
                S_OK
            );
            assert_eq!(
                test2(client.0.as_ptr(), c"-a".as_ptr() as *const _),
                E_INVALIDARG
            );
            assert_eq!(
                test2(client.0.as_ptr(), c"--arg2 \"100".as_ptr() as *const _),
                E_INVALIDARG
            );
            assert_eq!(
                test3(
                    client.0.as_ptr(),
                    c"--arg2 \"10 0\"".as_ptr() as *const _
                ),
                S_OK
            );
            assert_eq!(
                test4(client.0.as_ptr(), "test4".as_ptr() as *const _),
                E_UNEXPECTED
            );
        }
    }
}