mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
File list tests
This commit is contained in:
@@ -74,8 +74,8 @@ impl OwnStates {
|
|||||||
/// Incremenet list index
|
/// Incremenet list index
|
||||||
pub fn incr_list_index(&mut self) {
|
pub fn incr_list_index(&mut self) {
|
||||||
// Check if index is at last element
|
// Check if index is at last element
|
||||||
if self.list_len + 1 < self.list_len {
|
if self.list_index + 1 < self.list_len {
|
||||||
self.list_len += 1;
|
self.list_index += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,8 +84,8 @@ impl OwnStates {
|
|||||||
/// Decrement list index
|
/// Decrement list index
|
||||||
pub fn decr_list_index(&mut self) {
|
pub fn decr_list_index(&mut self) {
|
||||||
// Check if index is bigger than 0
|
// Check if index is bigger than 0
|
||||||
if self.list_len > 0 {
|
if self.list_index > 0 {
|
||||||
self.list_len -= 1;
|
self.list_index -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +93,7 @@ impl OwnStates {
|
|||||||
///
|
///
|
||||||
/// Reset list index to 0
|
/// Reset list index to 0
|
||||||
pub fn reset_list_index(&mut self) {
|
pub fn reset_list_index(&mut self) {
|
||||||
self.list_len = 0;
|
self.list_index = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,7 +172,7 @@ impl Component for FileList {
|
|||||||
.add_modifier(self.props.get_modifiers()),
|
.add_modifier(self.props.get_modifiers()),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
value: self.get_value(),
|
cursor: self.states.list_index,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -237,7 +237,7 @@ impl Component for FileList {
|
|||||||
}
|
}
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => {
|
||||||
// Report event
|
// Report event
|
||||||
Msg::OnSubmit(Payload::Unumber(self.states.get_list_index()))
|
Msg::OnSubmit(self.get_value())
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// Return key event to activity
|
// Return key event to activity
|
||||||
@@ -252,7 +252,7 @@ impl Component for FileList {
|
|||||||
|
|
||||||
/// ### get_value
|
/// ### get_value
|
||||||
///
|
///
|
||||||
/// Return component value
|
/// Return component value. File list return index
|
||||||
fn get_value(&self) -> Payload {
|
fn get_value(&self) -> Payload {
|
||||||
Payload::Unumber(self.states.get_list_index())
|
Payload::Unumber(self.states.get_list_index())
|
||||||
}
|
}
|
||||||
@@ -268,3 +268,90 @@ impl Component for FileList {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::ui::layout::props::TextParts;
|
||||||
|
|
||||||
|
use crossterm::event::KeyEvent;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ui_layout_components_file_list() {
|
||||||
|
// Make component
|
||||||
|
let mut component: FileList = FileList::new(
|
||||||
|
PropsBuilder::default()
|
||||||
|
.with_texts(TextParts::new(
|
||||||
|
Some(String::from("filelist")),
|
||||||
|
Some(vec![String::from("file1"), String::from("file2")]),
|
||||||
|
))
|
||||||
|
.build(),
|
||||||
|
);
|
||||||
|
// Verify states
|
||||||
|
assert_eq!(component.states.list_index, 0);
|
||||||
|
assert_eq!(component.states.list_len, 2);
|
||||||
|
// Increment list index
|
||||||
|
component.states.list_index += 1;
|
||||||
|
assert_eq!(component.render().unwrap().cursor, 1);
|
||||||
|
// Should umount
|
||||||
|
assert_eq!(component.should_umount(), false);
|
||||||
|
// Update
|
||||||
|
component.update(
|
||||||
|
component
|
||||||
|
.get_props()
|
||||||
|
.with_texts(TextParts::new(
|
||||||
|
Some(String::from("filelist")),
|
||||||
|
Some(vec![
|
||||||
|
String::from("file1"),
|
||||||
|
String::from("file2"),
|
||||||
|
String::from("file3"),
|
||||||
|
]),
|
||||||
|
))
|
||||||
|
.build(),
|
||||||
|
);
|
||||||
|
// Verify states
|
||||||
|
assert_eq!(component.states.list_index, 0);
|
||||||
|
assert_eq!(component.states.list_len, 3);
|
||||||
|
// Render
|
||||||
|
assert_eq!(component.render().unwrap().cursor, 0);
|
||||||
|
// Handle inputs
|
||||||
|
assert_eq!(
|
||||||
|
component.on(InputEvent::Key(KeyEvent::from(KeyCode::Down))),
|
||||||
|
Msg::None
|
||||||
|
);
|
||||||
|
// Index should be incremented
|
||||||
|
assert_eq!(component.states.list_index, 1);
|
||||||
|
// Index should be decremented
|
||||||
|
assert_eq!(
|
||||||
|
component.on(InputEvent::Key(KeyEvent::from(KeyCode::Up))),
|
||||||
|
Msg::None
|
||||||
|
);
|
||||||
|
// Index should be incremented
|
||||||
|
assert_eq!(component.states.list_index, 0);
|
||||||
|
// Index should be 2
|
||||||
|
assert_eq!(
|
||||||
|
component.on(InputEvent::Key(KeyEvent::from(KeyCode::PageDown))),
|
||||||
|
Msg::None
|
||||||
|
);
|
||||||
|
// Index should be incremented
|
||||||
|
assert_eq!(component.states.list_index, 2);
|
||||||
|
// Index should be 0
|
||||||
|
assert_eq!(
|
||||||
|
component.on(InputEvent::Key(KeyEvent::from(KeyCode::PageUp))),
|
||||||
|
Msg::None
|
||||||
|
);
|
||||||
|
// Index should be incremented
|
||||||
|
assert_eq!(component.states.list_index, 0);
|
||||||
|
// Enter
|
||||||
|
assert_eq!(
|
||||||
|
component.on(InputEvent::Key(KeyEvent::from(KeyCode::Enter))),
|
||||||
|
Msg::OnSubmit(Payload::Unumber(0))
|
||||||
|
);
|
||||||
|
// On key
|
||||||
|
assert_eq!(
|
||||||
|
component.on(InputEvent::Key(KeyEvent::from(KeyCode::Backspace))),
|
||||||
|
Msg::OnKey(KeyEvent::from(KeyCode::Backspace))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ use tui::widgets::Widget;
|
|||||||
///
|
///
|
||||||
/// Msg is an enum returned after an event is raised for a certain component
|
/// Msg is an enum returned after an event is raised for a certain component
|
||||||
/// Yep, I took inspiration from Elm.
|
/// Yep, I took inspiration from Elm.
|
||||||
#[derive(std::fmt::Debug)]
|
#[derive(std::fmt::Debug, PartialEq)]
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
OnSubmit(Payload),
|
OnSubmit(Payload),
|
||||||
OnKey(KeyEvent),
|
OnKey(KeyEvent),
|
||||||
@@ -50,7 +50,7 @@ pub enum Msg {
|
|||||||
/// ## Payload
|
/// ## Payload
|
||||||
///
|
///
|
||||||
/// Payload describes a component value
|
/// Payload describes a component value
|
||||||
#[derive(std::fmt::Debug)]
|
#[derive(std::fmt::Debug, PartialEq)]
|
||||||
pub enum Payload {
|
pub enum Payload {
|
||||||
Text(String),
|
Text(String),
|
||||||
Number(isize),
|
Number(isize),
|
||||||
@@ -64,8 +64,8 @@ pub enum Payload {
|
|||||||
///
|
///
|
||||||
/// Render is the object which contains data related to the component render
|
/// Render is the object which contains data related to the component render
|
||||||
pub struct Render {
|
pub struct Render {
|
||||||
pub widget: Box<dyn Widget>,
|
pub widget: Box<dyn Widget>, // Widget
|
||||||
pub value: Payload,
|
pub cursor: usize, // Cursor position
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- States
|
// -- States
|
||||||
|
|||||||
@@ -351,7 +351,7 @@ mod tests {
|
|||||||
assert_eq!(props.italic, true);
|
assert_eq!(props.italic, true);
|
||||||
assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello");
|
assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello");
|
||||||
assert_eq!(props.input_type, InputType::Password);
|
assert_eq!(props.input_type, InputType::Password);
|
||||||
assert_eq!(props.input_len.as_ref().unwrap(), 16);
|
assert_eq!(*props.input_len.as_ref().unwrap(), 16);
|
||||||
assert_eq!(props.value.as_ref().unwrap().as_str(), "Hello");
|
assert_eq!(props.value.as_ref().unwrap().as_str(), "Hello");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
props.texts.body.as_ref().unwrap().get(0).unwrap().as_str(),
|
props.texts.body.as_ref().unwrap().get(0).unwrap().as_str(),
|
||||||
|
|||||||
Reference in New Issue
Block a user