1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
use std::iter::Iterator; use std::vec::IntoIter; use std::rc::Rc; use std::cell::RefCell; use std::fmt::Debug; use validator::Validator; use ansi_term::Colour::*; pub struct Table<I, E> { values: IntoIter<(I, E)>, nb_test_failed: Rc<RefCell<usize>>, nb_of_tests: usize, } impl<I, E> Table<I, E> { pub fn new(values: Vec<(I, E)>) -> Table<I, E> { Table { nb_of_tests: values.len(), values: values.into_iter(), nb_test_failed: Rc::new(RefCell::new(0)), } } } impl<I, E> Drop for Table<I, E> { fn drop(&mut self) { let nb_test_failed = *self.nb_test_failed.borrow(); if nb_test_failed > 0 { println!("\n--------------------"); let nb_test_passed = Green .bold() .paint(format!("{}", self.nb_of_tests - nb_test_failed)); let nb_test_failed = Red.bold().paint(format!("{}", nb_test_failed)); println!("{} Passed {} Failed\n", nb_test_passed, nb_test_failed); panic!("Test Failed"); } } } impl<I: Debug, E: Debug> Iterator for Table<I, E> { type Item = (Validator, I, E); fn next(&mut self) -> Option<Self::Item> { if let Some(value) = self.values.next() { Some(( Validator::new(Rc::clone(&self.nb_test_failed)), value.0, value.1, )) } else { None } } } #[cfg(test)] mod tests { use super::*; #[test] #[should_panic] pub fn given_ugly_names_when_validate_is_beautiful_then_table_panic() { let table = Table::new(vec![ ("an ugly name", false), ("a beautiful name", true), ("an amazing name", true), ("worst name ever", false), ]); for (validator, name, expected) in table { validator .given(name) .when("validate is beautiful") .then(&format!("{}", expected)) .assert_eq(expected, true); } } #[test] pub fn given_wonderful_names_when_validate_is_beautiful_then_table_dont_panic() { let table = Table::new(vec![("a beautiful name", true), ("an amazing name", true)]); for (validator, name, expected) in table { validator .given(name) .when("validate is beatifull") .then(&format!("{}", expected)) .assert_eq(expected, true); } } }