use blvm_sdk::cli::output::{OutputFormat, OutputFormatter};
use serde_json::json;
#[test]
fn test_output_format_from_str() {
use std::str::FromStr;
assert_eq!(OutputFormat::from_str("text").unwrap(), OutputFormat::Text);
assert_eq!(OutputFormat::from_str("json").unwrap(), OutputFormat::Json);
assert_eq!(OutputFormat::from_str("TEXT").unwrap(), OutputFormat::Text);
assert_eq!(OutputFormat::from_str("JSON").unwrap(), OutputFormat::Json);
assert_eq!(OutputFormat::from_str("txt").unwrap(), OutputFormat::Text);
assert!(OutputFormat::from_str("invalid").is_err());
assert!(OutputFormat::from_str("").is_err());
}
#[test]
fn test_output_format_debug() {
assert!(format!("{:?}", OutputFormat::Text).contains("Text"));
assert!(format!("{:?}", OutputFormat::Json).contains("Json"));
}
#[test]
fn test_output_formatter_new() {
let formatter = OutputFormatter::new(OutputFormat::Text);
let test_string = "test".to_string();
assert!(formatter.format(&test_string).is_ok());
let formatter = OutputFormatter::new(OutputFormat::Json);
assert!(formatter.format(&test_string).is_ok());
}
#[test]
fn test_output_formatter_format_text() {
let formatter = OutputFormatter::new(OutputFormat::Text);
let result = formatter.format(&"test string");
assert_eq!(result, Ok("test string".to_string()));
let result = formatter.format(&42);
assert_eq!(result, Ok("42".to_string()));
let result = formatter.format(&true);
assert_eq!(result, Ok("true".to_string()));
let result = formatter.format(&"");
assert_eq!(result, Ok("".to_string()));
}
#[test]
fn test_output_formatter_format_json() {
let formatter = OutputFormatter::new(OutputFormat::Json);
let data = json!({"key": "value"});
let result = formatter.format(&data);
assert!(result.is_ok());
let json_str = result.unwrap();
assert!(json_str.contains("\"key\""));
assert!(json_str.contains("\"value\""));
let complex_data = json!({
"nested": {
"array": [1, 2, 3],
"string": "test",
"boolean": true,
"null": null
}
});
let result = formatter.format(&complex_data);
assert!(result.is_ok());
let json_str = result.unwrap();
assert!(json_str.contains("\"nested\""));
assert!(json_str.contains("\"array\""));
assert!(json_str.contains("\"string\""));
assert!(json_str.contains("\"boolean\""));
assert!(json_str.contains("\"null\""));
let array_data = json!([1, 2, 3, "test"]);
let result = formatter.format(&array_data);
assert!(result.is_ok());
let json_str = result.unwrap();
assert!(json_str.starts_with("["));
assert!(json_str.ends_with("]"));
}
#[test]
fn test_output_formatter_format_json_error() {
let formatter = OutputFormatter::new(OutputFormat::Json);
let large_data = json!({
"large_number": 1e308,
"infinity": f64::INFINITY
});
let result = formatter.format(&large_data);
assert!(result.is_ok());
}
#[test]
fn test_output_formatter_format_edge_cases() {
let text_formatter = OutputFormatter::new(OutputFormat::Text);
let json_formatter = OutputFormatter::new(OutputFormat::Json);
let special_chars = "test\nwith\ttabs\r\nand\0null";
let text_result = text_formatter.format(&special_chars);
assert_eq!(text_result, Ok(special_chars.to_string()));
let json_result = json_formatter.format(&special_chars);
assert!(json_result.is_ok());
let json_str = json_result.unwrap();
assert!(json_str.contains("\\n"));
assert!(json_str.contains("\\t"));
assert!(json_str.contains("\\r"));
let unicode_str = "ζ΅θ―δΈζπ";
let text_result = text_formatter.format(&unicode_str);
assert_eq!(text_result, Ok(unicode_str.to_string()));
let json_result = json_formatter.format(&unicode_str);
assert!(json_result.is_ok());
let json_str = json_result.unwrap();
assert!(json_str.contains("ζ΅θ―"));
assert!(json_str.contains("π"));
}
#[test]
fn test_output_formatter_format_with_serde_json_value() {
let formatter = OutputFormatter::new(OutputFormat::Json);
let value = serde_json::Value::String("test".to_string());
let result = formatter.format(&value);
assert!(result.is_ok());
assert_eq!(result.unwrap(), "\"test\"");
let value = serde_json::Value::Number(serde_json::Number::from(42));
let result = formatter.format(&value);
assert!(result.is_ok());
assert_eq!(result.unwrap(), "42");
let value = serde_json::Value::Bool(true);
let result = formatter.format(&value);
assert!(result.is_ok());
assert_eq!(result.unwrap(), "true");
let value = serde_json::Value::Null;
let result = formatter.format(&value);
assert!(result.is_ok());
assert_eq!(result.unwrap(), "null");
}