rusty-diceware

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit bd203aaa99c476cb22caf5f7e648927b9e7ea2d8
parent 70121dcbaca9fa04ac5d4f8e380dcc2e5cb8781f
Author: Valentin Brandl <valentin.brandl@ebsnet.de>
Date:   Tue,  6 Sep 2022 10:33:44 +0200

Implement tests using a macro

Diffstat:
Mtests/tests.rs | 78+++++++++++++++++++++++++++++++++---------------------------------------------
1 file changed, 33 insertions(+), 45 deletions(-)

diff --git a/tests/tests.rs b/tests/tests.rs @@ -8,50 +8,38 @@ use diceware::BealeWord; use diceware::ReinholdWord; use diceware::Word; -fn make_beale_vector() -> Vec<BealeWord> { - let seed: [u8; 32] = [0; 32]; - let mut rng: StdRng = SeedableRng::from_seed(seed); - - let mut beale_vector: Vec<BealeWord> = vec![]; - for _ in 0..4 { - let word: BealeWord = rng.gen(); - beale_vector.push(word); - } - beale_vector +macro_rules! create_test { + ( $gen_name: ident, $test_name: ident, $expected: expr ) => { + #[test] + fn $test_name() { + fn make_vektor() -> Vec<$gen_name> { + let seed: [u8; 32] = [0; 32]; + let mut rng: StdRng = SeedableRng::from_seed(seed); + + let mut vector: Vec<$gen_name> = vec![]; + for _ in 0..4 { + let word: $gen_name = rng.gen(); + vector.push(word); + } + vector + } + let wanted: Vec<$gen_name> = $expected.into_iter().map($gen_name::new).collect(); + + let got = make_vektor(); + + assert_eq!(got, wanted); + } + }; } -fn make_reinhold_vector() -> Vec<ReinholdWord> { - let seed: [u8; 32] = [0; 32]; - let mut rng: StdRng = SeedableRng::from_seed(seed); - - let mut reinhold_vector: Vec<ReinholdWord> = vec![]; - for _ in 0..4 { - let word: ReinholdWord = rng.gen(); - reinhold_vector.push(word); - } - reinhold_vector -} - -#[test] -fn beale_rng_test() { - let wanted: Vec<BealeWord> = vec!["io", "gavel", "beam", "time"] - .into_iter() - .map(BealeWord::new) - .collect(); - - let got = make_beale_vector(); - - assert_eq!(got, wanted); -} - -#[test] -fn reinhold_rng_test() { - let wanted: Vec<ReinholdWord> = vec!["india", "gamma", "bcd", "theme"] - .into_iter() - .map(ReinholdWord::new) - .collect(); - - let got = make_reinhold_vector(); - - assert_eq!(got, wanted); -} +create_test!( + BealeWord, + beale_rng_test, + vec!["io", "gavel", "beam", "time"] +); + +create_test!( + ReinholdWord, + reinhold_rng_test, + vec!["india", "gamma", "bcd", "theme"] +);