Complete unit tests
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 1m43s
Cargo CI / Lint (pull_request) Successful in 1m14s

This commit is contained in:
Wojciech Kozlowski 2024-03-01 22:00:59 +01:00
parent 588abb12b8
commit abc51ffb02
5 changed files with 321 additions and 140 deletions

View File

@ -387,8 +387,9 @@ mod tests {
let sel = &search.inner.selection; let sel = &search.inner.selection;
assert_eq!(sel.artist.album.state.list.selected(), Some(0)); assert_eq!(sel.artist.album.state.list.selected(), Some(0));
search.state.string = String::from("album_title a.b"); let search = search.append_character('a').unwrap_search();
search.incremental_search(false); let search = search.append_character('.').unwrap_search();
let search = search.append_character('b').unwrap_search();
let sel = &search.inner.selection; let sel = &search.inner.selection;
assert_eq!(sel.artist.album.state.list.selected(), Some(1)); assert_eq!(sel.artist.album.state.list.selected(), Some(1));
@ -409,8 +410,11 @@ mod tests {
let sel = &search.inner.selection; let sel = &search.inner.selection;
assert_eq!(sel.artist.album.track.state.list.selected(), Some(0)); assert_eq!(sel.artist.album.track.state.list.selected(), Some(0));
search.state.string = String::from("track a.a.2"); let search = search.append_character('a').unwrap_search();
search.incremental_search(false); let search = search.append_character('.').unwrap_search();
let search = search.append_character('a').unwrap_search();
let search = search.append_character('.').unwrap_search();
let search = search.append_character('2').unwrap_search();
let sel = &search.inner.selection; let sel = &search.inner.selection;
assert_eq!(sel.artist.album.track.state.list.selected(), Some(1)); assert_eq!(sel.artist.album.track.state.list.selected(), Some(1));

View File

@ -1,6 +1,9 @@
use std::cmp; use std::cmp;
use musichoard::collection::{album::{Album, AlbumId}, track::Track}; use musichoard::collection::{
album::{Album, AlbumId},
track::Track,
};
use crate::tui::app::selection::{ use crate::tui::app::selection::{
track::{IdSelectTrack, TrackSelection}, track::{IdSelectTrack, TrackSelection},
@ -172,59 +175,92 @@ mod tests {
use super::*; use super::*;
#[test]
fn album_select() {
let albums = &COLLECTION[0].albums;
assert!(albums.len() > 1);
let mut sel = AlbumSelection::initialise(albums);
assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.selected_track(), Some(0));
sel.select(albums, None);
assert_eq!(sel.selected(), None);
assert_eq!(sel.selected_track(), Some(0));
sel.select(albums, Some(albums.len()));
assert_eq!(sel.selected(), Some(albums.len() - 1));
assert_eq!(sel.selected_track(), Some(0));
sel.select_track(albums, None);
assert_eq!(sel.selected(), Some(albums.len() - 1));
assert_eq!(sel.selected_track(), None);
sel.reset_track(albums);
assert_eq!(sel.selected(), Some(albums.len() - 1));
assert_eq!(sel.selected_track(), Some(0));
sel.select_track(albums, Some(1));
assert_eq!(sel.selected(), Some(albums.len() - 1));
assert_eq!(sel.selected_track(), Some(1));
sel.reset(albums);
assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.selected_track(), Some(0));
}
#[test] #[test]
fn album_selection() { fn album_delta_line() {
let albums = &COLLECTION[0].albums; let albums = &COLLECTION[0].albums;
assert!(albums.len() > 1); assert!(albums.len() > 1);
let mut empty = AlbumSelection::initialise(&[]); let mut empty = AlbumSelection::initialise(&[]);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
assert_eq!(empty.track.state.list.selected(), None); assert_eq!(empty.selected_track(), None);
empty.increment(albums, Delta::Line); empty.increment(albums, Delta::Line);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
assert_eq!(empty.track.state.list.selected(), None); assert_eq!(empty.selected_track(), None);
empty.decrement(albums, Delta::Line); empty.decrement(albums, Delta::Line);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
assert_eq!(empty.track.state.list.selected(), None); assert_eq!(empty.selected_track(), None);
let mut sel = AlbumSelection::initialise(albums); let mut sel = AlbumSelection::initialise(albums);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.track.state.list.selected(), Some(0)); assert_eq!(sel.selected_track(), Some(0));
sel.increment_track(albums, Delta::Line); sel.increment_track(albums, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.track.state.list.selected(), Some(1)); assert_eq!(sel.selected_track(), Some(1));
// Verify that decrement that doesn't change index does not reset track. // Verify that decrement that doesn't change index does not reset track.
sel.decrement(albums, Delta::Line); sel.decrement(albums, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.track.state.list.selected(), Some(1)); assert_eq!(sel.selected_track(), Some(1));
sel.increment(albums, Delta::Line); sel.increment(albums, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(1)); assert_eq!(sel.selected(), Some(1));
assert_eq!(sel.track.state.list.selected(), Some(0)); assert_eq!(sel.selected_track(), Some(0));
sel.decrement(albums, Delta::Line); sel.decrement(albums, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.track.state.list.selected(), Some(0)); assert_eq!(sel.selected_track(), Some(0));
for _ in 0..(albums.len() + 5) { for _ in 0..(albums.len() + 5) {
sel.increment(albums, Delta::Line); sel.increment(albums, Delta::Line);
} }
assert_eq!(sel.state.list.selected(), Some(albums.len() - 1)); assert_eq!(sel.selected(), Some(albums.len() - 1));
assert_eq!(sel.track.state.list.selected(), Some(0)); assert_eq!(sel.selected_track(), Some(0));
sel.increment_track(albums, Delta::Line); sel.increment_track(albums, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(albums.len() - 1)); assert_eq!(sel.selected(), Some(albums.len() - 1));
assert_eq!(sel.track.state.list.selected(), Some(1)); assert_eq!(sel.selected_track(), Some(1));
// Verify that increment that doesn't change index does not reset track. // Verify that increment that doesn't change index does not reset track.
sel.increment(albums, Delta::Line); sel.increment(albums, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(albums.len() - 1)); assert_eq!(sel.selected(), Some(albums.len() - 1));
assert_eq!(sel.track.state.list.selected(), Some(1)); assert_eq!(sel.selected_track(), Some(1));
} }
#[test] #[test]
@ -233,46 +269,46 @@ mod tests {
assert!(albums.len() > 1); assert!(albums.len() > 1);
let empty = AlbumSelection::initialise(&[]); let empty = AlbumSelection::initialise(&[]);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
let mut sel = AlbumSelection::initialise(albums); let mut sel = AlbumSelection::initialise(albums);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.track.state.list.selected(), Some(0)); assert_eq!(sel.selected_track(), Some(0));
assert!(albums.len() >= 4); assert!(albums.len() >= 4);
sel.state.height = 3; sel.state.height = 3;
sel.increment_track(albums, Delta::Line); sel.increment_track(albums, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.track.state.list.selected(), Some(1)); assert_eq!(sel.selected_track(), Some(1));
// Verify that decrement that doesn't change index does not reset track. // Verify that decrement that doesn't change index does not reset track.
sel.decrement(albums, Delta::Page); sel.decrement(albums, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.track.state.list.selected(), Some(1)); assert_eq!(sel.selected_track(), Some(1));
sel.increment(albums, Delta::Page); sel.increment(albums, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(2)); assert_eq!(sel.selected(), Some(2));
assert_eq!(sel.track.state.list.selected(), Some(0)); assert_eq!(sel.selected_track(), Some(0));
sel.decrement(albums, Delta::Page); sel.decrement(albums, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.track.state.list.selected(), Some(0)); assert_eq!(sel.selected_track(), Some(0));
for _ in 0..(albums.len() + 5) { for _ in 0..(albums.len() + 5) {
sel.increment(albums, Delta::Page); sel.increment(albums, Delta::Page);
} }
assert_eq!(sel.state.list.selected(), Some(albums.len() - 1)); assert_eq!(sel.selected(), Some(albums.len() - 1));
assert_eq!(sel.track.state.list.selected(), Some(0)); assert_eq!(sel.selected_track(), Some(0));
sel.increment_track(albums, Delta::Line); sel.increment_track(albums, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(albums.len() - 1)); assert_eq!(sel.selected(), Some(albums.len() - 1));
assert_eq!(sel.track.state.list.selected(), Some(1)); assert_eq!(sel.selected_track(), Some(1));
// Verify that increment that doesn't change index does not reset track. // Verify that increment that doesn't change index does not reset track.
sel.increment(albums, Delta::Page); sel.increment(albums, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(albums.len() - 1)); assert_eq!(sel.selected(), Some(albums.len() - 1));
assert_eq!(sel.track.state.list.selected(), Some(1)); assert_eq!(sel.selected_track(), Some(1));
} }
#[test] #[test]

View File

@ -204,59 +204,114 @@ mod tests {
use super::*; use super::*;
#[test]
fn artist_select() {
let artists = &COLLECTION;
assert!(artists.len() > 1);
let mut sel = ArtistSelection::initialise(artists);
assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.selected_album(), Some(0));
assert_eq!(sel.selected_track(), Some(0));
sel.select(artists, None);
assert_eq!(sel.selected(), None);
assert_eq!(sel.selected_album(), Some(0));
assert_eq!(sel.selected_track(), Some(0));
sel.select(artists, Some(artists.len()));
assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.selected_album(), Some(0));
assert_eq!(sel.selected_track(), Some(0));
sel.select_track(artists, None);
assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.selected_album(), Some(0));
assert_eq!(sel.selected_track(), None);
sel.select_album(artists, None);
assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.selected_album(), None);
assert_eq!(sel.selected_track(), None);
sel.reset_album(artists);
assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.selected_album(), Some(0));
assert_eq!(sel.selected_track(), Some(0));
sel.select_track(artists, None);
assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.selected_album(), Some(0));
assert_eq!(sel.selected_track(), None);
sel.reset_track(artists);
assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.selected_album(), Some(0));
assert_eq!(sel.selected_track(), Some(0));
sel.select_album(artists, Some(1));
assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.selected_album(), Some(1));
assert_eq!(sel.selected_track(), Some(0));
sel.reset(artists);
assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.selected_album(), Some(0));
assert_eq!(sel.selected_track(), Some(0));
}
#[test] #[test]
fn artist_selection() { fn artist_delta_line() {
let artists = &COLLECTION; let artists = &COLLECTION;
assert!(artists.len() > 1); assert!(artists.len() > 1);
let mut empty = ArtistSelection::initialise(&[]); let mut empty = ArtistSelection::initialise(&[]);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
assert_eq!(empty.album.state.list.selected(), None); assert_eq!(empty.album.selected(), None);
empty.increment(artists, Delta::Line); empty.increment(artists, Delta::Line);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
assert_eq!(empty.album.state.list.selected(), None); assert_eq!(empty.album.selected(), None);
empty.decrement(artists, Delta::Line); empty.decrement(artists, Delta::Line);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
assert_eq!(empty.album.state.list.selected(), None); assert_eq!(empty.album.selected(), None);
let mut sel = ArtistSelection::initialise(artists); let mut sel = ArtistSelection::initialise(artists);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.album.state.list.selected(), Some(0)); assert_eq!(sel.album.selected(), Some(0));
sel.increment_album(artists, Delta::Line); sel.increment_album(artists, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.album.state.list.selected(), Some(1)); assert_eq!(sel.album.selected(), Some(1));
// Verify that decrement that doesn't change index does not reset album. // Verify that decrement that doesn't change index does not reset album.
sel.decrement(artists, Delta::Line); sel.decrement(artists, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.album.state.list.selected(), Some(1)); assert_eq!(sel.album.selected(), Some(1));
sel.increment(artists, Delta::Line); sel.increment(artists, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(1)); assert_eq!(sel.selected(), Some(1));
assert_eq!(sel.album.state.list.selected(), Some(0)); assert_eq!(sel.album.selected(), Some(0));
sel.decrement(artists, Delta::Line); sel.decrement(artists, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.album.state.list.selected(), Some(0)); assert_eq!(sel.album.selected(), Some(0));
for _ in 0..(artists.len() + 5) { for _ in 0..(artists.len() + 5) {
sel.increment(artists, Delta::Line); sel.increment(artists, Delta::Line);
} }
assert_eq!(sel.state.list.selected(), Some(artists.len() - 1)); assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.album.state.list.selected(), Some(0)); assert_eq!(sel.album.selected(), Some(0));
sel.increment_album(artists, Delta::Line); sel.increment_album(artists, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(artists.len() - 1)); assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.album.state.list.selected(), Some(1)); assert_eq!(sel.album.selected(), Some(1));
// Verify that increment that doesn't change index does not reset album. // Verify that increment that doesn't change index does not reset album.
sel.increment(artists, Delta::Line); sel.increment(artists, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(artists.len() - 1)); assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.album.state.list.selected(), Some(1)); assert_eq!(sel.album.selected(), Some(1));
} }
#[test] #[test]
@ -265,46 +320,46 @@ mod tests {
assert!(artists.len() > 1); assert!(artists.len() > 1);
let empty = ArtistSelection::initialise(&[]); let empty = ArtistSelection::initialise(&[]);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
let mut sel = ArtistSelection::initialise(artists); let mut sel = ArtistSelection::initialise(artists);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.album.state.list.selected(), Some(0)); assert_eq!(sel.album.selected(), Some(0));
assert!(artists.len() >= 4); assert!(artists.len() >= 4);
sel.state.height = 3; sel.state.height = 3;
sel.increment_album(artists, Delta::Line); sel.increment_album(artists, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.album.state.list.selected(), Some(1)); assert_eq!(sel.album.selected(), Some(1));
// Verify that decrement that doesn't change index does not reset album. // Verify that decrement that doesn't change index does not reset album.
sel.decrement(artists, Delta::Page); sel.decrement(artists, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.album.state.list.selected(), Some(1)); assert_eq!(sel.album.selected(), Some(1));
sel.increment(artists, Delta::Page); sel.increment(artists, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(2)); assert_eq!(sel.selected(), Some(2));
assert_eq!(sel.album.state.list.selected(), Some(0)); assert_eq!(sel.album.selected(), Some(0));
sel.decrement(artists, Delta::Page); sel.decrement(artists, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert_eq!(sel.album.state.list.selected(), Some(0)); assert_eq!(sel.album.selected(), Some(0));
for _ in 0..(artists.len() + 5) { for _ in 0..(artists.len() + 5) {
sel.increment(artists, Delta::Page); sel.increment(artists, Delta::Page);
} }
assert_eq!(sel.state.list.selected(), Some(artists.len() - 1)); assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.album.state.list.selected(), Some(0)); assert_eq!(sel.album.selected(), Some(0));
sel.increment_album(artists, Delta::Line); sel.increment_album(artists, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(artists.len() - 1)); assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.album.state.list.selected(), Some(1)); assert_eq!(sel.album.selected(), Some(1));
// Verify that increment that doesn't change index does not reset album. // Verify that increment that doesn't change index does not reset album.
sel.increment(artists, Delta::Page); sel.increment(artists, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(artists.len() - 1)); assert_eq!(sel.selected(), Some(artists.len() - 1));
assert_eq!(sel.album.state.list.selected(), Some(1)); assert_eq!(sel.album.selected(), Some(1));
} }
#[test] #[test]

View File

@ -232,81 +232,148 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn selection() { fn selection_select() {
let mut selection = Selection::new(&COLLECTION);
selection.select(&COLLECTION, Some(1));
selection.increment_category();
selection.select(&COLLECTION, Some(1));
selection.increment_category();
selection.select(&COLLECTION, Some(1));
assert_eq!(selection.active, Category::Track);
assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(1));
selection.reset(&COLLECTION);
assert_eq!(selection.active, Category::Track);
assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.select(&COLLECTION, Some(1));
assert_eq!(selection.active, Category::Track);
assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(1));
selection.decrement_category();
selection.reset(&COLLECTION);
assert_eq!(selection.active, Category::Album);
assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.selected(), Some(0));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.select(&COLLECTION, Some(1));
assert_eq!(selection.active, Category::Album);
assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.decrement_category();
selection.reset(&COLLECTION);
assert_eq!(selection.active, Category::Artist);
assert_eq!(selection.artist.selected(), Some(0));
assert_eq!(selection.artist.album.selected(), Some(0));
assert_eq!(selection.artist.album.track.selected(), Some(0));
}
#[test]
fn selection_delta() {
let mut selection = Selection::new(&COLLECTION); let mut selection = Selection::new(&COLLECTION);
assert_eq!(selection.active, Category::Artist); assert_eq!(selection.active, Category::Artist);
assert_eq!(selection.artist.state.list.selected(), Some(0)); assert_eq!(selection.selected(), Some(0));
assert_eq!(selection.artist.album.state.list.selected(), Some(0)); assert_eq!(selection.artist.selected(), Some(0));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(0)); assert_eq!(selection.artist.album.selected(), Some(0));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.increment_selection(&COLLECTION, Delta::Line); selection.increment_selection(&COLLECTION, Delta::Line);
assert_eq!(selection.active, Category::Artist); assert_eq!(selection.active, Category::Artist);
assert_eq!(selection.artist.state.list.selected(), Some(1)); assert_eq!(selection.selected(), Some(1));
assert_eq!(selection.artist.album.state.list.selected(), Some(0)); assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(0)); assert_eq!(selection.artist.album.selected(), Some(0));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.increment_category(); selection.increment_category();
assert_eq!(selection.active, Category::Album); assert_eq!(selection.active, Category::Album);
assert_eq!(selection.artist.state.list.selected(), Some(1)); assert_eq!(selection.selected(), Some(0));
assert_eq!(selection.artist.album.state.list.selected(), Some(0)); assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(0)); assert_eq!(selection.artist.album.selected(), Some(0));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.increment_selection(&COLLECTION, Delta::Line); selection.increment_selection(&COLLECTION, Delta::Line);
assert_eq!(selection.active, Category::Album); assert_eq!(selection.active, Category::Album);
assert_eq!(selection.artist.state.list.selected(), Some(1)); assert_eq!(selection.selected(), Some(1));
assert_eq!(selection.artist.album.state.list.selected(), Some(1)); assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(0)); assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.increment_category(); selection.increment_category();
assert_eq!(selection.active, Category::Track); assert_eq!(selection.active, Category::Track);
assert_eq!(selection.artist.state.list.selected(), Some(1)); assert_eq!(selection.selected(), Some(0));
assert_eq!(selection.artist.album.state.list.selected(), Some(1)); assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(0)); assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.increment_selection(&COLLECTION, Delta::Line); selection.increment_selection(&COLLECTION, Delta::Line);
assert_eq!(selection.active, Category::Track); assert_eq!(selection.active, Category::Track);
assert_eq!(selection.artist.state.list.selected(), Some(1)); assert_eq!(selection.selected(), Some(1));
assert_eq!(selection.artist.album.state.list.selected(), Some(1)); assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(1)); assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(1));
selection.increment_category(); selection.increment_category();
assert_eq!(selection.active, Category::Track); assert_eq!(selection.active, Category::Track);
assert_eq!(selection.artist.state.list.selected(), Some(1)); assert_eq!(selection.selected(), Some(1));
assert_eq!(selection.artist.album.state.list.selected(), Some(1)); assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(1)); assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(1));
selection.decrement_selection(&COLLECTION, Delta::Line); selection.decrement_selection(&COLLECTION, Delta::Line);
assert_eq!(selection.active, Category::Track); assert_eq!(selection.active, Category::Track);
assert_eq!(selection.artist.state.list.selected(), Some(1)); assert_eq!(selection.selected(), Some(0));
assert_eq!(selection.artist.album.state.list.selected(), Some(1)); assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(0)); assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.increment_selection(&COLLECTION, Delta::Line); selection.increment_selection(&COLLECTION, Delta::Line);
selection.decrement_category(); selection.decrement_category();
assert_eq!(selection.active, Category::Album); assert_eq!(selection.active, Category::Album);
assert_eq!(selection.artist.state.list.selected(), Some(1)); assert_eq!(selection.selected(), Some(1));
assert_eq!(selection.artist.album.state.list.selected(), Some(1)); assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(1)); assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(1));
selection.decrement_selection(&COLLECTION, Delta::Line); selection.decrement_selection(&COLLECTION, Delta::Line);
assert_eq!(selection.active, Category::Album); assert_eq!(selection.active, Category::Album);
assert_eq!(selection.artist.state.list.selected(), Some(1)); assert_eq!(selection.selected(), Some(0));
assert_eq!(selection.artist.album.state.list.selected(), Some(0)); assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(0)); assert_eq!(selection.artist.album.selected(), Some(0));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.increment_selection(&COLLECTION, Delta::Line); selection.increment_selection(&COLLECTION, Delta::Line);
selection.decrement_category(); selection.decrement_category();
assert_eq!(selection.active, Category::Artist); assert_eq!(selection.active, Category::Artist);
assert_eq!(selection.artist.state.list.selected(), Some(1)); assert_eq!(selection.selected(), Some(1));
assert_eq!(selection.artist.album.state.list.selected(), Some(1)); assert_eq!(selection.artist.selected(), Some(1));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(0)); assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.decrement_selection(&COLLECTION, Delta::Line); selection.decrement_selection(&COLLECTION, Delta::Line);
assert_eq!(selection.active, Category::Artist); assert_eq!(selection.active, Category::Artist);
assert_eq!(selection.artist.state.list.selected(), Some(0)); assert_eq!(selection.selected(), Some(0));
assert_eq!(selection.artist.album.state.list.selected(), Some(0)); assert_eq!(selection.artist.selected(), Some(0));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(0)); assert_eq!(selection.artist.album.selected(), Some(0));
assert_eq!(selection.artist.album.track.selected(), Some(0));
selection.increment_category(); selection.increment_category();
selection.increment_selection(&COLLECTION, Delta::Line); selection.increment_selection(&COLLECTION, Delta::Line);
@ -314,8 +381,9 @@ mod tests {
selection.decrement_selection(&COLLECTION, Delta::Line); selection.decrement_selection(&COLLECTION, Delta::Line);
selection.decrement_category(); selection.decrement_category();
assert_eq!(selection.active, Category::Artist); assert_eq!(selection.active, Category::Artist);
assert_eq!(selection.artist.state.list.selected(), Some(0)); assert_eq!(selection.selected(), Some(0));
assert_eq!(selection.artist.album.state.list.selected(), Some(1)); assert_eq!(selection.artist.selected(), Some(0));
assert_eq!(selection.artist.album.track.state.list.selected(), Some(0)); assert_eq!(selection.artist.album.selected(), Some(1));
assert_eq!(selection.artist.album.track.selected(), Some(0));
} }
} }

View File

@ -118,35 +118,53 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn track_selection() { fn track_select() {
let tracks = &COLLECTION[0].albums[0].tracks;
assert!(tracks.len() > 1);
let mut sel = TrackSelection::initialise(tracks);
assert_eq!(sel.selected(), Some(0));
sel.select(tracks, None);
assert_eq!(sel.selected(), None);
sel.select(tracks, Some(tracks.len()));
assert_eq!(sel.selected(), Some(tracks.len() - 1));
sel.reset(tracks);
assert_eq!(sel.selected(), Some(0));
}
#[test]
fn track_delta_line() {
let tracks = &COLLECTION[0].albums[0].tracks; let tracks = &COLLECTION[0].albums[0].tracks;
assert!(tracks.len() > 1); assert!(tracks.len() > 1);
let mut empty = TrackSelection::initialise(&[]); let mut empty = TrackSelection::initialise(&[]);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
empty.increment(tracks, Delta::Line); empty.increment(tracks, Delta::Line);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
empty.decrement(tracks, Delta::Line); empty.decrement(tracks, Delta::Line);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
let mut sel = TrackSelection::initialise(tracks); let mut sel = TrackSelection::initialise(tracks);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
sel.decrement(tracks, Delta::Line); sel.decrement(tracks, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
sel.increment(tracks, Delta::Line); sel.increment(tracks, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(1)); assert_eq!(sel.selected(), Some(1));
sel.decrement(tracks, Delta::Line); sel.decrement(tracks, Delta::Line);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
for _ in 0..(tracks.len() + 5) { for _ in 0..(tracks.len() + 5) {
sel.increment(tracks, Delta::Line); sel.increment(tracks, Delta::Line);
} }
assert_eq!(sel.state.list.selected(), Some(tracks.len() - 1)); assert_eq!(sel.selected(), Some(tracks.len() - 1));
} }
#[test] #[test]
@ -155,27 +173,27 @@ mod tests {
assert!(tracks.len() > 1); assert!(tracks.len() > 1);
let empty = TrackSelection::initialise(&[]); let empty = TrackSelection::initialise(&[]);
assert_eq!(empty.state.list.selected(), None); assert_eq!(empty.selected(), None);
let mut sel = TrackSelection::initialise(tracks); let mut sel = TrackSelection::initialise(tracks);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
assert!(tracks.len() >= 4); assert!(tracks.len() >= 4);
sel.state.height = 3; sel.state.height = 3;
sel.decrement(tracks, Delta::Page); sel.decrement(tracks, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
sel.increment(tracks, Delta::Page); sel.increment(tracks, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(2)); assert_eq!(sel.selected(), Some(2));
sel.decrement(tracks, Delta::Page); sel.decrement(tracks, Delta::Page);
assert_eq!(sel.state.list.selected(), Some(0)); assert_eq!(sel.selected(), Some(0));
for _ in 0..(tracks.len() + 5) { for _ in 0..(tracks.len() + 5) {
sel.increment(tracks, Delta::Page); sel.increment(tracks, Delta::Page);
} }
assert_eq!(sel.state.list.selected(), Some(tracks.len() - 1)); assert_eq!(sel.selected(), Some(tracks.len() - 1));
} }
#[test] #[test]