From 10717e8002fc712a2d6cdbdf068db26f485de386 Mon Sep 17 00:00:00 2001 From: WindowGenerator Date: Wed, 25 Oct 2023 00:43:42 +0400 Subject: [PATCH] Added tests for widgets --- src/runner.rs | 159 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 64 deletions(-) diff --git a/src/runner.rs b/src/runner.rs index f0c8870..8232f78 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -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 @@ -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::() - .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::() - .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::() - .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]