aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2016-04-24 08:44:04 -0400
committerTeddy Wing2016-04-24 08:44:04 -0400
commit7c9439bc11ae95b6f2da59eabf2950d15df4d558 (patch)
treea7ba31fadb59ee4892822eaee1f6b902154fb71e
parent66193af4e53799299c04ecf8715e44693f1bff8a (diff)
downloadmutt-alias-auto-add-7c9439bc11ae95b6f2da59eabf2950d15df4d558.tar.bz2
`write_to_file` test: Extract append lines to closure
We want to be able to write a test that ensures aliases can be written to the file even if they don't already exist. To do this, I want to be able to reuse the code in the helper function. We can't use the code that appends to the file because this is relevant only to the `alias_write_to_file_must_write_given_alias_to_file` test (which I just realised should actually be renamed to something more specific). In order to run this code without requiring it to be in the helper function, extract it to a closure that gets passed to the helper. We need to pass `alias` into the function explicitly in order to use it otherwise we get an error on an immutable borrow.
-rw-r--r--src/tests.rs21
1 files changed, 12 insertions, 9 deletions
diff --git a/src/tests.rs b/src/tests.rs
index e4ec254..c897a32 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -122,18 +122,13 @@ fn update_alias_id_increments_alias() {
}
-fn alias_write_to_file_helper(alias: &mut Alias) -> String {
+fn alias_write_to_file_helper<F>(alias: &mut Alias, f: F) -> String
+ where F: Fn(&Alias, &str) {
// Create a new test file
let test_file = "./testdata/write_to_file";
fs::copy("./testdata/aliases", test_file).expect("Alias file copy failed");
- // Write a duplicate alias so that `write_to_file` is able to append a
- // new one
- let mut f = OpenOptions::new().append(true).open(test_file)
- .expect("Failed to open test file for appending");
- writeln!(f, "{}", Alias { email: "derpy@home.pv".to_owned(), .. alias.clone() }
- .to_string())
- .expect("Failed to append matching alias");
+ f(alias, test_file);
// Write our new alias to the file
alias.write_to_file(test_file).expect("`write_to_file` failed");
@@ -151,7 +146,15 @@ fn alias_write_to_file_helper(alias: &mut Alias) -> String {
#[test]
fn alias_write_to_file_must_write_given_alias_to_file() {
let mut alias = update_alias_id_sample_alias();
- let alias_line = alias_write_to_file_helper(&mut alias);
+ let alias_line = alias_write_to_file_helper(&mut alias, |alias: &Alias, filename: &str| {
+ // Write a duplicate alias so that `write_to_file` is able to append a
+ // new one
+ let mut f = OpenOptions::new().append(true).open(filename)
+ .expect("Failed to open test file for appending");
+ writeln!(f, "{}", Alias { email: "derpy@home.pv".to_owned(), .. alias.clone() }
+ .to_string())
+ .expect("Failed to append matching alias");
+ });
assert_eq!(alias.to_string(), alias_line);
}