Add testing for the examine command

This commit is contained in:
Matt McCoy 2016-06-21 23:05:22 -04:00
parent bebcfab52c
commit 8e99f80dca
2 changed files with 42 additions and 4 deletions

View file

@ -72,13 +72,13 @@ impl<T: Read+Write> Client<T> {
let flags_regex = Regex::new(r"^\* FLAGS (.+)\r\n").unwrap(); let flags_regex = Regex::new(r"^\* FLAGS (.+)\r\n").unwrap();
let unseen_regex = Regex::new(r"^OK \[UNSEEN (\d+)\](.*)\r\n").unwrap(); let unseen_regex = Regex::new(r"^\* OK \[UNSEEN (\d+)\](.*)\r\n").unwrap();
let uid_validity_regex = Regex::new(r"^OK \[UIDVALIDITY (\d+)\](.*)\r\n").unwrap(); let uid_validity_regex = Regex::new(r"^\* OK \[UIDVALIDITY (\d+)\](.*)\r\n").unwrap();
let uid_next_regex = Regex::new(r"^OK \[UIDNEXT (\d+)\](.*)\r\n").unwrap(); let uid_next_regex = Regex::new(r"^\* OK \[UIDNEXT (\d+)\](.*)\r\n").unwrap();
let permanent_flags_regex = Regex::new(r"^OK \[PERMANENTFLAGS (.+)\]\r\n").unwrap(); let permanent_flags_regex = Regex::new(r"^\* OK \[PERMANENTFLAGS (.+)\]\r\n").unwrap();
//Check Ok //Check Ok
match self.parse_response_ok(lines.clone()) { match self.parse_response_ok(lines.clone()) {
@ -312,6 +312,7 @@ mod tests {
use super::*; use super::*;
use super::INITIAL_TAG; use super::INITIAL_TAG;
use super::super::mock_stream::MockStream; use super::super::mock_stream::MockStream;
use super::super::mailbox::Mailbox;
fn create_client_with_mock_stream(mock_stream: MockStream) -> Client<MockStream> { fn create_client_with_mock_stream(mock_stream: MockStream) -> Client<MockStream> {
Client { Client {
@ -456,6 +457,34 @@ mod tests {
assert!(client.stream.written_buf == b"a1 CHECK\r\n".to_vec(), "Invalid check command"); assert!(client.stream.written_buf == b"a1 CHECK\r\n".to_vec(), "Invalid check command");
} }
#[test]
fn examine() {
let response = b"* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)\r\n\
* OK [PERMANENTFLAGS ()] Read-only mailbox.\r\n\
* 1 EXISTS\r\n\
* 1 RECENT\r\n\
* OK [UNSEEN 1] First unseen.\r\n\
* OK [UIDVALIDITY 1257842737] UIDs valid\r\n\
* OK [UIDNEXT 2] Predicted next UID\r\n\
a1 OK [READ-ONLY] Select completed.\r\n".to_vec();
let expected_mailbox = Mailbox {
flags: String::from("(\\Answered \\Flagged \\Deleted \\Seen \\Draft)"),
exists: 1,
recent: 1,
unseen: Some(1),
permanent_flags: None,
uid_next: Some(2),
uid_validity: Some(1257842737)
};
let mailbox_name = "INBOX";
let command = format!("a1 EXAMINE {}\r\n", mailbox_name);
let mock_stream = MockStream::new(response);
let mut client = create_client_with_mock_stream(mock_stream);
let mailbox = client.examine(mailbox_name).unwrap();
assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid create command");
assert!(mailbox == expected_mailbox, "Unexpected mailbox returned");
}
#[test] #[test]
fn create() { fn create() {
// TODO Make sure the response was read correctly // TODO Make sure the response was read correctly

View file

@ -1,3 +1,6 @@
use std::fmt;
#[derive(Eq,PartialEq)]
pub struct Mailbox { pub struct Mailbox {
pub flags: String, pub flags: String,
pub exists: u32, pub exists: u32,
@ -21,3 +24,9 @@ impl Default for Mailbox {
} }
} }
} }
impl fmt::Display for Mailbox {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "flags: {}, exists: {}, recent: {}, unseen: {:?}, permanent_flags: {:?}, uid_next: {:?}, uid_validity: {:?}", self.flags, self.exists, self.recent, self.unseen, self.permanent_flags, self.uid_next, self.uid_validity)
}
}