From e3ec52970ef45d695fbd2849961f78f32cc005d6 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 9 May 2024 00:34:31 +0200 Subject: [PATCH] add unit test for DebugInfo::extract --- Cargo.lock | 13 ++++++++ Cargo.toml | 1 + src/debug_info.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index f9bb897e6..19cc482c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -647,6 +647,7 @@ dependencies = [ "cairo-lang-plugins", "cairo-lang-proc-macros", "cairo-lang-syntax", + "cairo-lang-test-utils", "cairo-lang-utils", "id-arena", "indoc", @@ -859,6 +860,18 @@ dependencies = [ "serde", ] +[[package]] +name = "cairo-lang-test-utils" +version = "2.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ab221aa0119b6e613992127687a1352a896d30a2e55a9295c52eb9598bcc78" +dependencies = [ + "cairo-lang-utils", + "colored", + "log", + "pretty_assertions", +] + [[package]] name = "cairo-lang-utils" version = "2.5.4" diff --git a/Cargo.toml b/Cargo.toml index ba15522a2..972a13b2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,6 +94,7 @@ proptest = "1.4" test-case = "3.3" walkdir = "2" serde_json = { version = "1.0" } +cairo-lang-semantic = { version = "2.5.4", features = ["testing"] } [build-dependencies] cc = "1.0.92" diff --git a/src/debug_info.rs b/src/debug_info.rs index 327bb8b43..4ccd0d377 100644 --- a/src/debug_info.rs +++ b/src/debug_info.rs @@ -156,3 +156,87 @@ fn extract_location_from_stable_loc<'c>( Location::new(context, &path.to_string_lossy(), pos.line, pos.col) } + +#[cfg(test)] +mod test { + use super::*; + use cairo_lang_semantic::test_utils::setup_test_function; + use cairo_lang_sierra::program::ConcreteLibfuncLongId; + use cairo_lang_sierra::program::ConcreteTypeLongId; + use cairo_lang_sierra::program::FunctionSignature; + use cairo_lang_sierra::program::GenFunction; + use cairo_lang_sierra::program::LibfuncDeclaration; + use cairo_lang_sierra::program::StatementIdx; + use cairo_lang_sierra::program::TypeDeclaration; + use cairo_lang_sierra_generator::db::SierraGenGroup; + + #[test] + fn test_extract_debug_locations() { + // Build the root database with corelib detection + let db = RootDatabase::builder().detect_corelib().build().unwrap(); + + // Setup a test function using the `setup_test_function` utility + let test_function = setup_test_function(&db, "fn foo(a: felt252) {}", "foo", "").unwrap(); + let function_id = cairo_lang_lowering::ids::ConcreteFunctionWithBodyId::from_semantic( + &db, + test_function.concrete_function_id, + ); + let _ = db.function_with_body_sierra(function_id); + + // Define a dummy program for testing + let program = Program { + type_declarations: vec![TypeDeclaration { + id: "test_id_type_declarations".into(), + long_id: ConcreteTypeLongId { + generic_id: "u128".into(), + generic_args: vec![], + }, + declared_type_info: None, + }], + libfunc_declarations: vec![LibfuncDeclaration { + id: "test_id_libfunc_declarations".into(), + long_id: ConcreteLibfuncLongId { + generic_id: "u128_sqrt".into(), + generic_args: vec![], + }, + }], + statements: vec![], + funcs: vec![GenFunction { + id: FunctionId { + id: 0, + debug_name: Some("some_name".into()), + }, + signature: FunctionSignature { + ret_types: vec![], + param_types: vec![], + }, + params: vec![], + entry_point: StatementIdx(0), + }], + }; + + // Extract debug information from the program + let res = DebugInfo::extract(&db, &program).unwrap(); + + // Assertions to test the extracted debug information + assert!(res.type_declarations.len() == 1); + assert!(res + .type_declarations + .contains_key(&ConcreteTypeId::from_string("test_id_type_declarations"))); + + assert!(res.libfunc_declarations.len() == 1); + assert!(res + .libfunc_declarations + .contains_key(&ConcreteLibfuncId::from_string( + "test_id_libfunc_declarations" + ))); + + assert!(res.statements.is_empty()); + + assert!(res.funcs.len() == 1); + assert!(res.funcs.contains_key(&FunctionId { + id: 0, + debug_name: Some("some_name".into()), + })); + } +}