From 9430d5c5426ba1edfccb674ff13116d134be20a3 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 9 Dec 2023 17:10:36 +0100 Subject: [PATCH] Port test_wgetopt --- fish-rust/src/tests/mod.rs | 1 + fish-rust/src/tests/wgetopt.rs | 43 +++++++++++++++++++++++++++++++ src/fish_tests.cpp | 46 ---------------------------------- 3 files changed, 44 insertions(+), 46 deletions(-) create mode 100644 fish-rust/src/tests/wgetopt.rs diff --git a/fish-rust/src/tests/mod.rs b/fish-rust/src/tests/mod.rs index 69ad761b1..c421d7723 100644 --- a/fish-rust/src/tests/mod.rs +++ b/fish-rust/src/tests/mod.rs @@ -21,6 +21,7 @@ mod string_escape; #[cfg(test)] mod tokenizer; mod topic_monitor; +mod wgetopt; mod prelude { use crate::env::EnvStack; diff --git a/fish-rust/src/tests/wgetopt.rs b/fish-rust/src/tests/wgetopt.rs new file mode 100644 index 000000000..94a94c8c0 --- /dev/null +++ b/fish-rust/src/tests/wgetopt.rs @@ -0,0 +1,43 @@ +use crate::wchar::prelude::*; +use crate::wcstringutil::join_strings; +use crate::wgetopt::{wgetopter_t, wopt, woption, woption_argument_t}; + +#[test] +fn test_wgetopt() { + // Regression test for a crash. + const short_options: &wstr = L!("-a"); + const long_options: &[woption] = &[wopt(L!("add"), woption_argument_t::no_argument, 'a')]; + let mut argv = [ + L!("abbr"), + L!("--add"), + L!("emacsnw"), + L!("emacs"), + L!("-nw"), + ]; + let mut w = wgetopter_t::new(short_options, long_options, &mut argv); + let mut a_count = 0; + let mut arguments = vec![]; + while let Some(opt) = w.wgetopt_long() { + match opt { + 'a' => { + a_count += 1; + } + '\x01' => { + // non-option argument + arguments.push(w.woptarg.as_ref().unwrap().to_owned()); + } + '?' => { + // unrecognized option + if let Some(arg) = w.argv.get(w.woptind - 1) { + arguments.push(arg.to_owned()); + } + } + _ => { + panic!("unexpected option: {:?}", opt); + } + } + } + assert_eq!(a_count, 1); + assert_eq!(arguments.len(), 3); + assert_eq!(join_strings(&arguments, ' '), "emacsnw emacs -nw"); +} diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index b373ea56a..fc1c67bd4 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -2082,51 +2082,6 @@ static void test_fd_event_signaller() { do_test(!sema.try_consume()); } -// todo!("port this") -void test_wgetopt() { - // Regression test for a crash. - const wchar_t *const short_options = L"-a"; - const struct woption long_options[] = { - {L"add", no_argument, 'a'}, - {}, - }; - const wchar_t *argv[] = {L"abbr", L"--add", L"emacsnw", L"emacs", L"-nw", nullptr}; - int argc = builtin_count_args(argv); - wgetopter_t w; - int opt; - int a_count = 0; - std::vector arguments; - while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { - switch (opt) { - case 'a': { - a_count += 1; - break; - } - case 1: { - // non-option argument - do_test(w.woptarg != nullptr); - arguments.push_back(w.woptarg); - break; - } - case '?': { - // unrecognized option - if (argv[w.woptind - 1]) { - do_test(argv[w.woptind - 1] != nullptr); - arguments.push_back(argv[w.woptind - 1]); - } - break; - } - default: { - err(L"Unexpected option '%d'", opt); - break; - } - } - } - do_test(a_count == 1); - do_test(arguments.size() == 3); - do_test(join_strings(arguments, L' ') == L"emacsnw emacs -nw"); -} - void test_rust_smoke() { size_t x = rust::add(37, 5); do_test(x == 42); @@ -2188,7 +2143,6 @@ static const test_t s_tests[]{ {TEST_GROUP("dirname"), test_dirname_basename}, {TEST_GROUP("pipes"), test_pipes}, {TEST_GROUP("fd_event"), test_fd_event_signaller}, - {TEST_GROUP("wgetopt"), test_wgetopt}, {TEST_GROUP("rust_smoke"), test_rust_smoke}, {TEST_GROUP("rust_ffi"), test_rust_ffi}, };