From 165e7969db02ffab9fd6deff2c7f945840b000a5 Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Wed, 5 Nov 2014 01:45:17 -0500 Subject: [PATCH] Added some basic tests to newly redesigned library. Test coverage is still less than satisfactory. --- .travis.yml | 2 ++ mktestconfig.sh | 1 + src/conn.rs | 22 ++++++++++++++++ src/data/config.rs | 64 ++++++++++++++++++++++++++++++++++++++++++--- src/data/message.rs | 52 ++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 4 deletions(-) create mode 100755 mktestconfig.sh diff --git a/.travis.yml b/.travis.yml index b284ea8..6eae4d7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: rust script: + - chmod +x mktestconfig.sh + - ./mktestconfig.sh - cargo build --verbose - cargo test --verbose - cargo doc --verbose diff --git a/mktestconfig.sh b/mktestconfig.sh new file mode 100755 index 0000000..541a0a6 --- /dev/null +++ b/mktestconfig.sh @@ -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 diff --git a/src/conn.rs b/src/conn.rs index 533de00..236b8f4 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -51,3 +51,25 @@ impl Connection 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"); + } +} diff --git a/src/data/config.rs b/src/data/config.rs index 3b65f26..46d1b5d 100644 --- a/src/data/config.rs +++ b/src/data/config.rs @@ -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 { 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::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")); + } +} diff --git a/src/data/message.rs b/src/data/message.rs index 7039608..6b06ce0 100644 --- a/src/data/message.rs +++ b/src/data/message.rs @@ -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)); + } +}