aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2016-04-21 14:59:17 -0400
committerTeddy Wing2016-04-21 14:59:17 -0400
commit763d47befd80547f12525c01cc5a103d1ef5f285 (patch)
tree7dace6cac7c907e43d48823e1a771e6be4556cee /src
parent9b56cecda8fb33cb006d40d2e367bca2796c7d23 (diff)
downloadmutt-alias-auto-add-763d47befd80547f12525c01cc5a103d1ef5f285.tar.bz2
Add test find_alias_in_file_email_already_exists
When the email of the alias we're looking is already in the file, expect an `AliasSearchError::EmailExists` error. Oh man, this was a tough one. After much searching, finally figured out how to implement the `PartialEq` trait for my error type so that we could actually test it. Many thanks to @peterbudai for an example in the 'redux' project of how to do this (https://github.com/peterbudai/redux/blob/ef5d47a0a64cef9fa9e1e9c6f21badc46fa283fc/src/lib.rs): #[cfg(test)] impl PartialEq<Error> for Error { fn eq(&self, other: &Error) -> bool { match *self { Error::Eof => match *other { Error::Eof => true, _ => false }, Error::InvalidInput => match *other { Error::InvalidInput => true, _ => false }, Error::IoError(_) => match *other { Error::IoError(_) => true, _ => false }, } } } With that example, I was able to correctly build an equality function to get past my compiler errors which complained that an implementation of `std::cmp::PartialEq` might be missing for `std::io::error::Error` when I tried to `#[derive(PartialEq)]` on my `AliasSearchError` type.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs20
-rw-r--r--src/tests.rs21
2 files changed, 32 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index aebabef..4c0cf26 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -97,6 +97,26 @@ impl From<io::Error> for AliasSearchError {
}
}
+#[cfg(test)]
+impl PartialEq<AliasSearchError> for AliasSearchError {
+ fn eq(&self, other: &AliasSearchError) -> bool {
+ match *self {
+ AliasSearchError::NotFound => match *other {
+ AliasSearchError::NotFound => true,
+ _ => false,
+ },
+ AliasSearchError::EmailExists => match *other {
+ AliasSearchError::EmailExists => true,
+ _ => false,
+ },
+ AliasSearchError::Io(_) => match *other {
+ AliasSearchError::Io(_) => true,
+ _ => false,
+ },
+ }
+ }
+}
+
fn find_alias_in_file(alias: &Alias, file: &str) -> Result<Vec<String>, AliasSearchError> {
let mut matches = Vec::new();
let f = try!(File::open(file));
diff --git a/src/tests.rs b/src/tests.rs
index ccacd29..a6f2777 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -1,4 +1,4 @@
-use super::{Alias, find_alias_in_file};
+use super::{Alias, AliasSearchError, find_alias_in_file};
#[test]
fn new_alias_with_only_email() {
@@ -42,13 +42,16 @@ fn new_alias_with_special_characters() {
#[test]
-fn find_alias_in_file_finds_a_match() {
- find_alias_in_file(
- &Alias {
- alias: "farnsworth-hubert".to_owned(),
- name: "Hubert Farnsworth".to_owned(),
- email: "<professor@planetexpress.com>".to_owned()
- },
- "./testdata/aliases"
+fn find_alias_in_file_email_already_exists() {
+ assert_eq!(
+ Err(AliasSearchError::EmailExists),
+ find_alias_in_file(
+ &Alias {
+ alias: "farnsworth-hubert".to_owned(),
+ name: "Hubert Farnsworth".to_owned(),
+ email: "<professor@planetexpress.com>".to_owned()
+ },
+ "./testdata/aliases"
+ )
);
}