Skip to content

Commit febf1b9

Browse files
authored
Merge pull request kstep#25 from tomprince/sticker-test
Add a basic test of sticker functionality.
2 parents dd8ca55 + 5c21ffc commit febf1b9

File tree

8 files changed

+64
-5
lines changed

8 files changed

+64
-5
lines changed

src/client.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use status::{ReplayGain, Status};
2222
use std::convert::From;
2323
use std::io::{BufRead, Lines, Read, Write};
2424
use std::net::{TcpStream, ToSocketAddrs};
25+
use sticker::Sticker;
2526
use version::Version;
2627

2728
// Client {{{
@@ -598,8 +599,10 @@ impl<S: Read + Write> Client<S> {
598599
// Sticker methods {{{
599600
/// Show sticker value for a given object, identified by type and uri
600601
pub fn sticker(&mut self, typ: &str, uri: &str, name: &str) -> Result<String> {
601-
self.run_command("sticker set", (typ, uri, name))
602-
.and_then(|_| self.read_field("sticker"))
602+
self.run_command("sticker get", (typ, uri, name))
603+
// TODO: This should parse to a `Sticker` type.
604+
.and_then(|_| self.read_field::<Sticker>("sticker"))
605+
.map(|s| s.value)
603606
}
604607

605608
/// Set sticker value for a given object, identified by type and uri

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ pub enum ProtoError {
370370
BadBanner,
371371
/// expected some field, but it was missing
372372
NoField(&'static str),
373+
/// expected sticker value, but didn't find it
374+
BadSticker,
373375
}
374376

375377
impl fmt::Display for ProtoError {
@@ -385,6 +387,7 @@ impl StdError for ProtoError {
385387
ProtoError::NotPair => "pair expected",
386388
ProtoError::BadBanner => "banner error",
387389
ProtoError::NoField(_) => "missing field",
390+
ProtoError::BadSticker => "sticker error",
388391
}
389392
}
390393
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub mod search;
5151
pub mod message;
5252
pub mod idle;
5353
pub mod mount;
54+
mod sticker;
5455

5556
mod proto;
5657
pub mod client;

src/proto.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,8 @@ pub trait Proto {
162162
}
163163
}
164164

165-
fn read_field<T, E>(&mut self, field: &'static str) -> Result<T>
166-
where T: FromStr<Err = E>,
167-
ParseError: From<E>
165+
fn read_field<T: FromStr>(&mut self, field: &'static str) -> Result<T>
166+
where ParseError: From<T::Err>
168167
{
169168
let (a, b) = self.read_pair()?;
170169
self.expect_ok()?;

src/sticker.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use error::ParseError;
2+
use std::str::FromStr;
3+
4+
pub struct Sticker {
5+
pub name: String,
6+
pub value: String,
7+
}
8+
9+
impl FromStr for Sticker {
10+
type Err = ParseError;
11+
fn from_str(s: &str) -> Result<Sticker, ParseError> {
12+
let mut parts = s.splitn(2, '=');
13+
match (parts.next(), parts.next()) {
14+
(Some(name), Some(value)) => {
15+
Ok(Sticker {
16+
name: name.to_owned(),
17+
value: value.to_owned(),
18+
})
19+
}
20+
_ => Err(ParseError::BadValue(s.to_owned())),
21+
}
22+
}
23+
}

tests/data/empty.flac

8.09 KB
Binary file not shown.

tests/helpers/daemon.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct MpdConfig {
1212
db_file: PathBuf,
1313
music_directory: PathBuf,
1414
playlist_directory: PathBuf,
15+
sticker_file: PathBuf,
1516
config_path: PathBuf,
1617
sock_path: PathBuf,
1718
}
@@ -25,6 +26,7 @@ impl MpdConfig {
2526
db_file: base.join("db"),
2627
music_directory: base.join("music"),
2728
playlist_directory: base.join("playlists"),
29+
sticker_file: base.join("sticker_file"),
2830
config_path: base.join("config"),
2931
sock_path: base.join("sock"),
3032
}
@@ -36,6 +38,7 @@ db_file "{db_file}"
3638
log_file "/dev/null"
3739
music_directory "{music_directory}"
3840
playlist_directory "{playlist_directory}"
41+
sticker_file "{sticker_file}"
3942
bind_to_address "{sock_path}"
4043
audio_output {{
4144
type "null"
@@ -45,6 +48,7 @@ audio_output {{
4548
db_file=self.db_file.display(),
4649
music_directory=self.music_directory.display(),
4750
playlist_directory=self.playlist_directory.display(),
51+
sticker_file=self.sticker_file.display(),
4852
sock_path=self.sock_path.display(),
4953
)
5054
}
@@ -83,11 +87,20 @@ fn sleep() {
8387
thread::sleep(ten_millis);
8488
}
8589

90+
static EMPTY_FLAC_BYTES: &'static [u8] = include_bytes!("../data/empty.flac");
91+
8692
impl Daemon {
8793
pub fn start() -> Daemon {
8894
let temp_dir = TempDir::new("mpd-test").unwrap();
8995
let config = MpdConfig::new(&temp_dir);
9096
config.generate();
97+
98+
// TODO: Factor out putting files in the music directory.
99+
File::create(config.music_directory.join("empty.flac"))
100+
.unwrap()
101+
.write_all(EMPTY_FLAC_BYTES)
102+
.unwrap();
103+
91104
let process = Command::new("mpd")
92105
.arg("--no-daemon")
93106
.arg(&config.config_path)

tests/stickers.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
extern crate mpd;
2+
3+
mod helpers;
4+
use helpers::connect;
5+
6+
#[test]
7+
/// Creating a sticker and then getting that sticker returns the value that was set.
8+
fn set_sticker() {
9+
let mut mpd = connect();
10+
11+
static VALUE: &'static str = "value";
12+
13+
mpd.set_sticker("song", "empty.flac", "test_sticker", VALUE).unwrap();
14+
15+
let sticker = mpd.sticker("song", "empty.flac", "test_sticker").unwrap();
16+
assert_eq!(sticker, VALUE);
17+
}

0 commit comments

Comments
 (0)