Skip to content

Commit c0e880f

Browse files
committed
#42 Infrastructure for deep unit test
1 parent 27426c8 commit c0e880f

File tree

1 file changed

+65
-8
lines changed

1 file changed

+65
-8
lines changed

src/lib.rs

+65-8
Original file line numberDiff line numberDiff line change
@@ -1146,22 +1146,61 @@ mod render {
11461146
}
11471147
}
11481148

1149-
impl TestsGroup {
1150-
pub fn get_test_functions(&self) -> Vec<ItemFn> {
1151-
let mut f = TestFunctions(vec![]);
1149+
trait ModuleInspector {
1150+
fn get_test_functions(&self) -> Vec<ItemFn>;
1151+
fn get_submodules(&self) -> Vec<ItemMod>;
1152+
}
11521153

1153-
f.visit_item_mod(&self.module);
1154+
impl ModuleInspector for ItemMod {
1155+
fn get_test_functions(&self) -> Vec<ItemFn> {
1156+
let mut f = TestFunctions(vec![]);
1157+
f.visit_item_mod(&self);
11541158
f.0
11551159
}
11561160

1157-
pub fn get_submodules(&self) -> Vec<ItemMod> {
1161+
fn get_submodules(&self) -> Vec<ItemMod> {
11581162
let mut f = SubModules(vec![]);
11591163

1160-
self.module.content.as_ref().map(|(_, items)| items.iter().for_each(|it| f.visit_item(it)));
1164+
self.content.as_ref().map(|(_, items)|
1165+
items.iter().for_each(|it| f.visit_item(it))
1166+
);
11611167
f.0
11621168
}
11631169
}
11641170

1171+
impl ModuleInspector for TestsGroup {
1172+
fn get_test_functions(&self) -> Vec<ItemFn> {
1173+
self.module.get_test_functions()
1174+
}
1175+
1176+
fn get_submodules(&self) -> Vec<ItemMod> {
1177+
self.module.get_submodules()
1178+
}
1179+
}
1180+
1181+
#[derive(Default, Debug)]
1182+
struct Assignments(HashMap<String, syn::Expr>);
1183+
1184+
impl <'ast> Visit<'ast> for Assignments {
1185+
//noinspection RsTypeCheck
1186+
fn visit_local(&mut self, assign: &syn::Local) {
1187+
match &assign {
1188+
syn::Local { pat: syn::Pat::Ident(pat), init: Some((_, expr)), .. } => {
1189+
self.0.insert(pat.ident.to_string(), expr.as_ref().clone());
1190+
},
1191+
_ => {}
1192+
}
1193+
}
1194+
}
1195+
1196+
impl Assignments {
1197+
pub fn collect_assignments(item_fn: &ItemFn) -> Self {
1198+
let mut collect = Self::default();
1199+
collect.visit_item_fn(item_fn);
1200+
collect
1201+
}
1202+
}
1203+
11651204
impl From<TokenStream> for TestsGroup {
11661205
fn from(tokens: TokenStream) -> Self {
11671206
syn::parse2::<TestsGroup>(tokens).unwrap()
@@ -1525,7 +1564,10 @@ mod render {
15251564
items: vec![fixture("fix", vec!["2"]).into(),
15261565
ident("a").into(), ident("b").into(),
15271566
vec!["1f64", "2f32"].into_iter().collect::<TestCase>().into(),
1528-
vec!["3f64", "4f32"].into_iter().collect::<TestCase>().into(),
1567+
TestCase {
1568+
description: Some(ident("description")),
1569+
..vec!["3f64", "4f32"].into_iter().collect::<TestCase>()
1570+
}.into(),
15291571
values_list("x", &["12", "-2"]).into(),
15301572
values_list("y", &["-3", "42"]).into(),
15311573
],
@@ -1535,7 +1577,22 @@ mod render {
15351577
let output = TestsGroup::from(tokens);
15361578

15371579
assert_eq!(output.module.ident, "should_be_the_outer_module_name");
1538-
assert_eq!(2, output.get_submodules().len());
1580+
1581+
let sub_modules = output.get_submodules();
1582+
assert_eq!(2, sub_modules.len());
1583+
1584+
assert_eq!(sub_modules[0].ident, "case_1");
1585+
assert_eq!(sub_modules[1].ident, "case_2_description");
1586+
1587+
// 4 foreach modules
1588+
assert_eq!(4, sub_modules[0].get_test_functions().len());
1589+
assert_eq!(4, sub_modules[1].get_test_functions().len());
1590+
1591+
// No more
1592+
assert_eq!(8, output.get_test_functions().len());
1593+
1594+
dbg!(Assignments::collect_assignments(&sub_modules[0].get_test_functions()[0]));
1595+
assert!(false);
15391596
}
15401597
}
15411598

0 commit comments

Comments
 (0)