mod common;
use multiline_input::Builder;
use common::mock_terminal::{ MockTerminal, key };
use crossterm::event::{ KeyCode, KeyModifiers };
#[ test ]
fn test_submit_single_line_workflow()
{
let mut terminal = MockTerminal::new(
true, ( 80, 24 ) );
terminal.push_key( key( KeyCode::Char( 'h' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'e' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'l' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'l' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'o' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Enter, KeyModifiers::NONE ) );
let editor = Builder::new()
.prompt( "Input:" )
.build_with( terminal );
let result = editor.collect();
assert!( result.is_ok() );
let text = result.unwrap();
assert_eq!( text, Some( "hello".to_string() ) );
}
#[ test ]
fn test_cancel_on_esc_workflow()
{
let mut terminal = MockTerminal::new( true, ( 80, 24 ) );
terminal.push_key( key( KeyCode::Char( 'h' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'e' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Esc, KeyModifiers::NONE ) );
let editor = Builder::new()
.prompt( "Input:" )
.build_with( terminal );
let result = editor.collect();
assert!( result.is_ok() );
let text = result.unwrap();
assert_eq!( text, None );
}
#[ test ]
fn test_multiline_editing_workflow()
{
let mut terminal = MockTerminal::new( true, ( 80, 24 ) );
terminal.push_key( key( KeyCode::Char( 'h' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'i' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Enter, KeyModifiers::CONTROL ) );
terminal.push_key( key( KeyCode::Char( 'b' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'y' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Enter, KeyModifiers::NONE ) );
let editor = Builder::new()
.prompt( "Input:" )
.build_with( terminal );
let result = editor.collect();
assert!( result.is_ok() );
let text = result.unwrap();
assert_eq!( text, Some( "hi\nby".to_string() ) );
}
#[ test ]
fn test_validation_empty_workflow()
{
let mut terminal = MockTerminal::new( true, ( 80, 24 ) );
terminal.push_key( key( KeyCode::Enter, KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'h' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'i' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Enter, KeyModifiers::NONE ) );
let editor = Builder::new()
.prompt( "Input:" )
.allow_empty( false ) .build_with( terminal );
let result = editor.collect();
assert!( result.is_ok() );
let text = result.unwrap();
assert_eq!( text, Some( "hi".to_string() ) );
}
#[ test ]
fn test_shift_enter_inserts_newline_workflow()
{
let mut terminal = MockTerminal::new( true, ( 80, 24 ) );
terminal.push_key( key( KeyCode::Char( 'h' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'i' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Enter, KeyModifiers::SHIFT ) );
terminal.push_key( key( KeyCode::Char( 'b' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Char( 'y' ), KeyModifiers::NONE ) );
terminal.push_key( key( KeyCode::Enter, KeyModifiers::NONE ) );
let editor = Builder::new()
.prompt( "Input:" )
.build_with( terminal );
let result = editor.collect();
assert!( result.is_ok() );
let text = result.unwrap();
assert_eq!( text, Some( "hi\nby".to_string() ) );
}