Create unit tests for query related methods

This commit is contained in:
Anas Elgarhy 2023-02-12 05:23:36 +02:00
parent a74efe45bb
commit 532b9d5490
No known key found for this signature in database
GPG key ID: 0501802A1D496528
4 changed files with 140 additions and 0 deletions

View file

@ -38,3 +38,83 @@ impl CmusQueryResponse {
PlayerSettings::from_str(&self.player_settings_row)
}
}
#[cfg(test)]
mod tests {
use test_context::{test_context, TestContext};
use crate::cmus::player_settings::{AAAMode, Shuffle};
use crate::cmus::TrackStatus;
use super::*;
#[test]
fn test_parse_query_from_str() {
let row = include_str!("../../tests/samples/row/cmus-remote-output-row.txt");
let query = CmusQueryResponse::from_str(row);
assert!(query.is_ok());
let query = query.unwrap();
assert_eq!(query.track_row, include_str!("../../tests/samples/row/cmus-remote-output-track-row.txt"));
assert_eq!(query.player_settings_row, include_str!("../../tests/samples/row/cmus-remote-output-player-row.txt"));
}
struct Context {
query: CmusQueryResponse,
}
impl TestContext for Context {
fn setup() -> Self {
let row = include_str!("../../tests/samples/row/cmus-remote-output-row.txt");
let query = CmusQueryResponse::from_str(row).unwrap();
Self {
query,
}
}
}
#[test_context(Context)]
#[test]
fn test_actually_parse_the_track_info(ctx: &Context) {
let track = ctx.query.track();
assert!(track.is_ok());
let track = track.unwrap();
assert_eq!(track.path, "/mnt/Data/Music/FLAC/Taylor Swift/Taylor Swift - Speak Now/12 - Haunted.mp3");
assert_eq!(track.status, TrackStatus::Playing);
assert_eq!(track.position, 34);
assert_eq!(track.duration, 242);
let metadata = track.metadata;
assert_eq!(metadata.get("artist"), Some("Taylor Swift"));
assert_eq!(metadata.get("album"), Some("Speak Now"));
assert_eq!(metadata.get("title"), Some("Haunted"));
assert_eq!(metadata.get("date"), Some("2010"));
assert_eq!(metadata.get("genre"), Some("Pop"));
assert_eq!(metadata.get("discnumber"), Some("1"));
assert_eq!(metadata.get("tracknumber"), Some("12"));
assert_eq!(metadata.get("albumartist"), Some("Taylor Swift"));
assert_eq!(metadata.get("replaygain_track_gain"), Some("-11.3 dB"));
assert_eq!(metadata.get("composer"), Some("Taylor Swift"));
assert_eq!(metadata.get("label"), Some("Big Machine Records, LLC"));
assert_eq!(metadata.get("publisher"), Some("Big Machine Records, LLC"));
assert_eq!(metadata.get("bpm"), Some("162"));
assert_eq!(metadata.get("comment"), None);
}
#[test_context(Context)]
#[test]
fn test_actually_parse_the_player_settings(ctx: &Context) {
let player_settings = ctx.query.player_settings();
assert!(player_settings.is_ok());
let player_settings = player_settings.unwrap();
assert_eq!(player_settings.aaa_mode, AAAMode::All);
assert_eq!(player_settings.repeat, true);
assert_eq!(player_settings.repeat_current, false);
assert_eq!(player_settings.shuffle, Shuffle::Off);
assert_eq!(player_settings.volume.left, 17);
assert_eq!(player_settings.volume.right, 17);
}
}

View file

@ -0,0 +1,13 @@
set aaa_mode all
set continue true
set play_library false
set play_sorted true
set replaygain disabled
set replaygain_limit true
set replaygain_preamp 0.000000
set repeat true
set repeat_current false
set shuffle off
set softvol false
set vol_left 17
set vol_right 17

View file

@ -0,0 +1,30 @@
status playing
file /mnt/Data/Music/FLAC/Taylor Swift/Taylor Swift - Speak Now/12 - Haunted.mp3
duration 242
position 34
tag artist Taylor Swift
tag album Speak Now
tag title Haunted
tag date 2010
tag genre Pop
tag discnumber 1
tag tracknumber 12
tag albumartist Taylor Swift
tag replaygain_track_gain -11.3 dB
tag composer Taylor Swift
tag label Big Machine Records, LLC
tag publisher Big Machine Records, LLC
tag bpm 162
set aaa_mode all
set continue true
set play_library false
set play_sorted true
set replaygain disabled
set replaygain_limit true
set replaygain_preamp 0.000000
set repeat true
set repeat_current false
set shuffle off
set softvol false
set vol_left 17
set vol_right 17

View file

@ -0,0 +1,17 @@
status playing
file /mnt/Data/Music/FLAC/Taylor Swift/Taylor Swift - Speak Now/12 - Haunted.mp3
duration 242
position 34
tag artist Taylor Swift
tag album Speak Now
tag title Haunted
tag date 2010
tag genre Pop
tag discnumber 1
tag tracknumber 12
tag albumartist Taylor Swift
tag replaygain_track_gain -11.3 dB
tag composer Taylor Swift
tag label Big Machine Records, LLC
tag publisher Big Machine Records, LLC
tag bpm 162