use blvm_sdk::cli::input::{parse_comma_separated, parse_threshold};
use blvm_sdk::cli::output::{OutputFormat, OutputFormatter};
use std::error::Error;
#[test]
fn test_output_format_text() {
let format = OutputFormat::Text;
let formatter = OutputFormatter::new(format);
let result = formatter.format(&"test");
assert!(result.is_ok());
}
#[test]
fn test_output_format_json() {
let format = OutputFormat::Json;
let formatter = OutputFormatter::new(format);
let result = formatter.format(&"test");
assert!(result.is_ok());
}
#[test]
fn test_output_format_from_str() {
let text_format: Result<OutputFormat, _> = "text".parse();
assert!(text_format.is_ok());
assert_eq!(text_format.unwrap(), OutputFormat::Text);
let json_format: Result<OutputFormat, _> = "json".parse();
assert!(json_format.is_ok());
assert_eq!(json_format.unwrap(), OutputFormat::Json);
}
#[test]
fn test_output_format_invalid() {
let invalid: Result<OutputFormat, _> = "invalid".parse();
assert!(invalid.is_err());
}
#[test]
fn test_output_formatter_creation() {
let formatter = OutputFormatter::new(OutputFormat::Text);
let result = formatter.format(&"test");
assert!(result.is_ok());
}
#[test]
fn test_output_formatter_format_error() {
let formatter = OutputFormatter::new(OutputFormat::Text);
let error: Box<dyn Error> = Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"File not found",
));
let formatted = formatter.format_error(&*error);
assert!(!formatted.is_empty());
}
#[test]
fn test_output_formatter_json_error() {
let formatter = OutputFormatter::new(OutputFormat::Json);
let error: Box<dyn Error> = Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"File not found",
));
let formatted = formatter.format_error(&*error);
assert!(formatted.contains("error") || formatted.contains("Error"));
}
#[test]
fn test_parse_comma_separated_single() {
let result = parse_comma_separated("value1");
assert_eq!(result, vec!["value1".to_string()]);
}
#[test]
fn test_parse_comma_separated_multiple() {
let result = parse_comma_separated("value1,value2,value3");
assert_eq!(result.len(), 3);
assert_eq!(result[0], "value1");
assert_eq!(result[1], "value2");
assert_eq!(result[2], "value3");
}
#[test]
fn test_parse_comma_separated_with_spaces() {
let result = parse_comma_separated("value1 , value2 , value3");
assert_eq!(result.len(), 3);
assert_eq!(result[0], "value1");
assert_eq!(result[1], "value2");
assert_eq!(result[2], "value3");
}
#[test]
fn test_parse_comma_separated_empty() {
let result: Vec<String> = parse_comma_separated("");
assert_eq!(result, Vec::<String>::new());
}
#[test]
fn test_parse_threshold_valid() {
let result = parse_threshold("3-of-5");
assert!(result.is_ok());
let (threshold, total) = result.unwrap();
assert_eq!(threshold, 3);
assert_eq!(total, 5);
}
#[test]
fn test_parse_threshold_different_formats() {
let result = parse_threshold("3-of-5");
assert!(result.is_ok());
let (threshold, total) = result.unwrap();
assert_eq!(threshold, 3);
assert_eq!(total, 5);
assert!(parse_threshold("3/5").is_err());
assert!(parse_threshold("3:5").is_err());
}
#[test]
fn test_parse_threshold_invalid() {
let result = parse_threshold("invalid");
assert!(result.is_err());
}
#[test]
fn test_parse_threshold_edge_cases() {
let result = parse_threshold("1-of-1");
if let Ok((threshold, total)) = result {
assert_eq!(threshold, 1);
assert_eq!(total, 1);
}
let result = parse_threshold("5-of-5");
if let Ok((threshold, total)) = result {
assert_eq!(threshold, 5);
assert_eq!(total, 5);
}
}
#[test]
fn test_cli_output_format_roundtrip() {
let format = OutputFormat::Text;
let parsed: Result<OutputFormat, _> = "text".parse();
assert!(parsed.is_ok());
assert_eq!(parsed.unwrap(), format);
}
#[test]
fn test_cli_output_format_all_variants() {
let formats = vec![OutputFormat::Text, OutputFormat::Json];
for format in formats {
let formatter = OutputFormatter::new(format);
let result = formatter.format(&"test");
assert!(result.is_ok());
}
}
#[test]
fn test_cli_input_parsing_robustness() {
let result = parse_comma_separated("value1,,value3");
assert_eq!(result.len(), 2); assert_eq!(result[0], "value1");
assert_eq!(result[1], "value3");
let result = parse_comma_separated(",,,");
assert_eq!(result.len(), 0); }
#[test]
fn test_cli_threshold_validation() {
let result = parse_threshold("3-of-5");
assert!(result.is_ok());
let (threshold, total) = result.unwrap();
assert!(threshold <= total);
let result = parse_threshold("0-of-5");
assert!(result.is_ok());
let (threshold, total) = result.unwrap();
assert_eq!(threshold, 0);
assert_eq!(total, 5);
}
#[test]
fn test_output_formatter_error_handling() {
let formatter = OutputFormatter::new(OutputFormat::Text);
let io_error: Box<dyn Error> = Box::new(std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"Permission denied",
));
let formatted = formatter.format_error(&*io_error);
assert!(!formatted.is_empty());
let string_error: Box<dyn Error> = "Test error".into();
let formatted = formatter.format_error(&*string_error);
assert!(!formatted.is_empty());
}
#[test]
fn test_input_parsing_error_messages() {
let result = parse_threshold("invalid-format");
assert!(result.is_err());
let error_msg = format!("{}", result.unwrap_err());
assert!(!error_msg.is_empty());
}
#[test]
fn test_output_format_consistency() {
let formatter1 = OutputFormatter::new(OutputFormat::Text);
let formatter2 = OutputFormatter::new(OutputFormat::Text);
let error: Box<dyn Error> = Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Test error",
));
let formatted1 = formatter1.format_error(&*error);
let formatted2 = formatter2.format_error(&*error);
assert_eq!(formatted1, formatted2);
}
#[test]
fn test_output_format_differences() {
let text_formatter = OutputFormatter::new(OutputFormat::Text);
let json_formatter = OutputFormatter::new(OutputFormat::Json);
let error: Box<dyn Error> = Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Test error",
));
let text_output = text_formatter.format_error(&*error);
let json_output = json_formatter.format_error(&*error);
assert_ne!(text_output, json_output);
}