Add match and closure syntax

This commit is contained in:
2025-01-11 00:20:15 -05:00
parent 5602c66b11
commit 79912c3cc6
6 changed files with 736 additions and 312 deletions

View File

@@ -1,3 +1,6 @@
extern crate comlexr;
extern crate rstest;
use comlexr::cmd;
use rstest::rstest;
@@ -77,3 +80,76 @@ fn if_let(#[case] single: Option<&str>, #[case] multi: Option<&str>, #[case] exp
assert_eq!(format!("{command:?}"), expected.to_string());
}
enum TestArgs {
Arg1,
Arg2,
Arg3,
}
#[rstest]
#[case(TestArgs::Arg1, r#""echo" "test" "arg1""#)]
#[case(TestArgs::Arg2, r#""echo" "test" "arg1" "arg2""#)]
#[case(TestArgs::Arg3, r#""echo" "test" "arg1" "arg2" "arg3""#)]
fn match_statement(#[case] match_arg: TestArgs, #[case] expected: &str) {
let command = cmd!(
"echo",
"test",
match match_arg {
TestArgs::Arg1 => "arg1",
TestArgs::Arg2 => ["arg1", "arg2"],
TestArgs::Arg3 => ["arg1", "arg2", "arg3"],
}
);
assert_eq!(format!("{command:?}"), expected.to_string());
}
#[rstest]
#[case(None, None, r#""echo" "test""#)]
#[case(Some("arg"), None, r#""echo" "test" "arg""#)]
#[case(None, Some("arg"), r#""echo" "test" "multi" "arg""#)]
#[case(Some("1"), Some("2"), r#""echo" "test" "1" "multi" "2""#)]
fn multi_match(#[case] single: Option<&str>, #[case] multi: Option<&str>, #[case] expected: &str) {
let command = cmd!(
"echo",
"test",
match (single, multi) {
(None, None) => [],
(Some(single), None) => single,
(None, Some(multi)) => ["multi", multi],
(Some(single), Some(multi)) => [single, "multi", multi],
},
);
assert_eq!(format!("{command:?}"), expected.to_string());
}
#[rstest]
#[case(1, r#""echo" "test" "1" "2" "3""#)]
#[case(2, r#""echo" "test" "2" "4" "6""#)]
#[case(3, r#""echo" "test" "3" "6" "9""#)]
fn closure_expr(#[case] input: usize, #[case] expected: &str) {
let arr = vec![1, 2, 3];
let command = cmd!("echo", "test", || arr
.into_iter()
.map(|i| format!("{}", i * input)));
assert_eq!(format!("{command:?}"), expected.to_string());
}
#[rstest]
#[case(1, r#""echo" "test" "--test=1a" "--test=2a" "--test=3a""#)]
#[case(2, r#""echo" "test" "--test=2a" "--test=4a" "--test=6a""#)]
#[case(3, r#""echo" "test" "--test=3a" "--test=6a" "--test=9a""#)]
fn closure_block(#[case] input: usize, #[case] expected: &str) {
let arr = vec![1, 2, 3];
let suffix = "a";
let command = cmd!("echo", "test", || {
let prefix = "--test";
arr.into_iter()
.map(move |i| format!("{prefix}={}{suffix}", i * input))
});
assert_eq!(format!("{command:?}"), expected.to_string());
}