Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(barchart): divide by zero in render #525

Merged
merged 1 commit into from Sep 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/widgets/barchart.rs
Expand Up @@ -506,10 +506,15 @@
buf.set_style(area, self.style);

self.render_block(&mut area, buf);

if area.area() == 0 {
return;
}
if self.data.is_empty() {
return;
}
if self.bar_width == 0 {
return;

Check warning on line 516 in src/widgets/barchart.rs

View check run for this annotation

Codecov / codecov/patch

src/widgets/barchart.rs#L516

Added line #L516 was not covered by tests
}

let label_height = self.label_height();
if area.height <= label_height {
Expand Down Expand Up @@ -1180,4 +1185,16 @@
]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn handles_zero_width() {
// this test is to ensure that a BarChart with zero bar / gap width does not panic
let chart = BarChart::default()
.data(&[("A", 1)])
.bar_width(0)
.bar_gap(0);
let mut buffer = Buffer::empty(Rect::new(0, 0, 0, 10));
chart.render(buffer.area, &mut buffer);
assert_buffer_eq!(buffer, Buffer::empty(Rect::new(0, 0, 0, 10)));
}
}