From 56f2486e5a5fae79ae1fd5a03f287f0191d4550b Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Thu, 1 May 2014 11:20:38 +0200 Subject: [PATCH] Add basic tests for SmtpResponse --- src/smtp/client.rs | 52 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/src/smtp/client.rs b/src/smtp/client.rs index 68d3833..fea323f 100644 --- a/src/smtp/client.rs +++ b/src/smtp/client.rs @@ -24,7 +24,7 @@ use commands; use commands::{SMTP_PORT, SmtpCommand, EsmtpParameter}; /// Contains an SMTP reply, with separed code and message -#[deriving(Clone)] +#[deriving(Clone, Eq)] pub struct SmtpResponse { /// Server response code code: uint, @@ -46,13 +46,13 @@ impl FromStr for SmtpResponse { if s.len() < 5 { None } else { - if vec!(" ", "-").contains(&s.slice(3,4)) { - Some(SmtpResponse{ - code: from_str(s.slice_to(3)).unwrap(), - message: StrBuf::from_str(s.slice_from(4)) - }) - } else { - None + match (from_str::(s.slice_to(3)), vec!(" ", "-").contains(&s.slice(3,4)), StrBuf::from_str(s.slice_from(4))) { + (Some(code), true, message) => Some(SmtpResponse{ + code: code, + message: message + }), + _ => None + } } } @@ -508,3 +508,39 @@ impl Writer for SmtpClient { self.stream.clone().unwrap().write_str(string) } } + +#[cfg(test)] +mod test { + use super::{SmtpResponse}; + + #[test] + fn test_smtp_response_fmt() { + assert!(format!("{}", SmtpResponse{code: 200, message: "message"}) == ~"200 message"); + } + + #[test] + fn test_smtp_response_from_str() { + assert!(from_str::>("200 response message") == + Some(SmtpResponse{ + code: 200, + message: StrBuf::from_str("response message") + }) + ); + assert!(from_str::>("200-response message") == + Some(SmtpResponse{ + code: 200, + message: StrBuf::from_str("response message") + }) + ); + assert!(from_str::>("2000response message") == None); + assert!(from_str::>("20a response message") == None); + } + + #[test] + fn test_smtp_response_with_code() { + assert!(SmtpResponse{code: 200, message: "message"}.with_code(vec!(200)) == + Ok(SmtpResponse{code: 200, message: "message"})); + assert!(SmtpResponse{code: 400, message: "message"}.with_code(vec!(200)) == + Err(SmtpResponse{code: 400, message: "message"})); + } +}