Skip to content

Commit 0d78814

Browse files
committed
test(render/text_ascii): cover figlet_wrapped's raw-input fallback when the font renders nothing (empty string and all-control-code input), blocks_width and pixel_name taking the conservative defaults on an unrecognised PixelSize, and render's defence-in-depth Body::Text guard drawing a blank cell instead of panicking when handed a non-Text body
1 parent 531bcca commit 0d78814

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

‎src/render/text_ascii.rs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,45 @@ mod tests {
518518
8
519519
);
520520
}
521+
522+
#[test]
523+
fn figlet_wrapped_returns_raw_input_when_font_cannot_render_it() {
524+
// figlet_rs `convert` yields `None` for an empty string and for input whose every
525+
// character is absent from the font (e.g. C0 control codes). `figlet_wrapped` then
526+
// falls back to the raw input rather than emitting a silently-empty cell.
527+
assert_eq!(figlet_wrapped("", None, 200, 200), "");
528+
assert_eq!(figlet_wrapped("\u{1}\u{2}", None, 200, 200), "\u{1}\u{2}");
529+
}
530+
531+
#[test]
532+
fn blocks_width_and_pixel_name_fall_back_for_unrecognised_pixel_size() {
533+
// `blocks_width` only special-cases the three sizes `parse_pixel_size` can produce;
534+
// any other `tui-big-text` variant takes the conservative 4-cols-per-glyph fallback.
535+
assert_eq!(blocks_width("hi", PixelSize::HalfHeight), 8);
536+
assert_eq!(pixel_name(PixelSize::HalfHeight), "other");
537+
}
538+
539+
#[test]
540+
fn render_draws_nothing_for_a_non_text_body() {
541+
// The renderer accepts `Text` only; `render_payload` already gates shape mismatches,
542+
// but the `if let Body::Text` guard inside `render` is the defence-in-depth that must
543+
// neither panic nor draw if it is ever handed another body directly.
544+
use ratatui::{Terminal, backend::TestBackend};
545+
546+
let mut terminal = Terminal::new(TestBackend::new(20, 4)).unwrap();
547+
let body = Body::TextBlock(TextBlockData {
548+
lines: vec!["ignored".into()],
549+
});
550+
let registry = Registry::with_builtins();
551+
let theme = Theme::default();
552+
let opts = RenderOptions::default();
553+
terminal
554+
.draw(|f| TextAsciiRenderer.render(f, f.area(), &body, &opts, &theme, &registry))
555+
.unwrap();
556+
let buf = terminal.backend().buffer().clone();
557+
assert!(
558+
(0..buf.area.width).all(|x| buf.cell((x, 0)).unwrap().symbol() == " "),
559+
"a non-text body must leave the cell blank",
560+
);
561+
}
521562
}

0 commit comments

Comments
 (0)