Skip to content

Commit

Permalink
Added tests for widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
WindowGenerator committed Oct 24, 2023
1 parent aac1256 commit 10717e8
Showing 1 changed file with 95 additions and 64 deletions.
159 changes: 95 additions & 64 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,40 @@ impl<'a, 'b, B: Backend> FrameWrapperInterface for FrameWrapper<'a, 'b, B> {
mod test {
use mockall::predicate;

use crate::expected_input::MockExpectedInputInterface;
use crate::expected_input::{MockExpectedInputInterface, ExpectedInput};
use ratatui::{
backend::TestBackend,
buffer::Buffer
};

use super::*;

fn get_expected_input() -> ExpectedInput {
let config = Config::default();
ExpectedInput::new(&config).expect("unable to create expected input")

}

/// Tests the [`Runner`] widget against the expected [`Buffer`] by rendering it onto an equal
/// area and comparing the rendered and expected content.
fn test_runner(runner: &mut Runner, expected: Buffer, run_before: fn(frame: &mut FrameWrapper<'_, '_, TestBackend>, runner: &mut Runner) -> ()) {
let backend = TestBackend::new(expected.area.width, expected.area.height);
let mut terminal = Terminal::new(backend).unwrap();

terminal
.draw(|f| {
let mut frame_wrapper = FrameWrapper::new(f);
run_before(&mut frame_wrapper, runner);
})
.unwrap();

terminal.backend().assert_buffer(&expected);
}

#[test]
fn should_render_single_line_input() {
let config = Config::default();

let mut expected_input = MockExpectedInputInterface::default();

expected_input
Expand Down Expand Up @@ -517,99 +544,103 @@ mod test {
expected_input
.expect_get_string()
.with(predicate::eq(4 * 3))
.return_const("foobarbazqux".to_string());
.return_const("foobarbazquxaboba".to_string());

let mut runner = Runner::new(config, expected_input);

runner.input_mode = InputMode::Editing;
runner.input = "foobar".to_string();

let mut frame = MockFrameWrapperInterface::default();

frame.expect_size().times(2).return_const(Rect {
let mut buffer = Buffer::empty(Rect {
x: 0,
y: 0,
width: 4,
height: 3,
});
for (x, line) in ["foob"].iter().enumerate() {
buffer.set_string(x as u16, 0, line, Style::default().fg(Color::Green));
}

frame
.expect_render_widget::<Paragraph>()
.times(10)
.return_const(());
for (x, line) in ["ar"].iter().enumerate() {
buffer.set_string(x as u16, 1, line, Style::default().fg(Color::Green));
}

frame
.expect_set_cursor()
.with(predicate::eq(2), predicate::eq(1))
.times(1)
.return_const(());
for (x, line) in ["ba"].iter().enumerate() {
buffer.set_string(x as u16 + 2, 1, line, Style::default().fg(Color::Gray));
}

runner.render(&mut frame, 0);
// for (x, line) in ["pres"].iter().enumerate() {
// buffer.set_string(x as u16, 2, line, Style::default().fg(Color::Yellow));
// }

test_runner(&mut runner, buffer, |frame, runner| {
runner.render(frame, 0);
});
}

#[test]
fn should_print_input() {
let config = Config::default();
let expected_input = MockExpectedInputInterface::default();
let expected_input = get_expected_input();
let mut runner = Runner::new(config, expected_input);

runner.input = "foo".to_string();

let mut frame = MockFrameWrapperInterface::default();

frame
.expect_render_widget::<Paragraph>()
.times(3)
.return_const(());
let mut buffer = Buffer::empty(Rect {
x: 0,
y: 0,
width: 50,
height: 1,
});
for (y, line) in ["foo"].iter().enumerate() {
buffer.set_string(0, y as u16, line, Style::default().fg(Color::Green));
}

runner.print_input(
&mut frame,
"foo",
Rect {
x: 0,
y: 0,
width: 50,
height: 1,
},
50,
);
test_runner(&mut runner, buffer, |frame, runner| {
runner.print_input(
frame,
"foo",
Rect {
x: 0,
y: 0,
width: 50,
height: 1,
},
50,
);
});
}

#[test]
fn should_print_block_of_text() {
let config = Config::default();
let expected_input = MockExpectedInputInterface::default();
let runner = Runner::new(config, expected_input);

let mut frame = MockFrameWrapperInterface::default();
let expected_input = get_expected_input();
let mut runner = Runner::new(config, expected_input);

frame
.expect_render_widget::<Paragraph>()
.withf(|_widget: &Paragraph<'_>, area| {
*area
== Rect {
x: 0,
y: 0,
width: 50,
height: 1,
}
})
.times(1)
.return_const(());
let mut buffer = Buffer::empty(Rect {
x: 0,
y: 0,
width: 50,
height: 1,
});
for (y, line) in ["foo"].iter().enumerate() {
buffer.set_string(0, y as u16, line, Style::default().fg(Color::Gray));
}

runner.print_block_of_text(
&mut frame,
"foo".to_string(),
Rect {
x: 0,
y: 0,
width: 50,
height: 1,
},
Color::Gray,
false,
false,
);
test_runner(&mut runner, buffer, |frame, runner| {
runner.print_block_of_text(
frame,
"foo".to_string(),
Rect {
x: 0,
y: 0,
width: 50,
height: 1,
},
Color::Gray,
false,
false,
);
});
}

#[test]
Expand Down

0 comments on commit 10717e8

Please sign in to comment.