Selected item is always at the bottom of list #41

Merged
wojtek merged 7 commits from 40---selected-item-is-always-at-the-bottom-of-list into main 2023-04-27 19:05:37 +02:00
2 changed files with 98 additions and 98 deletions
Showing only changes of commit 1be636b648 - Show all commits

View File

@ -178,7 +178,7 @@ mod tests {
Terminal::new(backend).unwrap() Terminal::new(backend).unwrap()
} }
pub fn app(collection: Collection) -> MhUi<MockCollectionManager> { pub fn ui(collection: Collection) -> MhUi<MockCollectionManager> {
let mut collection_manager = MockCollectionManager::new(); let mut collection_manager = MockCollectionManager::new();
collection_manager collection_manager
@ -206,8 +206,8 @@ mod tests {
let mut handler = MockEventHandler::new(); let mut handler = MockEventHandler::new();
handler handler
.expect_handle_next_event() .expect_handle_next_event()
.return_once(|app: &mut MhUi<MockCollectionManager>| { .return_once(|ui: &mut MhUi<MockCollectionManager>| {
app.quit(); ui.quit();
Ok(()) Ok(())
}); });
handler handler
@ -216,19 +216,19 @@ mod tests {
#[test] #[test]
fn run() { fn run() {
let terminal = terminal(); let terminal = terminal();
let app = app(COLLECTION.to_owned()); let ui = ui(COLLECTION.to_owned());
let listener = listener(); let listener = listener();
let handler = handler(); let handler = handler();
let result = Tui::main(terminal, app, handler, listener); let result = Tui::main(terminal, ui, handler, listener);
assert!(result.is_ok()); assert!(result.is_ok());
} }
#[test] #[test]
fn event_error() { fn event_error() {
let terminal = terminal(); let terminal = terminal();
let app = app(COLLECTION.to_owned()); let ui = ui(COLLECTION.to_owned());
let listener = listener(); let listener = listener();
@ -237,7 +237,7 @@ mod tests {
.expect_handle_next_event() .expect_handle_next_event()
.return_once(|_| Err(EventError::Recv)); .return_once(|_| Err(EventError::Recv));
let result = Tui::main(terminal, app, handler, listener); let result = Tui::main(terminal, ui, handler, listener);
assert!(result.is_err()); assert!(result.is_err());
assert_eq!( assert_eq!(
result.unwrap_err(), result.unwrap_err(),
@ -248,7 +248,7 @@ mod tests {
#[test] #[test]
fn listener_error() { fn listener_error() {
let terminal = terminal(); let terminal = terminal();
let app = app(COLLECTION.to_owned()); let ui = ui(COLLECTION.to_owned());
let error = EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "error")); let error = EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "error"));
let listener_handle: thread::JoinHandle<EventError> = thread::spawn(|| error); let listener_handle: thread::JoinHandle<EventError> = thread::spawn(|| error);
@ -262,7 +262,7 @@ mod tests {
.expect_handle_next_event() .expect_handle_next_event()
.return_once(|_| Err(EventError::Recv)); .return_once(|_| Err(EventError::Recv));
let result = Tui::main(terminal, app, handler, listener); let result = Tui::main(terminal, ui, handler, listener);
assert!(result.is_err()); assert!(result.is_err());
let error = EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "error")); let error = EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "error"));
@ -272,7 +272,7 @@ mod tests {
#[test] #[test]
fn listener_panic() { fn listener_panic() {
let terminal = terminal(); let terminal = terminal();
let app = app(COLLECTION.to_owned()); let ui = ui(COLLECTION.to_owned());
let listener_handle: thread::JoinHandle<EventError> = thread::spawn(|| panic!()); let listener_handle: thread::JoinHandle<EventError> = thread::spawn(|| panic!());
while !listener_handle.is_finished() {} while !listener_handle.is_finished() {}
@ -285,7 +285,7 @@ mod tests {
.expect_handle_next_event() .expect_handle_next_event()
.return_once(|_| Err(EventError::Recv)); .return_once(|_| Err(EventError::Recv));
let result = Tui::main(terminal, app, handler, listener); let result = Tui::main(terminal, ui, handler, listener);
assert!(result.is_err()); assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::ListenerPanic); assert_eq!(result.unwrap_err(), Error::ListenerPanic);
} }

View File

@ -601,7 +601,7 @@ impl<CM: CollectionManager> Ui for MhUi<CM> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tests::{MockCollectionManager, COLLECTION}; use crate::tests::{MockCollectionManager, COLLECTION};
use crate::tui::tests::{app, terminal}; use crate::tui::tests::{terminal, ui};
use super::*; use super::*;
@ -758,7 +758,7 @@ mod tests {
} }
#[test] #[test]
fn app_running() { fn ui_running() {
let mut collection_manager = MockCollectionManager::new(); let mut collection_manager = MockCollectionManager::new();
collection_manager collection_manager
@ -769,15 +769,15 @@ mod tests {
.expect_get_collection() .expect_get_collection()
.return_const(COLLECTION.to_owned()); .return_const(COLLECTION.to_owned());
let mut app = MhUi::new(collection_manager).unwrap(); let mut ui = MhUi::new(collection_manager).unwrap();
assert!(app.is_running()); assert!(ui.is_running());
app.quit(); ui.quit();
assert!(!app.is_running()); assert!(!ui.is_running());
} }
#[test] #[test]
fn app_modifiers() { fn ui_modifiers() {
let mut collection_manager = MockCollectionManager::new(); let mut collection_manager = MockCollectionManager::new();
collection_manager collection_manager
@ -788,91 +788,91 @@ mod tests {
.expect_get_collection() .expect_get_collection()
.return_const(COLLECTION.to_owned()); .return_const(COLLECTION.to_owned());
let mut app = MhUi::new(collection_manager).unwrap(); let mut ui = MhUi::new(collection_manager).unwrap();
assert!(app.is_running()); assert!(ui.is_running());
assert_eq!(app.selection.active, Category::Artist); assert_eq!(ui.selection.active, Category::Artist);
assert_eq!(app.selection.artist.state.selected(), Some(0)); assert_eq!(ui.selection.artist.state.selected(), Some(0));
assert_eq!(app.selection.artist.album.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.state.selected(), Some(0));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(0));
app.increment_selection(); ui.increment_selection();
assert_eq!(app.selection.active, Category::Artist); assert_eq!(ui.selection.active, Category::Artist);
assert_eq!(app.selection.artist.state.selected(), Some(1)); assert_eq!(ui.selection.artist.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.state.selected(), Some(0));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(0));
app.increment_category(); ui.increment_category();
assert_eq!(app.selection.active, Category::Album); assert_eq!(ui.selection.active, Category::Album);
assert_eq!(app.selection.artist.state.selected(), Some(1)); assert_eq!(ui.selection.artist.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.state.selected(), Some(0));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(0));
app.increment_selection(); ui.increment_selection();
assert_eq!(app.selection.active, Category::Album); assert_eq!(ui.selection.active, Category::Album);
assert_eq!(app.selection.artist.state.selected(), Some(1)); assert_eq!(ui.selection.artist.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(0));
app.increment_category(); ui.increment_category();
assert_eq!(app.selection.active, Category::Track); assert_eq!(ui.selection.active, Category::Track);
assert_eq!(app.selection.artist.state.selected(), Some(1)); assert_eq!(ui.selection.artist.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(0));
app.increment_selection(); ui.increment_selection();
assert_eq!(app.selection.active, Category::Track); assert_eq!(ui.selection.active, Category::Track);
assert_eq!(app.selection.artist.state.selected(), Some(1)); assert_eq!(ui.selection.artist.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(1));
app.increment_category(); ui.increment_category();
assert_eq!(app.selection.active, Category::Track); assert_eq!(ui.selection.active, Category::Track);
assert_eq!(app.selection.artist.state.selected(), Some(1)); assert_eq!(ui.selection.artist.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(1));
app.decrement_selection(); ui.decrement_selection();
assert_eq!(app.selection.active, Category::Track); assert_eq!(ui.selection.active, Category::Track);
assert_eq!(app.selection.artist.state.selected(), Some(1)); assert_eq!(ui.selection.artist.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(0));
app.increment_selection(); ui.increment_selection();
app.decrement_category(); ui.decrement_category();
assert_eq!(app.selection.active, Category::Album); assert_eq!(ui.selection.active, Category::Album);
assert_eq!(app.selection.artist.state.selected(), Some(1)); assert_eq!(ui.selection.artist.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(1));
app.decrement_selection(); ui.decrement_selection();
assert_eq!(app.selection.active, Category::Album); assert_eq!(ui.selection.active, Category::Album);
assert_eq!(app.selection.artist.state.selected(), Some(1)); assert_eq!(ui.selection.artist.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.state.selected(), Some(0));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(0));
app.increment_selection(); ui.increment_selection();
app.decrement_category(); ui.decrement_category();
assert_eq!(app.selection.active, Category::Artist); assert_eq!(ui.selection.active, Category::Artist);
assert_eq!(app.selection.artist.state.selected(), Some(1)); assert_eq!(ui.selection.artist.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(0));
app.decrement_selection(); ui.decrement_selection();
assert_eq!(app.selection.active, Category::Artist); assert_eq!(ui.selection.active, Category::Artist);
assert_eq!(app.selection.artist.state.selected(), Some(0)); assert_eq!(ui.selection.artist.state.selected(), Some(0));
assert_eq!(app.selection.artist.album.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.state.selected(), Some(0));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(0));
app.increment_category(); ui.increment_category();
app.increment_selection(); ui.increment_selection();
app.decrement_category(); ui.decrement_category();
app.decrement_selection(); ui.decrement_selection();
app.decrement_category(); ui.decrement_category();
assert_eq!(app.selection.active, Category::Artist); assert_eq!(ui.selection.active, Category::Artist);
assert_eq!(app.selection.artist.state.selected(), Some(0)); assert_eq!(ui.selection.artist.state.selected(), Some(0));
assert_eq!(app.selection.artist.album.state.selected(), Some(1)); assert_eq!(ui.selection.artist.album.state.selected(), Some(1));
assert_eq!(app.selection.artist.album.track.state.selected(), Some(0)); assert_eq!(ui.selection.artist.album.track.state.selected(), Some(0));
} }
#[test] #[test]
@ -1031,23 +1031,23 @@ mod tests {
#[test] #[test]
fn empty() { fn empty() {
let mut terminal = terminal(); let mut terminal = terminal();
let mut app = app(vec![]); let mut ui = ui(vec![]);
terminal.draw(|frame| app.render(frame)).unwrap(); terminal.draw(|frame| ui.render(frame)).unwrap();
} }
#[test] #[test]
fn collection() { fn collection() {
let mut terminal = terminal(); let mut terminal = terminal();
let mut app = app(COLLECTION.to_owned()); let mut ui = ui(COLLECTION.to_owned());
terminal.draw(|frame| app.render(frame)).unwrap(); terminal.draw(|frame| ui.render(frame)).unwrap();
// Change the track (which has a different track format). // Change the track (which has a different track format).
app.increment_category(); ui.increment_category();
app.increment_category(); ui.increment_category();
app.increment_selection(); ui.increment_selection();
terminal.draw(|frame| app.render(frame)).unwrap(); terminal.draw(|frame| ui.render(frame)).unwrap();
} }
} }