Added some basic tests to newly redesigned library. Test coverage is still less than satisfactory.

This commit is contained in:
Aaron Weiss 2014-11-05 01:45:17 -05:00
parent 503f14ea5a
commit 165e7969db
5 changed files with 137 additions and 4 deletions

View file

@ -1,5 +1,7 @@
language: rust
script:
- chmod +x mktestconfig.sh
- ./mktestconfig.sh
- cargo build --verbose
- cargo test --verbose
- cargo doc --verbose

1
mktestconfig.sh Executable file
View file

@ -0,0 +1 @@
echo "{\"owners\": [\"test\"],\"nickname\": \"test\",\"username\": \"test\",\"realname\": \"test\",\"password\": \"\",\"server\": \"irc.test.net\",\"port\": 6667,\"channels\": [\"#test\", \"#test2\"],\"options\": {}}" > config.json

View file

@ -51,3 +51,25 @@ impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
self.writer.lock()
}
}
#[cfg(test)]
mod test {
use super::Connection;
use std::io::{MemReader, MemWriter};
use std::io::util::{NullReader, NullWriter};
use data::message::Message;
#[test]
fn send() {
let conn = Connection::new(MemWriter::new(), NullReader);
assert!(conn.send(Message::new(None, "PRIVMSG", Some(vec!["test"]), Some("Testing!"))).is_ok());
let data = String::from_utf8(conn.writer().get_ref().to_vec()).unwrap();
assert_eq!(data[], "PRIVMSG test :Testing!\r\n");
}
#[test]
fn recv() {
let conn = Connection::new(NullWriter, MemReader::new("PRIVMSG test :Testing!\r\n".as_bytes().to_vec()));
assert_eq!(conn.recv().unwrap()[], "PRIVMSG test :Testing!\r\n");
}
}

View file

@ -6,7 +6,7 @@ use std::io::{InvalidInput, IoError, IoResult};
use serialize::json::decode;
/// Configuration data
#[deriving(Clone, Decodable)]
#[deriving(Clone, Decodable, PartialEq, Show)]
#[unstable]
pub struct Config {
/// A list of the owners of the bot by nickname
@ -30,7 +30,7 @@ pub struct Config {
}
impl Config {
/// Loads a JSON configuration from the desired path.
/// Loads a JSON configuration from the desired path
#[stable]
pub fn load(path: Path) -> IoResult<Config> {
let mut file = try!(File::open(&path));
@ -42,15 +42,71 @@ impl Config {
})
}
/// Loads a JSON configuration using the string as a UTF-8 path.
/// Loads a JSON configuration using the string as a UTF-8 path
#[stable]
pub fn load_utf8(path: &str) -> IoResult<Config> {
Config::load(Path::new(path))
}
/// Determines whether or not the nickname provided is the owner of the bot.
/// Determines whether or not the nickname provided is the owner of the bot
#[stable]
pub fn is_owner(&self, nickname: &str) -> bool {
self.owners[].contains(&String::from_str(nickname))
}
}
#[cfg(test)]
mod test {
use super::Config;
use std::collections::HashMap;
#[test]
fn load() {
let cfg = Config {
owners: vec![format!("test")],
nickname: format!("test"),
username: format!("test"),
realname: format!("test"),
password: String::new(),
server: format!("irc.test.net"),
port: 6667,
channels: vec![format!("#test"), format!("#test2")],
options: HashMap::new(),
};
assert_eq!(Config::load(Path::new("config.json")), Ok(cfg));
}
#[test]
fn load_utf8() {
let cfg = Config {
owners: vec![format!("test")],
nickname: format!("test"),
username: format!("test"),
realname: format!("test"),
password: String::new(),
server: format!("irc.test.net"),
port: 6667,
channels: vec![format!("#test"), format!("#test2")],
options: HashMap::new(),
};
assert_eq!(Config::load_utf8("config.json"), Ok(cfg));
}
#[test]
fn is_owner() {
let cfg = Config {
owners: vec![format!("test"), format!("test2")],
nickname: format!("test"),
username: format!("test"),
realname: format!("test"),
password: String::new(),
server: format!("irc.test.net"),
port: 6667,
channels: Vec::new(),
options: HashMap::new(),
};
assert!(cfg.is_owner("test"));
assert!(cfg.is_owner("test2"));
assert!(!cfg.is_owner("test3"));
}
}

View file

@ -82,3 +82,55 @@ impl FromStr for Message {
Some(Message::new(prefix, command, if args.len() > 0 { Some(args) } else { None }, suffix))
}
}
#[cfg(test)]
mod test {
use super::Message;
#[test]
fn new() {
let message = Message {
prefix: None,
command: format!("PRIVMSG"),
args: vec![format!("test")],
suffix: Some(format!("Testing!")),
};
assert_eq!(Message::new(None, "PRIVMSG", Some(vec!["test"]), Some("Testing!")), message);
}
#[test]
fn into_string() {
let message = Message {
prefix: None,
command: format!("PRIVMSG"),
args: vec![format!("test")],
suffix: Some(format!("Testing!")),
};
assert_eq!(message.into_string()[], "PRIVMSG test :Testing!\r\n");
let message = Message {
prefix: Some(format!("test!test@test")),
command: format!("PRIVMSG"),
args: vec![format!("test")],
suffix: Some(format!("Still testing!")),
};
assert_eq!(message.into_string()[], ":test!test@test PRIVMSG test :Still testing!\r\n");
}
#[test]
fn from_string() {
let message = Message {
prefix: None,
command: format!("PRIVMSG"),
args: vec![format!("test")],
suffix: Some(format!("Testing!")),
};
assert_eq!(from_str("PRIVMSG test :Testing!\r\n"), Some(message));
let message = Message {
prefix: Some(format!("test!test@test")),
command: format!("PRIVMSG"),
args: vec![format!("test")],
suffix: Some(format!("Still testing!")),
};
assert_eq!(from_str(":test!test@test PRIVMSG test :Still testing!\r\n"), Some(message));
}
}