Skip to content

Commit

Permalink
test(canvas): add tests for rectangle (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Aug 25, 2023
1 parent e4bcf78 commit ad4d6e7
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/widgets/canvas/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,91 @@ impl Shape for Rectangle {
}
}
}

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

#[test]
fn draw_block_lines() {
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let canvas = Canvas::default()
.marker(Marker::Block)
.x_bounds([0.0, 10.0])
.y_bounds([0.0, 10.0])
.paint(|context| {
context.draw(&Rectangle {
x: 0.0,
y: 0.0,
width: 10.0,
height: 10.0,
color: Color::Red,
});
});
canvas.render(buffer.area, &mut buffer);
let mut expected = Buffer::with_lines(vec![
"██████████",
"█ █",
"█ █",
"█ █",
"█ █",
"█ █",
"█ █",
"█ █",
"█ █",
"██████████",
]);
expected.set_style(buffer.area, Style::new().red());
expected.set_style(buffer.area.inner(&Margin::new(1, 1)), Style::reset());
assert_buffer_eq!(buffer, expected);
}

#[test]
fn draw_braille_lines() {
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let canvas = Canvas::default()
.marker(Marker::Braille)
.x_bounds([0.0, 10.0])
.y_bounds([0.0, 10.0])
.paint(|context| {
// a rectangle that will draw the outside part of the braille
context.draw(&Rectangle {
x: 0.0,
y: 0.0,
width: 10.0,
height: 10.0,
color: Color::Red,
});
// a rectangle that will draw the inside part of the braille
context.draw(&Rectangle {
x: 2.0,
y: 1.75,
width: 6.5,
height: 6.5,
color: Color::Green,
});
});
canvas.render(buffer.area, &mut buffer);
let mut expected = Buffer::with_lines(vec![
"⡏⠉⠉⠉⠉⠉⠉⠉⠉⢹",
"⡇⢠⠤⠤⠤⠤⠤⠤⡄⢸",
"⡇⢸ ⡇⢸",
"⡇⢸ ⡇⢸",
"⡇⢸ ⡇⢸",
"⡇⢸ ⡇⢸",
"⡇⢸ ⡇⢸",
"⡇⢸ ⡇⢸",
"⡇⠈⠉⠉⠉⠉⠉⠉⠁⢸",
"⣇⣀⣀⣀⣀⣀⣀⣀⣀⣸",
]);
expected.set_style(buffer.area, Style::new().red());
expected.set_style(buffer.area.inner(&Margin::new(1, 1)), Style::new().green());
expected.set_style(buffer.area.inner(&Margin::new(2, 2)), Style::reset());
assert_buffer_eq!(buffer, expected);
}
}

0 comments on commit ad4d6e7

Please sign in to comment.