Skip to content

Commit 037092b

Browse files
committed
test(render/chart_sparkline): cover render leaving the slot untouched when handed a non-NumberSeries body, the line style emitting a blank chart for an empty value window, and draw_line_series/draw_zero_baseline's defence-in-depth guards for a zero-sized area, an over-long value slice, and mid/bottom rows past the buffer
1 parent 8a0b507 commit 037092b

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

‎src/render/chart_sparkline.rs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,63 @@ mod tests {
331331
assert_eq!(buf.cell((1, 2)).unwrap().symbol(), ZERO_BASELINE);
332332
assert_eq!(buf.cell((2, 2)).unwrap().symbol(), ZERO_BASELINE);
333333
}
334+
335+
#[test]
336+
fn render_ignores_a_non_number_series_body() {
337+
use ratatui::{Terminal, backend::TestBackend};
338+
let mut terminal = Terminal::new(TestBackend::new(10, 3)).unwrap();
339+
let body = Body::Text(crate::payload::TextData { value: "x".into() });
340+
let opts = RenderOptions::default();
341+
let theme = Theme::default();
342+
let registry = Registry::with_builtins();
343+
terminal
344+
.draw(|frame| {
345+
let area = frame.area();
346+
ChartSparklineRenderer.render(frame, area, &body, &opts, &theme, &registry);
347+
})
348+
.unwrap();
349+
let buf = terminal.backend().buffer().clone();
350+
assert!(
351+
(0..buf.area.height).all(|y| line_text(&buf, y).trim().is_empty()),
352+
"a non-NumberSeries body must leave the slot untouched"
353+
);
354+
}
355+
356+
#[test]
357+
fn line_style_handles_empty_values() {
358+
let registry = Registry::with_builtins();
359+
#[derive(serde::Deserialize)]
360+
struct W {
361+
render: RenderSpec,
362+
}
363+
let w: W =
364+
toml::from_str(r#"render = { type = "chart_sparkline", style = "line" }"#).unwrap();
365+
let buf = render_to_buffer_with_spec(&payload(vec![]), Some(&w.render), &registry, 10, 3);
366+
assert!((0..buf.area.height).all(|y| line_text(&buf, y).trim().is_empty()));
367+
}
368+
369+
/// `render_sparkline` always truncates the value window to the chart width before calling
370+
/// these helpers, so their out-of-range guards (zero-sized area, an over-long slice, a
371+
/// mid/bottom row past the buffer) are defence-in-depth — exercise them directly.
372+
#[test]
373+
fn line_and_baseline_helpers_guard_out_of_range_inputs() {
374+
use ratatui::{Terminal, backend::TestBackend};
375+
let mut terminal = Terminal::new(TestBackend::new(4, 3)).unwrap();
376+
let theme = Theme::default();
377+
terminal
378+
.draw(|frame| {
379+
// Each clause of the degenerate-area short-circuit.
380+
draw_line_series(frame, Rect::new(0, 0, 0, 3), &[1, 2], &theme);
381+
draw_line_series(frame, Rect::new(0, 0, 4, 0), &[1, 2], &theme);
382+
draw_line_series(frame, Rect::new(0, 0, 4, 3), &[], &theme);
383+
draw_zero_baseline(frame, Rect::new(0, 0, 4, 0), &[0, 0], &theme);
384+
// A slice longer than the area width hits the in-loop break.
385+
draw_line_series(frame, Rect::new(0, 0, 2, 3), &[1, 2, 3, 4], &theme);
386+
draw_zero_baseline(frame, Rect::new(0, 0, 2, 3), &[0, 0, 0, 0], &theme);
387+
// A mid/bottom row past the buffer makes `cell_mut` return `None`.
388+
draw_line_series(frame, Rect::new(0, 0, 4, 20), &[0, 7], &theme);
389+
draw_zero_baseline(frame, Rect::new(0, 0, 4, 20), &[0, 0], &theme);
390+
})
391+
.unwrap();
392+
}
334393
}

0 commit comments

Comments
 (0)