diff options
Diffstat (limited to 'vendored/mpd/tests')
| -rw-r--r-- | vendored/mpd/tests/data/empty.flac | bin | 0 -> 8282 bytes | |||
| -rw-r--r-- | vendored/mpd/tests/helpers/daemon.rs | 140 | ||||
| -rw-r--r-- | vendored/mpd/tests/helpers/mod.rs | 36 | ||||
| -rw-r--r-- | vendored/mpd/tests/idle.rs | 20 | ||||
| -rw-r--r-- | vendored/mpd/tests/options.rs | 73 | ||||
| -rw-r--r-- | vendored/mpd/tests/outputs.rs | 25 | ||||
| -rw-r--r-- | vendored/mpd/tests/playback.rs | 10 | ||||
| -rw-r--r-- | vendored/mpd/tests/playlist.rs | 15 | ||||
| -rw-r--r-- | vendored/mpd/tests/reflect.rs | 28 | ||||
| -rw-r--r-- | vendored/mpd/tests/search.rs | 15 | ||||
| -rw-r--r-- | vendored/mpd/tests/song.rs | 28 | ||||
| -rw-r--r-- | vendored/mpd/tests/stickers.rs | 17 |
12 files changed, 407 insertions, 0 deletions
diff --git a/vendored/mpd/tests/data/empty.flac b/vendored/mpd/tests/data/empty.flac Binary files differnew file mode 100644 index 0000000..4407f5b --- /dev/null +++ b/vendored/mpd/tests/data/empty.flac diff --git a/vendored/mpd/tests/helpers/daemon.rs b/vendored/mpd/tests/helpers/daemon.rs new file mode 100644 index 0000000..a8e8b6c --- /dev/null +++ b/vendored/mpd/tests/helpers/daemon.rs @@ -0,0 +1,140 @@ +extern crate tempdir; + +use self::tempdir::TempDir; +use super::mpd; +use std::fs::{File, create_dir}; +use std::io::{Write, Read}; +use std::os::unix::net::UnixStream; +use std::path::{Path, PathBuf}; +use std::process::{Command, Child, Stdio}; + +struct MpdConfig { + db_file: PathBuf, + music_directory: PathBuf, + playlist_directory: PathBuf, + sticker_file: PathBuf, + config_path: PathBuf, + sock_path: PathBuf, +} + +impl MpdConfig { + pub fn new<P>(base: P) -> MpdConfig + where P: AsRef<Path> + { + let base = base.as_ref(); + MpdConfig { + db_file: base.join("db"), + music_directory: base.join("music"), + playlist_directory: base.join("playlists"), + sticker_file: base.join("sticker_file"), + config_path: base.join("config"), + sock_path: base.join("sock"), + } + } + + fn config_text(&self) -> String { + format!(r#" +db_file "{db_file}" +log_file "/dev/null" +music_directory "{music_directory}" +playlist_directory "{playlist_directory}" +sticker_file "{sticker_file}" +bind_to_address "{sock_path}" +audio_output {{ + type "null" + name "null" +}} +"#, + db_file=self.db_file.display(), + music_directory=self.music_directory.display(), + playlist_directory=self.playlist_directory.display(), + sticker_file=self.sticker_file.display(), + sock_path=self.sock_path.display(), + ) + } + + fn generate(&self) { + create_dir(&self.music_directory).expect("Could not create music directory."); + create_dir(&self.playlist_directory).expect("Could not create playlist directory."); + let mut file = File::create(&self.config_path).expect("Could not create config file."); + file.write_all(self.config_text().as_bytes()).expect("Could not write config file."); + } +} + +pub struct Daemon { + // Saved here so it gets dropped when this does. + _temp_dir: TempDir, + config: MpdConfig, + process: Child, +} + +impl Drop for Daemon { + fn drop(&mut self) { + self.process.kill().expect("Could not kill mpd daemon."); + self.process.wait().expect("Could not wait for mpd daemon to shutdown."); + if let Some(ref mut stderr) = self.process.stderr { + let mut output = String::new(); + stderr.read_to_string(&mut output).expect("Could not collect output from mpd."); + println!{"Output from mpd:"} + println!{"{}", output}; + } + } +} + +fn sleep() { + use std::{thread, time}; + let ten_millis = time::Duration::from_millis(10); + thread::sleep(ten_millis); +} + +static EMPTY_FLAC_BYTES: &'static [u8] = include_bytes!("../data/empty.flac"); + +impl Daemon { + pub fn start() -> Daemon { + let temp_dir = TempDir::new("mpd-test").unwrap(); + let config = MpdConfig::new(&temp_dir); + config.generate(); + + // TODO: Factor out putting files in the music directory. + File::create(config.music_directory.join("empty.flac")).unwrap().write_all(EMPTY_FLAC_BYTES).unwrap(); + + let process = Command::new("mpd") + .arg("--no-daemon") + .arg(&config.config_path) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + .expect("Could not create mpd daemon."); + + let daemon = Daemon { + _temp_dir: temp_dir, + config: config, + process: process, + }; + + // Wait until we can connect to the daemon + let mut client; + loop { + if let Ok(c) = daemon.maybe_connect() { + client = c; + break; + } + sleep() + } + while let Some(_) = client.status().expect("Couldn't get status.").updating_db { + sleep() + } + + daemon + } + + fn maybe_connect(&self) -> Result<mpd::Client<UnixStream>, mpd::error::Error> { + let stream = UnixStream::connect(&self.config.sock_path)?; + mpd::Client::new(stream) + } + + pub fn connect(&self) -> mpd::Client<UnixStream> { + self.maybe_connect().expect("Could not connect to daemon.") + } +} diff --git a/vendored/mpd/tests/helpers/mod.rs b/vendored/mpd/tests/helpers/mod.rs new file mode 100644 index 0000000..e331433 --- /dev/null +++ b/vendored/mpd/tests/helpers/mod.rs @@ -0,0 +1,36 @@ +extern crate mpd; + +mod daemon; + +pub use self::daemon::Daemon; +use std::os::unix::net::UnixStream; + +pub struct DaemonClient { + _daemon: Daemon, + client: mpd::Client<UnixStream>, +} + +use std::ops::{Deref, DerefMut}; + +impl Deref for DaemonClient { + type Target = mpd::Client<UnixStream>; + fn deref(&self) -> &Self::Target { + &self.client + } +} + +impl DerefMut for DaemonClient { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.client + } +} + +#[allow(dead_code)] +pub fn connect() -> DaemonClient { + let daemon = Daemon::start(); + let client = daemon.connect(); + DaemonClient { + _daemon: daemon, + client: client, + } +} diff --git a/vendored/mpd/tests/idle.rs b/vendored/mpd/tests/idle.rs new file mode 100644 index 0000000..d510aa2 --- /dev/null +++ b/vendored/mpd/tests/idle.rs @@ -0,0 +1,20 @@ +extern crate mpd; + +mod helpers; +use helpers::Daemon; + +use mpd::Idle; + +#[test] +fn idle() { + let daemon = Daemon::start(); + let mut mpd = daemon.connect(); + let idle = mpd.idle(&[]).unwrap(); + + let mut mpd1 = daemon.connect(); + mpd1.consume(true).unwrap(); + mpd1.consume(false).unwrap(); + + let sys = idle.get().unwrap(); + assert_eq!(&*sys, &[mpd::Subsystem::Options]); +} diff --git a/vendored/mpd/tests/options.rs b/vendored/mpd/tests/options.rs new file mode 100644 index 0000000..1bfa861 --- /dev/null +++ b/vendored/mpd/tests/options.rs @@ -0,0 +1,73 @@ +extern crate mpd; + +mod helpers; +use helpers::connect; +use std::time::Duration; + +#[test] +fn status() { + let mut mpd = connect(); + let status = mpd.status().unwrap(); + println!("{:?}", status); +} + +#[test] +fn stats() { + let mut mpd = connect(); + let stats = mpd.stats().unwrap(); + println!("{:?}", stats); +} + +macro_rules! test_options_impl { + ($name:ident, $val1:expr, $tval1:expr, $val2:expr, $tval2:expr) => { + #[test] + fn $name() { + let mut mpd = connect(); + mpd.$name($val1).unwrap(); + assert_eq!(mpd.status().unwrap().$name, $tval1); + mpd.$name($val2).unwrap(); + assert_eq!(mpd.status().unwrap().$name, $tval2); + } + } +} + +macro_rules! test_option { + ($name:ident, $val1:expr, $val2:expr) => { + test_options_impl!($name, $val1, $val1, $val2, $val2); + }; + ($name:ident, $val1:expr => $tval1:expr, $val2:expr => $tval2:expr) => { + test_options_impl!($name, $val1, $tval1, $val2, $tval2); + }; +} + +test_option!(consume, true, false); +test_option!(single, true, false); +test_option!(random, true, false); +test_option!(repeat, true, false); +// test_option!(mixrampdb, 1.0f32, 0.0f32); +// test_option!(mixrampdelay, 1 => Some(Duration::from_secs(1)), 0 => None); + +#[test] +fn volume() { + let mut mpd = connect(); + if mpd.status().unwrap().volume >= 0 { + mpd.volume(100).unwrap(); + assert_eq!(mpd.status().unwrap().volume, 100); + mpd.volume(0).unwrap(); + assert_eq!(mpd.status().unwrap().volume, 0); + } +} + +#[test] +fn crossfade() { + let mut mpd = connect(); + mpd.crossfade(1000).unwrap(); + assert_eq!(mpd.status().unwrap().crossfade, Some(Duration::from_secs(1000))); + mpd.crossfade(0).unwrap(); + assert_eq!(mpd.status().unwrap().crossfade, + if mpd.version >= mpd::Version(0, 19, 0) { + None + } else { + Some(Duration::from_secs(0)) + }); +} diff --git a/vendored/mpd/tests/outputs.rs b/vendored/mpd/tests/outputs.rs new file mode 100644 index 0000000..078aa94 --- /dev/null +++ b/vendored/mpd/tests/outputs.rs @@ -0,0 +1,25 @@ +extern crate mpd; +extern crate time; + +mod helpers; +use helpers::connect; + +#[test] +fn outputs() { + let mut mpd = connect(); + println!("{:?}", mpd.outputs()); +} + +#[test] +fn out_toggle() { + let mut mpd = connect(); + + mpd.out_disable(0).unwrap(); + mpd.out_enable(0).unwrap(); + + if mpd.version >= mpd::Version(0, 17, 0) { + mpd.out_toggle(0).unwrap(); + } + + mpd.output(0, true).unwrap(); +} diff --git a/vendored/mpd/tests/playback.rs b/vendored/mpd/tests/playback.rs new file mode 100644 index 0000000..4a51eb8 --- /dev/null +++ b/vendored/mpd/tests/playback.rs @@ -0,0 +1,10 @@ +extern crate mpd; +extern crate time; + +mod helpers; + +#[test] +fn playback() { + let mut mpd = helpers::connect(); + mpd.play().unwrap(); +} diff --git a/vendored/mpd/tests/playlist.rs b/vendored/mpd/tests/playlist.rs new file mode 100644 index 0000000..c8442c2 --- /dev/null +++ b/vendored/mpd/tests/playlist.rs @@ -0,0 +1,15 @@ +extern crate mpd; + +mod helpers; +use helpers::connect; + +#[test] +fn playlists() { + let mut mpd = connect(); + let pls = mpd.playlists().unwrap(); + println!("{:?}", pls); + + for pl in &pls { + println!("{}: {:?}", pl.name, mpd.playlist(&pl.name).unwrap()); + } +} diff --git a/vendored/mpd/tests/reflect.rs b/vendored/mpd/tests/reflect.rs new file mode 100644 index 0000000..f0d0858 --- /dev/null +++ b/vendored/mpd/tests/reflect.rs @@ -0,0 +1,28 @@ +extern crate mpd; + +mod helpers; +use helpers::connect; + +#[test] +fn commands() { + let mut mpd = connect(); + println!("{:?}", mpd.commands().unwrap()); +} + +#[test] +fn urlhandlers() { + let mut mpd = connect(); + println!("{:?}", mpd.urlhandlers().unwrap()); +} + +#[test] +fn decoders() { + let mut mpd = connect(); + println!("{:?}", mpd.decoders().unwrap()); +} + +#[test] +fn tagtypes() { + let mut mpd = connect(); + println!("{:?}", mpd.tagtypes().unwrap()); +} diff --git a/vendored/mpd/tests/search.rs b/vendored/mpd/tests/search.rs new file mode 100644 index 0000000..56c544d --- /dev/null +++ b/vendored/mpd/tests/search.rs @@ -0,0 +1,15 @@ +extern crate mpd; + +mod helpers; +use helpers::connect; +use mpd::Query; + +#[test] +fn search() { + let mut mpd = connect(); + let mut query = Query::new(); + let query = query.and(mpd::Term::Any, "Soul"); + let songs = mpd.find(query, None); + println!("{:?}", songs); + assert!(songs.is_ok()); +} diff --git a/vendored/mpd/tests/song.rs b/vendored/mpd/tests/song.rs new file mode 100644 index 0000000..3510f99 --- /dev/null +++ b/vendored/mpd/tests/song.rs @@ -0,0 +1,28 @@ +extern crate mpd; + +mod helpers; +use helpers::connect; + +#[test] +fn currentsong() { + let mut mpd = connect(); + let song = mpd.currentsong().unwrap(); + println!("{:?}", song); +} + +#[test] +fn queue() { + let mut mpd = connect(); + let queue = mpd.queue().unwrap(); + println!("{:?}", queue); + + let songs = mpd.songs(..).unwrap(); + assert_eq!(songs, queue); +} + +#[test] +fn rescan_update() { + let mut mpd = connect(); + println!("update: {:?}", mpd.update()); + println!("rescan: {:?}", mpd.rescan()); +} diff --git a/vendored/mpd/tests/stickers.rs b/vendored/mpd/tests/stickers.rs new file mode 100644 index 0000000..61aead6 --- /dev/null +++ b/vendored/mpd/tests/stickers.rs @@ -0,0 +1,17 @@ +extern crate mpd; + +mod helpers; +use helpers::connect; + +#[test] +/// Creating a sticker and then getting that sticker returns the value that was set. +fn set_sticker() { + let mut mpd = connect(); + + static VALUE: &'static str = "value"; + + mpd.set_sticker("song", "empty.flac", "test_sticker", VALUE).unwrap(); + + let sticker = mpd.sticker("song", "empty.flac", "test_sticker").unwrap(); + assert_eq!(sticker, VALUE); +} |