Clean up after #11, including unit tests.

This commit is contained in:
Aaron Weiss 2014-12-31 20:05:10 -05:00
parent 202748e7a9
commit a6146bc135
4 changed files with 87 additions and 32 deletions

View file

@ -114,8 +114,7 @@ impl<T: IrcReader, U: IrcWriter> Connection<T, U> {
/// Sends a Message over this connection. /// Sends a Message over this connection.
#[experimental] #[experimental]
#[cfg(feature = "encode")] #[cfg(feature = "encode")]
pub fn send<T: ToMessage>(&self, tomsg: T, encoding: &str) -> IoResult<()> { pub fn send<T: ToMessage>(&self, to_msg: T, encoding: &str) -> IoResult<()> {
let message = tomsg.to_message();
let encoding = match encoding_from_whatwg_label(encoding) { let encoding = match encoding_from_whatwg_label(encoding) {
Some(enc) => enc, Some(enc) => enc,
None => return Err(IoError { None => return Err(IoError {
@ -124,7 +123,8 @@ impl<T: IrcReader, U: IrcWriter> Connection<T, U> {
detail: Some(format!("Invalid decoder: {}", encoding)) detail: Some(format!("Invalid decoder: {}", encoding))
}) })
}; };
let data = match encoding.encode(message.into_string()[], EncoderTrap::Replace) { let msg = to_msg.to_message();
let data = match encoding.encode(msg.into_string()[], EncoderTrap::Replace) {
Ok(data) => data, Ok(data) => data,
Err(data) => return Err(IoError { Err(data) => return Err(IoError {
kind: IoErrorKind::InvalidInput, kind: IoErrorKind::InvalidInput,
@ -140,10 +140,9 @@ impl<T: IrcReader, U: IrcWriter> Connection<T, U> {
/// Sends a message over this connection. /// Sends a message over this connection.
#[experimental] #[experimental]
#[cfg(not(feature = "encode"))] #[cfg(not(feature = "encode"))]
pub fn send<T: ToMessage>(&self, tomsg: T) -> IoResult<()> { pub fn send<T: ToMessage>(&self, to_msg: T) -> IoResult<()> {
let message = tomsg.to_message();
let mut writer = self.writer.lock(); let mut writer = self.writer.lock();
try!(writer.write_str(message.into_string()[])); try!(writer.write_str(to_msg.to_message().into_string()[]));
writer.flush() writer.flush()
} }
@ -255,6 +254,16 @@ mod test {
assert_eq!(data[], "PRIVMSG test :Testing!\r\n"); assert_eq!(data[], "PRIVMSG test :Testing!\r\n");
} }
#[test]
#[cfg(not(feature = "encode"))]
fn send_str() {
let exp = "PRIVMSG test :Testing!\r\n";
let conn = Connection::new(NullReader, MemWriter::new());
assert!(conn.send(exp).is_ok());
let data = String::from_utf8(conn.writer().get_ref().to_vec()).unwrap();
assert_eq!(data[], exp);
}
#[test] #[test]
#[cfg(feature = "encode")] #[cfg(feature = "encode")]
fn send_utf8() { fn send_utf8() {
@ -266,6 +275,16 @@ mod test {
assert_eq!(data[], "PRIVMSG test :€ŠšŽžŒœŸ\r\n"); assert_eq!(data[], "PRIVMSG test :€ŠšŽžŒœŸ\r\n");
} }
#[test]
#[cfg(feature = "encode")]
fn send_utf8_str() {
let exp = "PRIVMSG test :€ŠšŽžŒœŸ\r\n";
let conn = Connection::new(NullReader, MemWriter::new());
assert!(conn.send(exp, "UTF-8").is_ok());
let data = UTF_8.decode(conn.writer().get_ref(), DecoderTrap::Strict).unwrap();
assert_eq!(data[], exp);
}
#[test] #[test]
#[cfg(feature = "encode")] #[cfg(feature = "encode")]
fn send_iso885915() { fn send_iso885915() {
@ -277,6 +296,16 @@ mod test {
assert_eq!(data[], "PRIVMSG test :€ŠšŽžŒœŸ\r\n"); assert_eq!(data[], "PRIVMSG test :€ŠšŽžŒœŸ\r\n");
} }
#[test]
#[cfg(feature = "encode")]
fn send_iso885915_str() {
let exp = "PRIVMSG test :€ŠšŽžŒœŸ\r\n";
let conn = Connection::new(NullReader, MemWriter::new());
assert!(conn.send(exp, "l9").is_ok());
let data = ISO_8859_15.decode(conn.writer().get_ref(), DecoderTrap::Strict).unwrap();
assert_eq!(data[], exp);
}
#[test] #[test]
#[cfg(not(feature = "encode"))] #[cfg(not(feature = "encode"))]
fn recv() { fn recv() {

View file

@ -151,7 +151,6 @@ pub enum Command<'a> {
} }
impl<'a> ToMessage for Command<'a> { impl<'a> ToMessage for Command<'a> {
/// Converts a Command into a Message. /// Converts a Command into a Message.
#[stable] #[stable]
fn to_message(&self) -> Message { fn to_message(&self) -> Message {
@ -163,7 +162,8 @@ impl<'a> ToMessage for Command<'a> {
Command::MODE(t, m, Some(p)) => Message::new(None, "MODE", Some(vec![t, m, p]), None), Command::MODE(t, m, Some(p)) => Message::new(None, "MODE", Some(vec![t, m, p]), None),
Command::MODE(t, m, None) => Message::new(None, "MODE", Some(vec![t, m]), None), Command::MODE(t, m, None) => Message::new(None, "MODE", Some(vec![t, m]), None),
Command::SERVICE(n, r, d, t, re, i) => Message::new(None, "SERVICE", Command::SERVICE(n, r, d, t, re, i) => Message::new(None, "SERVICE",
Some(vec![n, r, d, t, re]), Some(i)), Some(vec![n, r, d, t, re]),
Some(i)),
Command::QUIT(Some(m)) => Message::new(None, "QUIT", None, Some(m)), Command::QUIT(Some(m)) => Message::new(None, "QUIT", None, Some(m)),
Command::QUIT(None) => Message::new(None, "QUIT", None, None), Command::QUIT(None) => Message::new(None, "QUIT", None, None),
Command::SQUIT(s, c) => Message::new(None, "SQUIT", Some(vec![s]), Some(c)), Command::SQUIT(s, c) => Message::new(None, "SQUIT", Some(vec![s]), Some(c)),
@ -187,7 +187,7 @@ impl<'a> ToMessage for Command<'a> {
Command::MOTD(Some(t)) => Message::new(None, "MOTD", None, Some(t)), Command::MOTD(Some(t)) => Message::new(None, "MOTD", None, Some(t)),
Command::MOTD(None) => Message::new(None, "MOTD", None, None), Command::MOTD(None) => Message::new(None, "MOTD", None, None),
Command::LUSERS(Some(m), Some(t)) => Message::new(None, "LUSERS", Some(vec![m]), Command::LUSERS(Some(m), Some(t)) => Message::new(None, "LUSERS", Some(vec![m]),
Some(t)), Some(t)),
Command::LUSERS(Some(m), None) => Message::new(None, "LUSERS", Some(vec![m]), None), Command::LUSERS(Some(m), None) => Message::new(None, "LUSERS", Some(vec![m]), None),
Command::LUSERS(None, _) => Message::new(None, "LUSERS", None, None), Command::LUSERS(None, _) => Message::new(None, "LUSERS", None, None),
Command::VERSION(Some(t)) => Message::new(None, "VERSION", None, Some(t)), Command::VERSION(Some(t)) => Message::new(None, "VERSION", None, Some(t)),
@ -201,7 +201,7 @@ impl<'a> ToMessage for Command<'a> {
Command::TIME(Some(t)) => Message::new(None, "TIME", None, Some(t)), Command::TIME(Some(t)) => Message::new(None, "TIME", None, Some(t)),
Command::TIME(None) => Message::new(None, "TIME", None, None), Command::TIME(None) => Message::new(None, "TIME", None, None),
Command::CONNECT(t, p, Some(r)) => Message::new(None, "CONNECT", Some(vec![t, p]), Command::CONNECT(t, p, Some(r)) => Message::new(None, "CONNECT", Some(vec![t, p]),
Some(r)), Some(r)),
Command::CONNECT(t, p, None) => Message::new(None, "CONNECT", Some(vec![t, p]), None), Command::CONNECT(t, p, None) => Message::new(None, "CONNECT", Some(vec![t, p]), None),
Command::TRACE(Some(t)) => Message::new(None, "TRACE", None, Some(t)), Command::TRACE(Some(t)) => Message::new(None, "TRACE", None, Some(t)),
Command::TRACE(None) => Message::new(None, "TRACE", None, None), Command::TRACE(None) => Message::new(None, "TRACE", None, None),
@ -210,20 +210,20 @@ impl<'a> ToMessage for Command<'a> {
Command::INFO(Some(t)) => Message::new(None, "INFO", None, Some(t)), Command::INFO(Some(t)) => Message::new(None, "INFO", None, Some(t)),
Command::INFO(None) => Message::new(None, "INFO", None, None), Command::INFO(None) => Message::new(None, "INFO", None, None),
Command::SERVLIST(Some(m), Some(t)) => Message::new(None, "SERVLIST", Some(vec![m]), Command::SERVLIST(Some(m), Some(t)) => Message::new(None, "SERVLIST", Some(vec![m]),
Some(t)), Some(t)),
Command::SERVLIST(Some(m), None) => Message::new(None, "SERVLIST", Some(vec![m]), None), Command::SERVLIST(Some(m), None) => Message::new(None, "SERVLIST", Some(vec![m]), None),
Command::SERVLIST(None, _) => Message::new(None, "SERVLIST", None, None), Command::SERVLIST(None, _) => Message::new(None, "SERVLIST", None, None),
Command::SQUERY(s, t) => Message::new(None, "SQUERY", Some(vec![s, t]), None), Command::SQUERY(s, t) => Message::new(None, "SQUERY", Some(vec![s, t]), None),
Command::WHO(Some(s), Some(true)) => Message::new(None, "WHO", Some(vec![s, "o"]), Command::WHO(Some(s), Some(true)) => Message::new(None, "WHO", Some(vec![s, "o"]),
None), None),
Command::WHO(Some(s), _) => Message::new(None, "WHO", Some(vec![s]), None), Command::WHO(Some(s), _) => Message::new(None, "WHO", Some(vec![s]), None),
Command::WHO(None, _) => Message::new(None, "WHO", None, None), Command::WHO(None, _) => Message::new(None, "WHO", None, None),
Command::WHOIS(Some(t), m) => Message::new(None, "WHOIS", Some(vec![t, m]), None), Command::WHOIS(Some(t), m) => Message::new(None, "WHOIS", Some(vec![t, m]), None),
Command::WHOIS(None, m) => Message::new(None, "WHOIS", Some(vec![m]), None), Command::WHOIS(None, m) => Message::new(None, "WHOIS", Some(vec![m]), None),
Command::WHOWAS(n, Some(c), Some(t)) => Message::new(None, "WHOWAS", Some(vec![n, c]), Command::WHOWAS(n, Some(c), Some(t)) => Message::new(None, "WHOWAS", Some(vec![n, c]),
Some(t)), Some(t)),
Command::WHOWAS(n, Some(c), None) => Message::new(None, "WHOWAS", Some(vec![n, c]), Command::WHOWAS(n, Some(c), None) => Message::new(None, "WHOWAS", Some(vec![n, c]),
None), None),
Command::WHOWAS(n, None, _) => Message::new(None, "WHOWAS", Some(vec![n]), None), Command::WHOWAS(n, None, _) => Message::new(None, "WHOWAS", Some(vec![n]), None),
Command::KILL(n, c) => Message::new(None, "KILL", Some(vec![n]), Some(c)), Command::KILL(n, c) => Message::new(None, "KILL", Some(vec![n]), Some(c)),
Command::PING(s, Some(t)) => Message::new(None, "PING", Some(vec![s]), Some(t)), Command::PING(s, Some(t)) => Message::new(None, "PING", Some(vec![s]), Some(t)),
@ -237,9 +237,9 @@ impl<'a> ToMessage for Command<'a> {
Command::DIE => Message::new(None, "DIE", None, None), Command::DIE => Message::new(None, "DIE", None, None),
Command::RESTART => Message::new(None, "RESTART", None, None), Command::RESTART => Message::new(None, "RESTART", None, None),
Command::SUMMON(u, Some(t), Some(c)) => Message::new(None, "SUMMON", Some(vec![u, t]), Command::SUMMON(u, Some(t), Some(c)) => Message::new(None, "SUMMON", Some(vec![u, t]),
Some(c)), Some(c)),
Command::SUMMON(u, Some(t), None) => Message::new(None, "SUMMON", Some(vec![u, t]), Command::SUMMON(u, Some(t), None) => Message::new(None, "SUMMON", Some(vec![u, t]),
None), None),
Command::SUMMON(u, None, _) => Message::new(None, "SUMMON", Some(vec![u]), None), Command::SUMMON(u, None, _) => Message::new(None, "SUMMON", Some(vec![u]), None),
Command::USERS(Some(t)) => Message::new(None, "USERS", None, Some(t)), Command::USERS(Some(t)) => Message::new(None, "USERS", None, Some(t)),
Command::USERS(None) => Message::new(None, "USERS", None, None), Command::USERS(None) => Message::new(None, "USERS", None, None),
@ -248,7 +248,7 @@ impl<'a> ToMessage for Command<'a> {
Command::ISON(ref u) => Message::new(None, "ISON", Some(u.clone()), None), Command::ISON(ref u) => Message::new(None, "ISON", Some(u.clone()), None),
Command::SAJOIN(n, c) => Message::new(None, "SAJOIN", Some(vec![n, c]), None), Command::SAJOIN(n, c) => Message::new(None, "SAJOIN", Some(vec![n, c]), None),
Command::SAMODE(t, m, Some(p)) => Message::new(None, "SAMODE", Some(vec![t, m, p]), Command::SAMODE(t, m, Some(p)) => Message::new(None, "SAMODE", Some(vec![t, m, p]),
None), None),
Command::SAMODE(t, m, None) => Message::new(None, "SAMODE", Some(vec![t, m]), None), Command::SAMODE(t, m, None) => Message::new(None, "SAMODE", Some(vec![t, m]), None),
Command::SANICK(o, n) => Message::new(None, "SANICK", Some(vec![o, n]), None), Command::SANICK(o, n) => Message::new(None, "SANICK", Some(vec![o, n]), None),
Command::SAPART(c, r) => Message::new(None, "SAPART", Some(vec![c]), Some(r)), Command::SAPART(c, r) => Message::new(None, "SAPART", Some(vec![c]), Some(r)),
@ -262,7 +262,6 @@ impl<'a> ToMessage for Command<'a> {
Command::CAP(s, p) => Message::new(None, "CAP", Some(vec![s.to_str()]), p), Command::CAP(s, p) => Message::new(None, "CAP", Some(vec![s.to_str()]), p),
} }
} }
} }
impl<'a> Command<'a> { impl<'a> Command<'a> {

View file

@ -3,14 +3,6 @@
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::str::FromStr; use std::str::FromStr;
/// Represents something that can be converted to a message.
pub trait ToMessage {
/// Convert to message.
fn to_message(&self) -> Message;
}
/// IRC Message data. /// IRC Message data.
#[experimental] #[experimental]
#[deriving(Clone, PartialEq, Show)] #[deriving(Clone, PartialEq, Show)]
@ -63,11 +55,9 @@ impl Message {
} }
impl ToMessage for Message { impl ToMessage for Message {
fn to_message(&self) -> Message { fn to_message(&self) -> Message {
self.clone() self.clone()
} }
} }
impl FromStr for Message { impl FromStr for Message {
@ -101,9 +91,21 @@ impl FromStr for Message {
} }
} }
/// A trait representing the ability to be converted into a Message.
pub trait ToMessage {
/// Converts this to a Message.
fn to_message(&self) -> Message;
}
impl<'a> ToMessage for &'a str {
fn to_message(&self) -> Message {
self.parse().unwrap()
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::Message; use super::{Message, ToMessage};
#[test] #[test]
fn new() { fn new() {
@ -151,4 +153,28 @@ mod test {
}; };
assert_eq!(":test!test@test PRIVMSG test :Still testing!\r\n".parse(), Some(message)); assert_eq!(":test!test@test PRIVMSG test :Still testing!\r\n".parse(), Some(message));
} }
#[test]
fn to_message() {
let message = Message {
prefix: None,
command: format!("PRIVMSG"),
args: vec![format!("test")],
suffix: Some(format!("Testing!")),
};
assert_eq!("PRIVMSG test :Testing!\r\n".to_message(), message);
let message = Message {
prefix: Some(format!("test!test@test")),
command: format!("PRIVMSG"),
args: vec![format!("test")],
suffix: Some(format!("Still testing!")),
};
assert_eq!(":test!test@test PRIVMSG test :Still testing!\r\n".to_message(), message);
}
#[test]
#[should_fail]
fn to_message_invalid_format() {
":invalid :message".to_message();
}
} }

View file

@ -319,14 +319,15 @@ impl FromStr for Response {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::Response; use super::Response;
use data::message::ToMessage;
#[test] #[test]
fn from_message() { fn from_message() {
assert_eq!(Response::from_message( assert_eq!(Response::from_message(
&":irc.test.net 353 test = #test :test\r\n".parse().unwrap() &":irc.test.net 353 test = #test :test\r\n".to_message()
).unwrap(), Response::RPL_NAMREPLY); ).unwrap(), Response::RPL_NAMREPLY);
assert_eq!(Response::from_message( assert_eq!(Response::from_message(
&":irc.test.net 433 <nick> :Nickname is already in use\r\n".parse().unwrap() &":irc.test.net 433 <nick> :Nickname is already in use\r\n".to_message()
).unwrap(), Response::ERR_NICKNAMEINUSE); ).unwrap(), Response::ERR_NICKNAMEINUSE);
} }