From 510eaae73ac1e0169c57375cd432d89d2ad5e487 Mon Sep 17 00:00:00 2001 From: z78078 Date: Wed, 20 Jul 2022 16:50:09 +0800 Subject: [PATCH] nac3core: Add test for toplevel::composer::build_constructor_lookup --- nac3core/src/toplevel/test.rs | 145 ++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/nac3core/src/toplevel/test.rs b/nac3core/src/toplevel/test.rs index 49400711..6ad6fceb 100644 --- a/nac3core/src/toplevel/test.rs +++ b/nac3core/src/toplevel/test.rs @@ -566,6 +566,151 @@ fn test_analyze(source: Vec<&str>, res: Vec<&str>) { } } +#[test_case( + indoc! {" + class A: + def __init__(self): + pass + + class B(A): + pass + + class C(B): + pass + "}, + HashMap::from([ + ("A".into(),ast::Located { + location: ast::Location::new(2, 5, ast::FileName("unknown".into())), + custom: (), + node: ast::StmtKind::FunctionDef { + name: "__init__".into(), + args: Box::new(ast::Arguments { + posonlyargs: vec![], + args: vec![ + ast::Located { + location: ast::Location::new(2, 18, ast::FileName("unknown".into())), + custom: (), + node: ast::ArgData { + arg: "self".into(), + annotation: None, + type_comment: None, + }, + }, + ], + vararg: None, + kwonlyargs: vec![], + kw_defaults: vec![], + kwarg: None, + defaults: vec![], + }), + body: vec![ + ast::Located { + location: Location::new(3, 9, ast::FileName("unknown".into())), + custom: (), + node: ast::StmtKind::Pass { + config_comment: vec![], + }, + }, + ], + decorator_list: vec![], + returns: None, + type_comment: None, + config_comment: vec![], + }, + }), + ("B".into(), ast::Located { + location: ast::Location::new(2, 5, ast::FileName("unknown".into())), + custom: (), + node: ast::StmtKind::FunctionDef { + name: "__init__".into(), + args: Box::new(ast::Arguments { + posonlyargs: vec![], + args: vec![ + ast::Located { + location: ast::Location::new(2, 18, ast::FileName("unknown".into())), + custom: (), + node: ast::ArgData { + arg: "self".into(), + annotation: None, + type_comment: None, + }, + }, + ], + vararg: None, + kwonlyargs: vec![], + kw_defaults: vec![], + kwarg: None, + defaults: vec![], + }), + body: vec![ + ast::Located { + location: Location::new(3, 9, ast::FileName("unknown".into())), + custom: (), + node: ast::StmtKind::Pass { + config_comment: vec![], + }, + }, + ], + decorator_list: vec![], + returns: None, + type_comment: None, + config_comment: vec![], + }, + }), + ("C".into(), ast::Located { + location: ast::Location::new(2, 5, ast::FileName("unknown".into())), + custom: (), + node: ast::StmtKind::FunctionDef { + name: "__init__".into(), + args: Box::new(ast::Arguments { + posonlyargs: vec![], + args: vec![ + ast::Located { + location: ast::Location::new(2, 18, ast::FileName("unknown".into())), + custom: (), + node: ast::ArgData { + arg: "self".into(), + annotation: None, + type_comment: None, + }, + }, + ], + vararg: None, + kwonlyargs: vec![], + kw_defaults: vec![], + kwarg: None, + defaults: vec![], + }), + body: vec![ + ast::Located { + location: Location::new(3, 9, ast::FileName("unknown".into())), + custom: (), + node: ast::StmtKind::Pass { + config_comment: vec![], + }, + }, + ], + decorator_list: vec![], + returns: None, + type_comment: None, + config_comment: vec![], + }, + }), + ]); + "build_constructor_lookup_table" +)] +fn test_build_constructor_lookup(source: &str, result: HashMap>) { + let ast = parse_program(source, Default::default()); + let mut composer: TopLevelComposer = Default::default(); + if let Ok(stmts) = ast { + composer.build_constructor_lookup(stmts.iter()) + } + + assert_eq!(result.get(&"A".into()).unwrap(), result.get(&"B".into()).unwrap()); + assert_eq!(result.get(&"B".into()).unwrap(), result.get(&"C".into()).unwrap()); +} + + #[test_case( vec![ indoc! {"