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.
#[experimental]
#[cfg(feature = "encode")]
pub fn send<T: ToMessage>(&self, tomsg: T, encoding: &str) -> IoResult<()> {
let message = tomsg.to_message();
pub fn send<T: ToMessage>(&self, to_msg: T, encoding: &str) -> IoResult<()> {
let encoding = match encoding_from_whatwg_label(encoding) {
Some(enc) => enc,
None => return Err(IoError {
@ -124,7 +123,8 @@ impl<T: IrcReader, U: IrcWriter> Connection<T, U> {
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,
Err(data) => return Err(IoError {
kind: IoErrorKind::InvalidInput,
@ -140,10 +140,9 @@ impl<T: IrcReader, U: IrcWriter> Connection<T, U> {
/// Sends a message over this connection.
#[experimental]
#[cfg(not(feature = "encode"))]
pub fn send<T: ToMessage>(&self, tomsg: T) -> IoResult<()> {
let message = tomsg.to_message();
pub fn send<T: ToMessage>(&self, to_msg: T) -> IoResult<()> {
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()
}
@ -254,6 +253,16 @@ mod test {
let data = String::from_utf8(conn.writer().get_ref().to_vec()).unwrap();
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]
#[cfg(feature = "encode")]
@ -266,6 +275,16 @@ mod test {
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]
#[cfg(feature = "encode")]
fn send_iso885915() {
@ -277,6 +296,16 @@ mod test {
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]
#[cfg(not(feature = "encode"))]
fn recv() {

View file

@ -151,7 +151,6 @@ pub enum Command<'a> {
}
impl<'a> ToMessage for Command<'a> {
/// Converts a Command into a Message.
#[stable]
fn to_message(&self) -> Message {
@ -162,8 +161,9 @@ impl<'a> ToMessage for Command<'a> {
Command::OPER(u, p) => Message::new(None, "OPER", Some(vec![u]), Some(p)),
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::SERVICE(n, r, d, t, re, i) => Message::new(None, "SERVICE",
Some(vec![n, r, d, t, re]), Some(i)),
Command::SERVICE(n, r, d, t, re, i) => Message::new(None, "SERVICE",
Some(vec![n, r, d, t, re]),
Some(i)),
Command::QUIT(Some(m)) => Message::new(None, "QUIT", None, Some(m)),
Command::QUIT(None) => Message::new(None, "QUIT", None, None),
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(None) => Message::new(None, "MOTD", None, None),
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(None, _) => Message::new(None, "LUSERS", None, None),
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(None) => Message::new(None, "TIME", None, None),
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::TRACE(Some(t)) => Message::new(None, "TRACE", None, Some(t)),
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(None) => Message::new(None, "INFO", None, None),
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(None, _) => Message::new(None, "SERVLIST", None, 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"]),
None),
None),
Command::WHO(Some(s), _) => Message::new(None, "WHO", Some(vec![s]), 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(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]),
Some(t)),
Some(t)),
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::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)),
@ -237,9 +237,9 @@ impl<'a> ToMessage for Command<'a> {
Command::DIE => Message::new(None, "DIE", 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]),
Some(c)),
Some(c)),
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::USERS(Some(t)) => Message::new(None, "USERS", None, Some(t)),
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::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]),
None),
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::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),
}
}
}
impl<'a> Command<'a> {

View file

@ -3,14 +3,6 @@
use std::borrow::ToOwned;
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.
#[experimental]
#[deriving(Clone, PartialEq, Show)]
@ -63,11 +55,9 @@ impl Message {
}
impl ToMessage for Message {
fn to_message(&self) -> Message {
self.clone()
}
}
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)]
mod test {
use super::Message;
use super::{Message, ToMessage};
#[test]
fn new() {
@ -151,4 +153,28 @@ mod test {
};
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)]
mod test {
use super::Response;
use data::message::ToMessage;
#[test]
fn 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);
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);
}