Clean up after #11, including unit tests.
This commit is contained in:
parent
202748e7a9
commit
a6146bc135
4 changed files with 87 additions and 32 deletions
41
src/conn.rs
41
src/conn.rs
|
@ -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()
|
||||
}
|
||||
|
||||
|
@ -255,6 +254,16 @@ mod test {
|
|||
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")]
|
||||
fn send_utf8() {
|
||||
|
@ -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() {
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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, 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)),
|
||||
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)),
|
||||
|
@ -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> {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue