use pixelsrc::alias::{format_columns, parse_grid_row};
use pixelsrc::models::TtpObject;
use pixelsrc::parser::parse_stream;
use std::io::Cursor;
#[test]
fn test_inline_basic() {
let input = include_str!("../../../examples/demos/cli/format/inline_sprite.jsonl");
let reader = Cursor::new(input);
let result = parse_stream(reader);
let sprite = result
.objects
.iter()
.find_map(|o| match o {
TtpObject::Sprite(s) if s.name == "face" => Some(s),
_ => None,
})
.expect("Sprite 'face' should exist");
let rows: Vec<Vec<String>> = sprite.grid.iter().map(|row| parse_grid_row(row)).collect();
assert_eq!(rows.len(), 5, "Grid should have 5 rows");
assert!(rows.iter().all(|r| r.len() == 5), "Each row should have 5 tokens");
}
#[test]
fn test_inline_column_alignment() {
let rows = vec![
vec!["{_}".to_string(), "{_}".to_string(), "{body}".to_string()],
vec!["{_}".to_string(), "{skin_highlight}".to_string(), "{body}".to_string()],
vec!["{eye}".to_string(), "{skin_shadow}".to_string(), "{body_dark}".to_string()],
];
let formatted = format_columns(rows);
assert_eq!(formatted.len(), 3, "Should have 3 formatted rows");
let first_body_pos = formatted[0].find("{body}").expect("{{body}} should be in first row");
let second_body_pos = formatted[1].find("{body}").expect("{{body}} should be in second row");
let third_body_pos =
formatted[2].find("{body_dark}").expect("{{body_dark}} should be in third row");
assert_eq!(first_body_pos, second_body_pos, "Third column should be aligned across rows");
assert_eq!(first_body_pos, third_body_pos, "Third column should be aligned with body_dark");
}
#[test]
fn test_inline_token_preservation() {
let input = include_str!("../../../examples/demos/cli/format/inline_sprite.jsonl");
let reader = Cursor::new(input);
let result = parse_stream(reader);
let sprite = result
.objects
.iter()
.find_map(|o| match o {
TtpObject::Sprite(s) if s.name == "face" => Some(s),
_ => None,
})
.expect("Sprite 'face' should exist");
let rows: Vec<Vec<String>> = sprite.grid.iter().map(|row| parse_grid_row(row)).collect();
let formatted = format_columns(rows);
assert!(formatted.iter().any(|row| row.contains("{hair}")), "Should preserve {{hair}} token");
assert!(formatted.iter().any(|row| row.contains("{skin}")), "Should preserve {{skin}} token");
assert!(formatted.iter().any(|row| row.contains("{eye}")), "Should preserve {{eye}} token");
assert!(formatted.iter().any(|row| row.contains("{shirt}")), "Should preserve {{shirt}} token");
}
#[test]
fn test_inline_empty_grid() {
let rows: Vec<Vec<String>> = vec![];
let formatted = format_columns(rows);
assert!(formatted.is_empty(), "Empty input should produce empty output");
}
#[test]
fn test_inline_single_row() {
let rows = vec![vec!["{a}".to_string(), "{bb}".to_string(), "{ccc}".to_string()]];
let formatted = format_columns(rows);
assert_eq!(formatted.len(), 1, "Should have 1 formatted row");
assert!(formatted[0].contains("{a}"), "Should contain first token");
assert!(formatted[0].contains("{ccc}"), "Should contain last token");
}
#[test]
fn test_inline_wide_tokens() {
let rows = vec![
vec!["{short}".to_string(), "{x}".to_string()],
vec!["{very_long_token_name}".to_string(), "{y}".to_string()],
];
let formatted = format_columns(rows);
let first_x_pos = formatted[0].find("{x}").expect("{{x}} should be in first row");
let second_y_pos = formatted[1].find("{y}").expect("{{y}} should be in second row");
assert_eq!(first_x_pos, second_y_pos, "Second column should be aligned");
}