From bf3b4fa20746f074ebbdbe1b0d45a24da69837f5 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 22 Apr 2016 02:28:45 -0400 Subject: Make `update_alias_id` tests more DRY Eliminate some of the repeated setup code in our tests for this method. Instead of doing this work inside the test functions, move them to a new constant binding and function respectively. A `const` seemed like a good fit because our alias identifier is just a string. For the `Alias`, we have a new function that returns a test object for us, making it possible to get two mutable copies of it (one for each of our test functions). Tests still pass, I guess it worked. --- src/tests.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/tests.rs b/src/tests.rs index f877408..b3a172c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -90,27 +90,27 @@ fn find_alias_in_file_finds_a_match() { } -#[test] -fn update_alias_id_does_nothing_given_an_empty_vector() { - let alias_identifier = "hooves-derpy"; - let mut alias = Alias { - alias: alias_identifier.to_owned(), +const update_alias_id_alias_identifier: &'static str = "hooves-derpy"; + +fn update_alias_id_sample_alias() -> Alias { + Alias { + alias: update_alias_id_alias_identifier.to_owned(), name: "Derpy Hooves".to_owned(), email: "derpyhooves@postmaster.pv".to_owned() - }; + } +} + +#[test] +fn update_alias_id_does_nothing_given_an_empty_vector() { + let mut alias = update_alias_id_sample_alias(); alias.update_alias_id(vec![]); - assert_eq!(alias_identifier, &alias.alias); + assert_eq!(update_alias_id_alias_identifier, &alias.alias); } #[test] fn update_alias_id_increments_alias() { - let alias_identifier = "hooves-derpy"; - let mut alias = Alias { - alias: alias_identifier.to_owned(), - name: "Derpy Hooves".to_owned(), - email: "derpyhooves@postmaster.pv".to_owned() - }; + let mut alias = update_alias_id_sample_alias(); alias.update_alias_id(vec![ "hooves-derpy".to_owned(), "hooves-derpy-2".to_owned(), -- cgit v1.2.3