From 8e99f80dca98c7d398541f37123141f65fed8df4 Mon Sep 17 00:00:00 2001 From: Matt McCoy Date: Tue, 21 Jun 2016 23:05:22 -0400 Subject: [PATCH] Add testing for the examine command --- src/client.rs | 37 +++++++++++++++++++++++++++++++++---- src/mailbox.rs | 9 +++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/client.rs b/src/client.rs index 0367536..8c37878 100644 --- a/src/client.rs +++ b/src/client.rs @@ -72,13 +72,13 @@ impl Client { 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 match self.parse_response_ok(lines.clone()) { @@ -312,6 +312,7 @@ mod tests { use super::*; use super::INITIAL_TAG; use super::super::mock_stream::MockStream; + use super::super::mailbox::Mailbox; fn create_client_with_mock_stream(mock_stream: MockStream) -> Client { Client { @@ -456,6 +457,34 @@ mod tests { 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] fn create() { // TODO Make sure the response was read correctly diff --git a/src/mailbox.rs b/src/mailbox.rs index f41491d..876bf85 100644 --- a/src/mailbox.rs +++ b/src/mailbox.rs @@ -1,3 +1,6 @@ +use std::fmt; + +#[derive(Eq,PartialEq)] pub struct Mailbox { pub flags: String, 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) + } +}