Get macro functional with tests

This commit is contained in:
2025-01-10 18:47:52 -05:00
parent 85dce7f39f
commit 5602c66b11
5 changed files with 667 additions and 86 deletions

79
tests/cmd.rs Normal file
View File

@@ -0,0 +1,79 @@
use comlexr::cmd;
use rstest::rstest;
#[test]
fn expression() {
let command = cmd!("echo", "test");
assert_eq!(format!("{command:?}"), r#""echo" "test""#.to_string());
}
#[rstest]
#[case(false, false, r#""echo" "test""#)]
#[case(true, false, r#""echo" "test" "single""#)]
#[case(false, true, r#""echo" "test" "multi" "arg""#)]
#[case(true, true, r#""echo" "test" "single" "multi" "arg""#)]
fn if_statement(#[case] single: bool, #[case] multi: bool, #[case] expected: &str) {
let command = cmd!(
"echo",
"test",
if single => "single",
if multi => [
"multi",
"arg",
],
);
assert_eq!(format!("{command:?}"), expected.to_string());
}
#[rstest]
#[case(&[], r#""echo" "test""#)]
#[case(&["1", "2"], r#""echo" "test" "1" "2""#)]
fn for_iter(#[case] iter: &[&str], #[case] expected: &str) {
let command = cmd!(
"echo",
"test",
for iter,
);
assert_eq!(format!("{command:?}"), expected.to_string());
}
#[rstest]
#[case(&[], &[], r#""echo" "test""#)]
#[case(&["1", "2"], &[], r#""echo" "test" "1" "2""#)]
#[case(&[], &["3", "4"], r#""echo" "test" "multi" "3" "multi" "4""#)]
#[case(&["1", "2"], &["3", "4"], r#""echo" "test" "1" "2" "multi" "3" "multi" "4""#)]
fn for_in(#[case] single_iter: &[&str], #[case] multi_iter: &[&str], #[case] expected: &str) {
let command = cmd!(
"echo",
"test",
for arg in single_iter => arg,
for arg in multi_iter => [
"multi",
arg,
],
);
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 if_let(#[case] single: Option<&str>, #[case] multi: Option<&str>, #[case] expected: &str) {
let command = cmd!(
"echo",
"test",
if let Some(arg) = single => arg,
if let Some(arg) = multi => [
"multi",
arg,
],
);
assert_eq!(format!("{command:?}"), expected.to_string());
}