Skip to content

Commit

Permalink
test(canvas): simplify canvas line tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed May 12, 2024
1 parent 1b104b5 commit ee90ddc
Showing 1 changed file with 91 additions and 131 deletions.
222 changes: 91 additions & 131 deletions src/widgets/canvas/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,97 @@ fn draw_line_high(painter: &mut Painter, x1: usize, y1: usize, x2: usize, y2: us

#[cfg(test)]
mod tests {
use super::Line;
use crate::{prelude::*, widgets::canvas::Canvas};
use rstest::rstest;

#[track_caller]
fn test(line: &Line, expected_lines: Vec<&str>) {
use super::{super::*, *};
use crate::{buffer::Buffer, layout::Rect};

#[rstest]
#[case::off_grid(&Line::new(-1.0, -1.0, 10.0, 10.0, Color::Red), [" "; 10])]
#[case::off_grid(&Line::new(0.0, 0.0, 11.0, 11.0, Color::Red), [" "; 10])]
#[case::horizontal(&Line::new(0.0, 0.0, 10.0, 0.0, Color::Red), [
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"••••••••••",
])]
#[case::horizontal(&Line::new(10.0, 10.0, 0.0, 10.0, Color::Red), [
"••••••••••",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
])]
#[case::vertical(&Line::new(0.0, 0.0, 0.0, 10.0, Color::Red), ["• "; 10])]
#[case::vertical(&Line::new(10.0, 10.0, 10.0, 0.0, Color::Red), [" •"; 10])]
// dy < dx, x1 < x2
#[case::diagonal(&Line::new(0.0, 0.0, 10.0, 5.0, Color::Red), [
" ",
" ",
" ",
" ",
" •",
" •• ",
" •• ",
" •• ",
" •• ",
"• ",
])]
// dy < dx, x1 > x2
#[case::diagonal(&Line::new(10.0, 0.0, 0.0, 5.0, Color::Red), [
" ",
" ",
" ",
" ",
"• ",
" •• ",
" •• ",
" •• ",
" •• ",
" •",
])]
// dy > dx, y1 < y2
#[case::diagonal(&Line::new(0.0, 0.0, 5.0, 10.0, Color::Red), [
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
"• ",
"• ",
])]
// dy > dx, y1 > y2
#[case::diagonal(&Line::new(0.0, 10.0, 5.0, 0.0, Color::Red), [
"• ",
"• ",
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
])]
fn tests<'expected_line, ExpectedLines>(#[case] line: &Line, #[case] expected: ExpectedLines)
where
ExpectedLines: IntoIterator,
ExpectedLines::Item: Into<crate::text::Line<'expected_line>>,
{
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let canvas = Canvas::default()
.marker(Marker::Dot)
Expand All @@ -125,138 +211,12 @@ mod tests {
.paint(|context| context.draw(line));
canvas.render(buffer.area, &mut buffer);

let mut expected = Buffer::with_lines(expected_lines);
let mut expected = Buffer::with_lines(expected);
for cell in &mut expected.content {
if cell.symbol() == "•" {
cell.set_style(Style::new().red());
}
}
assert_eq!(buffer, expected);
}

#[test]
fn off_grid() {
test(
&Line::new(-1.0, -1.0, 10.0, 10.0, Color::Red),
vec![" "; 10],
);
test(
&Line::new(0.0, 0.0, 11.0, 11.0, Color::Red),
vec![" "; 10],
);
}

#[test]
fn horizontal() {
test(
&Line::new(0.0, 0.0, 10.0, 0.0, Color::Red),
vec![
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"••••••••••",
],
);
test(
&Line::new(10.0, 10.0, 0.0, 10.0, Color::Red),
vec![
"••••••••••",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
],
);
}

#[test]
fn vertical() {
test(
&Line::new(0.0, 0.0, 0.0, 10.0, Color::Red),
vec!["• "; 10],
);
test(
&Line::new(10.0, 10.0, 10.0, 0.0, Color::Red),
vec![" •"; 10],
);
}

#[test]
fn diagonal() {
// dy < dx, x1 < x2
test(
&Line::new(0.0, 0.0, 10.0, 5.0, Color::Red),
vec![
" ",
" ",
" ",
" ",
" •",
" •• ",
" •• ",
" •• ",
" •• ",
"• ",
],
);
// dy < dx, x1 > x2
test(
&Line::new(10.0, 0.0, 0.0, 5.0, Color::Red),
vec![
" ",
" ",
" ",
" ",
"• ",
" •• ",
" •• ",
" •• ",
" •• ",
" •",
],
);
// dy > dx, y1 < y2
test(
&Line::new(0.0, 0.0, 5.0, 10.0, Color::Red),
vec![
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
"• ",
"• ",
],
);
// dy > dx, y1 > y2
test(
&Line::new(0.0, 10.0, 5.0, 0.0, Color::Red),
vec![
"• ",
"• ",
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
" • ",
],
);
}
}

0 comments on commit ee90ddc

Please sign in to comment.