#[cfg(test)]
mod tests {
use cc_switch::cli::completion::*;
#[test]
fn test_generate_aliases_fish() {
let result = generate_aliases("fish");
assert!(result.is_ok(), "Should generate fish aliases successfully");
}
#[test]
fn test_generate_aliases_zsh() {
let result = generate_aliases("zsh");
assert!(result.is_ok(), "Should generate zsh aliases successfully");
}
#[test]
fn test_generate_aliases_bash() {
let result = generate_aliases("bash");
assert!(result.is_ok(), "Should generate bash aliases successfully");
}
#[test]
fn test_generate_aliases_unsupported_shell() {
let result = generate_aliases("unsupported");
assert!(result.is_err(), "Should fail for unsupported shell");
let error_msg = result.unwrap_err().to_string();
assert!(error_msg.contains("Unsupported shell: unsupported"));
assert!(error_msg.contains("Supported shells: fish, zsh, bash"));
}
#[test]
fn test_generate_aliases_empty_string() {
let result = generate_aliases("");
assert!(result.is_err(), "Should fail for empty shell string");
}
#[test]
fn test_generate_aliases_case_sensitivity() {
let result_upper = generate_aliases("FISH");
let result_mixed = generate_aliases("Fish");
assert!(
result_upper.is_err(),
"Should be case sensitive - FISH should fail"
);
assert!(
result_mixed.is_err(),
"Should be case sensitive - Fish should fail"
);
}
#[test]
fn test_generate_aliases_special_characters() {
let test_cases = vec!["fish!", "z$h", "bash#", "fish\n", "zsh\t"];
for shell in test_cases {
let result = generate_aliases(shell);
assert!(
result.is_err(),
"Should fail for shell with special characters: {}",
shell
);
}
}
#[test]
fn test_generate_completion_fish() {
let result = generate_completion("fish");
assert!(
result.is_ok(),
"Should generate fish completion successfully"
);
}
#[test]
fn test_generate_completion_zsh() {
let result = generate_completion("zsh");
assert!(
result.is_ok(),
"Should generate zsh completion successfully"
);
}
#[test]
fn test_generate_completion_bash() {
let result = generate_completion("bash");
assert!(
result.is_ok(),
"Should generate bash completion successfully"
);
}
#[test]
fn test_generate_completion_elvish() {
let result = generate_completion("elvish");
assert!(
result.is_ok(),
"Should generate elvish completion successfully"
);
}
#[test]
fn test_generate_completion_powershell() {
let result = generate_completion("powershell");
assert!(
result.is_ok(),
"Should generate powershell completion successfully"
);
}
#[test]
fn test_generate_completion_unsupported_shell() {
let result = generate_completion("unsupported");
assert!(result.is_err(), "Should fail for unsupported shell");
let error_msg = result.unwrap_err().to_string();
assert!(error_msg.contains("Unsupported shell: unsupported"));
assert!(error_msg.contains("Supported shells: fish, zsh, bash, elvish, powershell"));
}
#[test]
fn test_generate_completion_nushell_not_supported() {
let result = generate_completion("nushell");
assert!(
result.is_err(),
"Should fail for nushell as it's not implemented"
);
}
#[test]
fn test_generate_completion_case_sensitivity() {
let result_upper = generate_completion("FISH");
let result_mixed = generate_completion("Fish");
assert!(
result_upper.is_err(),
"Should be case sensitive - FISH should fail"
);
assert!(
result_mixed.is_err(),
"Should be case sensitive - Fish should fail"
);
}
#[test]
fn test_generate_completion_empty_string() {
let result = generate_completion("");
assert!(result.is_err(), "Should fail for empty shell string");
}
#[test]
fn test_list_aliases_for_completion_empty_storage() {
let result = list_aliases_for_completion();
if let Err(e) = &result {
eprintln!("Error: {:?}", e);
}
assert!(result.is_ok(), "Should handle empty storage gracefully");
}
#[test]
fn test_list_aliases_for_completion_with_current_config() {
let result = list_aliases_for_completion();
assert!(
result.is_ok(),
"Should handle configurations with 'current' alias"
);
}
#[test]
fn test_list_aliases_for_completion_multiple_configs() {
let result = list_aliases_for_completion();
assert!(result.is_ok(), "Should handle multiple configurations");
}
#[test]
fn test_supported_shells_list() {
let supported_alias_shells = vec!["fish", "zsh", "bash"];
let supported_completion_shells = vec!["fish", "zsh", "bash", "elvish", "powershell"];
for shell in supported_alias_shells {
let result = generate_aliases(shell);
assert!(
result.is_ok(),
"Shell {} should be supported for aliases",
shell
);
}
for shell in supported_completion_shells {
let result = generate_completion(shell);
assert!(
result.is_ok(),
"Shell {} should be supported for completion",
shell
);
}
}
#[test]
fn test_unsupported_shells_consistency() {
let unsupported_shells = vec!["tcsh", "csh", "sh", "nushell", "ion", "xonsh"];
for shell in unsupported_shells {
let alias_result = generate_aliases(shell);
let completion_result = generate_completion(shell);
assert!(
alias_result.is_err(),
"Shell {} should not be supported for aliases",
shell
);
if shell != "nushell" {
assert!(
completion_result.is_err(),
"Shell {} should not be supported for completion",
shell
);
}
}
}
#[test]
fn test_alias_shell_subset_of_completion_shells() {
let alias_shells = vec!["fish", "zsh", "bash"];
for shell in alias_shells {
let alias_result = generate_aliases(shell);
let completion_result = generate_completion(shell);
assert!(alias_result.is_ok(), "Alias shell {} should work", shell);
assert!(
completion_result.is_ok(),
"Completion shell {} should work",
shell
);
}
}
#[test]
fn test_alias_error_message_quality() {
let result = generate_aliases("invalid_shell");
assert!(result.is_err());
let error_msg = result.unwrap_err().to_string();
assert!(error_msg.contains("Unsupported shell"));
assert!(error_msg.contains("invalid_shell"));
assert!(error_msg.contains("fish"));
assert!(error_msg.contains("zsh"));
assert!(error_msg.contains("bash"));
}
#[test]
fn test_completion_error_message_quality() {
let result = generate_completion("invalid_shell");
assert!(result.is_err());
let error_msg = result.unwrap_err().to_string();
assert!(error_msg.contains("Unsupported shell"));
assert!(error_msg.contains("invalid_shell"));
assert!(error_msg.contains("fish"));
assert!(error_msg.contains("zsh"));
assert!(error_msg.contains("bash"));
assert!(error_msg.contains("elvish"));
assert!(error_msg.contains("powershell"));
}
#[test]
fn test_whitespace_in_shell_names() {
let whitespace_shells = vec![" fish", "fish ", " fish ", "fi sh", "\tfish", "fish\n"];
for shell in whitespace_shells {
let alias_result = generate_aliases(shell);
let completion_result = generate_completion(shell);
assert!(
alias_result.is_err(),
"Should reject shell name with whitespace: '{}'",
shell
);
assert!(
completion_result.is_err(),
"Should reject shell name with whitespace: '{}'",
shell
);
}
}
#[test]
fn test_unicode_shell_names() {
let unicode_shells = vec!["fish🐟", "zsh📚", "bash💥", "ﻪtset"];
for shell in unicode_shells {
let alias_result = generate_aliases(shell);
let completion_result = generate_completion(shell);
assert!(
alias_result.is_err(),
"Should reject unicode shell name: '{}'",
shell
);
assert!(
completion_result.is_err(),
"Should reject unicode shell name: '{}'",
shell
);
}
}
#[test]
fn test_very_long_shell_names() {
let long_shell = "a".repeat(1000);
let alias_result = generate_aliases(&long_shell);
let completion_result = generate_completion(&long_shell);
assert!(alias_result.is_err(), "Should reject very long shell name");
assert!(
completion_result.is_err(),
"Should reject very long shell name"
);
}
#[test]
fn test_function_consistency_supported_shells() {
let common_shells = vec!["fish", "zsh", "bash"];
for shell in common_shells {
let alias_result = generate_aliases(shell);
let completion_result = generate_completion(shell);
assert!(
alias_result.is_ok() && completion_result.is_ok(),
"Both alias and completion should work for shell: {}",
shell
);
}
}
#[test]
fn test_function_consistency_unsupported_shells() {
let unsupported_shells = vec!["tcsh", "csh", "invalid"];
for shell in unsupported_shells {
let alias_result = generate_aliases(shell);
assert!(
alias_result.is_err(),
"Alias should fail for unsupported shell: {}",
shell
);
}
}
#[test]
fn test_multiple_calls_same_shell() {
for _ in 0..10 {
let result = generate_aliases("fish");
assert!(result.is_ok(), "Multiple calls should work");
}
}
#[test]
fn test_alternating_shell_calls() {
let shells = vec!["fish", "zsh", "bash"];
for i in 0..30 {
let shell = shells[i % shells.len()];
let alias_result = generate_aliases(shell);
let completion_result = generate_completion(shell);
assert!(
alias_result.is_ok(),
"Alternating call {} should work for aliases",
i
);
assert!(
completion_result.is_ok(),
"Alternating call {} should work for completion",
i
);
}
}
#[test]
fn test_list_aliases_error_handling() {
let result = list_aliases_for_completion();
match result {
Ok(_) => {
}
Err(e) => {
let error_msg = e.to_string();
assert!(!error_msg.is_empty(), "Error message should not be empty");
}
}
}
#[test]
fn test_shell_output_generation_does_not_panic() {
let shells = vec!["fish", "zsh", "bash", "elvish", "powershell"];
for shell in shells {
let alias_result = if shell == "fish" || shell == "zsh" || shell == "bash" {
generate_aliases(shell)
} else {
Ok(()) };
let completion_result = generate_completion(shell);
if shell == "fish" || shell == "zsh" || shell == "bash" {
assert!(
alias_result.is_ok(),
"Alias generation should not panic for {}",
shell
);
}
assert!(
completion_result.is_ok(),
"Completion generation should not panic for {}",
shell
);
}
}
#[test]
fn test_command_factory_usage() {
use cc_switch::cli::Cli;
use clap::CommandFactory;
let app = Cli::command();
assert_eq!(app.get_name(), "cc-switch");
let subcommand_names: Vec<&str> = app.get_subcommands().map(|cmd| cmd.get_name()).collect();
assert!(
subcommand_names.contains(&"add"),
"Should have 'add' subcommand"
);
assert!(
subcommand_names.contains(&"list"),
"Should have 'list' subcommand"
);
assert!(
subcommand_names.contains(&"completion"),
"Should have 'completion' subcommand"
);
}
#[test]
fn test_multiple_operations_performance() {
let start = std::time::Instant::now();
for _ in 0..100 {
let _ = generate_aliases("fish");
let _ = generate_completion("zsh");
}
let duration = start.elapsed();
assert!(
duration.as_secs() < 10,
"Operations should complete within 10 seconds, took {:?}",
duration
);
}
}