Skip to content

Commit f86a796

Browse files
committed
add clap parsing tests
1 parent 4ba57f2 commit f86a796

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

contrib/clarity-cli/src/main.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,75 @@ fn main() {
676676

677677
process::exit(exit_code);
678678
}
679+
680+
#[cfg(test)]
681+
mod tests {
682+
use super::*;
683+
use clap::CommandFactory;
684+
685+
/// Validates the clap CLI structure has no configuration errors
686+
#[test]
687+
fn verify_cli_structure() {
688+
Cli::command().debug_assert();
689+
}
690+
691+
/// Tests that variadic arguments after positional args are collected correctly.
692+
#[test]
693+
fn test_execute_variadic_args() {
694+
let cli = Cli::try_parse_from([
695+
"clarity-cli",
696+
"execute",
697+
"ST1.contract",
698+
"transfer",
699+
"ST1SENDER",
700+
"/tmp/db",
701+
"u100",
702+
"'ST1RECIPIENT",
703+
"(list u1 u2 u3)",
704+
])
705+
.unwrap();
706+
match cli.command {
707+
Commands::Execute { args, .. } => {
708+
assert_eq!(args, vec!["u100", "'ST1RECIPIENT", "(list u1 u2 u3)"]);
709+
}
710+
_ => panic!("Expected Execute command"),
711+
}
712+
}
713+
714+
/// Tests that commands with many required positional args fail appropriately
715+
/// when args are missing. Execute has the most complex arg structure.
716+
#[test]
717+
fn test_execute_missing_required_args() {
718+
assert!(Cli::try_parse_from(["clarity-cli", "execute"]).is_err());
719+
assert!(Cli::try_parse_from(["clarity-cli", "execute", "ST1.contract"]).is_err());
720+
assert!(
721+
Cli::try_parse_from(["clarity-cli", "execute", "ST1.contract", "func"]).is_err()
722+
);
723+
assert!(
724+
Cli::try_parse_from(["clarity-cli", "execute", "ST1.contract", "func", "SENDER"])
725+
.is_err()
726+
);
727+
}
728+
729+
/// Tests that launch (another command with multiple required args) validates correctly
730+
#[test]
731+
fn test_launch_missing_required_args() {
732+
assert!(Cli::try_parse_from(["clarity-cli", "launch"]).is_err());
733+
assert!(Cli::try_parse_from(["clarity-cli", "launch", "ST1.contract"]).is_err());
734+
assert!(
735+
Cli::try_parse_from(["clarity-cli", "launch", "ST1.contract", "file.clar"]).is_err()
736+
);
737+
}
738+
739+
/// Verifies unknown subcommands are rejected
740+
#[test]
741+
fn test_unknown_command_rejected() {
742+
assert!(Cli::try_parse_from(["clarity-cli", "unknown-command"]).is_err());
743+
}
744+
745+
/// Verifies running with no subcommand is rejected
746+
#[test]
747+
fn test_no_command_rejected() {
748+
assert!(Cli::try_parse_from(["clarity-cli"]).is_err());
749+
}
750+
}

0 commit comments

Comments
 (0)