rusty-diceware

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

commit 592c7055db02203562abfa37337b62383fb401dd
parent 70096f952572c48e1e13b5cb1da3a6b66d34b5eb
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Sat, 13 Jun 2015 00:42:57 +0300

Adding some tests

Diffstat:
Msrc/main.rs | 52++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 50 insertions(+), 2 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -16,12 +16,24 @@ mod diceware { include!(concat!(env!("OUT_DIR"), "/diceware.rs")); - #[derive(Debug,Clone)] + #[derive(Debug,Clone,Eq,PartialEq)] pub struct BealeWord(&'static str); - #[derive(Debug,Clone)] + #[derive(Debug,Clone,Eq,PartialEq)] pub struct ReinholdWord(&'static str); + impl BealeWord { + pub fn new(word: &'static str) -> BealeWord { + BealeWord(word.clone()) + } + } + + impl ReinholdWord { + pub fn new(word: &'static str) -> ReinholdWord { + ReinholdWord(word.clone()) + } + } + impl rand::Rand for BealeWord { fn rand<R: rand::Rng>(rng: &mut R) -> BealeWord { rng.choose(&BEALE_WORDLIST).unwrap().clone() @@ -78,6 +90,7 @@ fn print_usage(program: &str, opts: Options) { */ +#[cfg(not(test))] fn main() { /* let args: Vec<String> = env::args().collect(); @@ -110,3 +123,38 @@ fn main() { } println!(""); } + +#[cfg(test)] +mod test { + extern crate rand; + + use rand::{Rng, SeedableRng, StdRng}; + + use diceware::{ReinholdWord, BealeWord}; + + fn make_beale_word() -> BealeWord { + let seed: &[_] = &[1, 2, 3, 4]; + let mut rng: StdRng = SeedableRng::from_seed(seed); + let word = rng.gen(); + word + } + + fn make_reinhold_word() -> ReinholdWord { + let seed: &[_] = &[1, 2, 3, 4]; + let mut rng: StdRng = SeedableRng::from_seed(seed); + let word = rng.gen(); + word + } + + #[test] + fn beale_rng_test() { + let rand_word = make_beale_word(); + assert_eq!(rand_word, BealeWord::new("ladder")) + } + + #[test] + fn reinhold_rng_test() { + let rand_word = make_reinhold_word(); + assert_eq!(rand_word, ReinholdWord::new("ks")) + } +}