Added some unit tests for IrcBot, along with required changes to Connection.

This commit is contained in:
Aaron Weiss 2014-10-08 22:09:35 -04:00
parent 31e633763b
commit a7d3a565b8
2 changed files with 47 additions and 7 deletions

View file

@ -81,10 +81,9 @@ impl<'a, T, U> Bot<'a> for IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
impl<'a, T, U> IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
pub fn from_connection(conn: Connection<T, U>, process: |&IrcBot<T, U>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<IrcBot<'a, T, U>> {
let config = try!(Config::load());
Ok(IrcBot {
conn: conn,
config: config,
config: try!(Config::load()),
process: RefCell::new(process),
chanlists: RefCell::new(HashMap::new()),
})
@ -154,3 +153,37 @@ impl<'a, T, U> IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
Ok(())
}
}
#[cfg(test)]
mod test {
use Bot;
use super::IrcBot;
use std::io::MemWriter;
use std::io::util::NullReader;
use conn::Connection;
#[test]
fn from_connection() {
let w = MemWriter::new();
let c = Connection::new(w, NullReader).unwrap();
assert!(IrcBot::from_connection(c, |_, _, _, _| { Ok(()) }).is_ok());
}
#[test]
fn send_nick() {
let w = MemWriter::new();
let c = Connection::new(w, NullReader).unwrap();
let b = IrcBot::from_connection(c, |_, _, _, _| { Ok(()) }).unwrap();
b.send_nick("test").unwrap();
assert_eq!(b.conn.writer().deref_mut().get_ref(), "NICK :test\r\n".as_bytes());
}
#[test]
fn send_user() {
let w = MemWriter::new();
let c = Connection::new(w, NullReader).unwrap();
let b = IrcBot::from_connection(c, |_, _, _, _| { Ok(()) }).unwrap();
b.send_user("test", "Test").unwrap();
assert_eq!(b.conn.writer().deref_mut().get_ref(), "USER test 0 * :Test\r\n".as_bytes());
}
}

View file

@ -15,7 +15,7 @@ impl Connection<BufferedWriter<TcpStream>, BufferedReader<TcpStream>> {
}
impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
fn new(writer: T, reader: U) -> IoResult<Connection<T, U>> {
pub fn new(writer: T, reader: U) -> IoResult<Connection<T, U>> {
Ok(Connection {
writer: RefCell::new(writer),
reader: RefCell::new(reader),
@ -30,13 +30,20 @@ impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
pub fn send(&self, msg: Message) -> IoResult<()> {
let mut send = msg.command.to_string();
send.push_str(msg.args.init().connect(" ").as_slice());
if msg.args.init().len() > 0 {
send.push_str(" ");
send.push_str(msg.args.init().connect(" ").as_slice());
}
send.push_str(" :");
send.push_str(*msg.args.last().unwrap());
send.push_str("\r\n");
self.send_internal(send.as_slice())
}
pub fn writer<'a>(&'a self) -> RefMut<'a, T> {
self.writer.borrow_mut()
}
pub fn reader<'a>(&'a self) -> RefMut<'a, U> {
self.reader.borrow_mut()
}
@ -44,10 +51,10 @@ impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
#[cfg(test)]
mod test {
use super::Connection;
use std::io::MemWriter;
use std::io::util::NullReader;
use data::Message;
use super::Connection;
#[test]
fn new_connection() {
@ -60,7 +67,7 @@ mod test {
let w = MemWriter::new();
let c = Connection::new(w, NullReader).unwrap();
c.send_internal("string of text").unwrap();
assert_eq!(c.writer.borrow_mut().deref_mut().get_ref(), "string of text".as_bytes());
assert_eq!(c.writer().deref_mut().get_ref(), "string of text".as_bytes());
}
#[test]
@ -69,6 +76,6 @@ mod test {
let c = Connection::new(w, NullReader).unwrap();
let args = ["flare.to.ca.fyrechat.net"];
c.send(Message::new(None, "PING", args)).unwrap();
assert_eq!(c.writer.borrow_mut().deref_mut().get_ref(), "PING :flare.to.ca.fyrechat.net\r\n".as_bytes());
assert_eq!(c.writer().deref_mut().get_ref(), "PING :flare.to.ca.fyrechat.net\r\n".as_bytes());
}
}