Refactor tests to better support UID commands

This commit is contained in:
Miquel Ruiz 2016-07-12 10:37:09 +01:00
parent eb0b4d9b50
commit 6e872bb340

View file

@ -390,32 +390,6 @@ mod tests {
assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid rename command"); assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid rename command");
} }
#[test]
fn fetch() {
generic_fetch(false)
}
#[test]
fn uid_fetch() {
generic_fetch(true)
}
fn generic_fetch(uid: bool) {
let response = b"a1 OK FETCH completed\r\n".to_vec();
let sequence_set = "1";
let query = "BODY[]";
let uid_cmd = if uid { " UID " } else { " " };
let command = format!("a1{}FETCH {} {}\r\n", uid_cmd, sequence_set, query);
let mock_stream = MockStream::new(response);
let mut client = Client::new(mock_stream);
if uid {
client.uid_fetch(sequence_set, query).unwrap();
} else {
client.fetch(sequence_set, query).unwrap();
}
assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid fetch command");
}
#[test] #[test]
fn subscribe() { fn subscribe() {
let response = b"a1 OK SUBSCRIBE completed\r\n".to_vec(); let response = b"a1 OK SUBSCRIBE completed\r\n".to_vec();
@ -566,56 +540,91 @@ mod tests {
#[test] #[test]
fn store() { fn store() {
generic_store(false); generic_store(" ", |mut c, set, query| c.store(set, query));
} }
#[test] #[test]
fn uid_store() { fn uid_store() {
generic_store(true); generic_store(" UID ", |mut c, set, query| c.uid_store(set, query));
} }
fn generic_store(uid: bool) { fn generic_store<F, T, E>(prefix: &str, op: F)
let response = b"* 2 FETCH (FLAGS (\\Deleted \\Seen))\r\n\ where F: FnOnce(&mut Client<MockStream>, &str, &str) -> Result<T, E> {
let res = "* 2 FETCH (FLAGS (\\Deleted \\Seen))\r\n\
* 3 FETCH (FLAGS (\\Deleted))\r\n\ * 3 FETCH (FLAGS (\\Deleted))\r\n\
* 4 FETCH (FLAGS (\\Deleted \\Flagged \\Seen))\r\n\ * 4 FETCH (FLAGS (\\Deleted \\Flagged \\Seen))\r\n\
a1 OK STORE completed\r\n".to_vec(); a1 OK STORE completed\r\n";
let sequence_set = "2:4";
let query = "+FLAGS (\\Deleted)"; generic_with_uid(
let uid_cmd = if uid { " UID " } else { " " }; res,
let command = format!("a1{}STORE {} {}\r\n", uid_cmd, sequence_set, query); "STORE",
let mock_stream = MockStream::new(response); "2.4",
let mut client = Client::new(mock_stream); "+FLAGS (\\Deleted)",
if uid { prefix,
client.uid_store(sequence_set, query).unwrap(); op,
} else { );
client.store(sequence_set, query).unwrap();
}
assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid store command");
} }
#[test] #[test]
fn copy() { fn copy() {
generic_copy(false) generic_copy(" ", |mut c, set, query| c.copy(set, query))
} }
#[test] #[test]
fn uid_copy() { fn uid_copy() {
generic_copy(true) generic_copy(" UID ", |mut c, set, query| c.uid_copy(set, query))
} }
fn generic_copy(uid: bool) { fn generic_copy<F, T, E>(prefix: &str, op: F)
let response = b"a1 OK COPY completed\r\n".to_vec(); where F: FnOnce(&mut Client<MockStream>, &str, &str) -> Result<T, E> {
let sequence_set = "2:4";
let mailbox = "MEETING"; generic_with_uid(
let uid_cmd = if uid { " UID " } else { " " }; "OK COPY completed\r\n",
let command = format!("a1{}COPY {} {}\r\n", uid_cmd, sequence_set, mailbox); "COPY",
let mock_stream = MockStream::new(response); "2:4",
let mut client = Client::new(mock_stream); "MEETING",
if uid { prefix,
client.uid_copy(sequence_set, mailbox).unwrap(); op,
} else { );
client.copy(sequence_set, mailbox).unwrap();
} }
assert!(client.stream.written_buf == command.as_bytes().to_vec(), "Invalid copy command");
#[test]
fn fetch() {
generic_fetch(" ", |mut c, seq, query| c.fetch(seq, query))
}
#[test]
fn uid_fetch() {
generic_fetch(" UID ", |mut c, seq, query| c.uid_fetch(seq, query))
}
fn generic_fetch<F, T, E>(prefix: &str, op: F)
where F: FnOnce(&mut Client<MockStream>, &str, &str) -> Result<T, E> {
generic_with_uid(
"OK FETCH completed\r\n",
"FETCH",
"1",
"BODY[]",
prefix,
op
);
}
fn generic_with_uid<F, T, E>(
res: &str,
cmd: &str,
seq: &str,
query: &str,
prefix: &str,
op: F) where F: FnOnce(&mut Client<MockStream>, &str, &str) -> Result<T, E>,
{
let resp = format!("a1 {}\r\n", res).as_bytes().to_vec();
let line = format!("a1{}{} {} {}\r\n", prefix, cmd, seq, query);
let mut client = Client::new(MockStream::new(resp));
let _ = op(&mut client, seq, query);
assert!(client.stream.written_buf == line.as_bytes().to_vec(), "Invalid command");
} }
} }