From ced65041c1c92b537385ece57b1bc995e1261fa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sm=C3=B3=C5=82ka?= Date: Thu, 5 Dec 2024 18:29:14 +0100 Subject: [PATCH] Redesigned `semantic` testing utilities commit-id:46150109 --- crates/cairo-lang-compiler/src/test.rs | 2 +- crates/cairo-lang-executable/src/test.rs | 10 +- .../src/borrow_check/test.rs | 6 +- crates/cairo-lang-lowering/src/inline/test.rs | 6 +- .../src/lower/generated_test.rs | 6 +- .../optimizations/branch_inversion_test.rs | 6 +- .../src/optimizations/cancel_ops_test.rs | 6 +- .../src/optimizations/const_folding_test.rs | 6 +- .../src/optimizations/gas_redeposit_test.rs | 6 +- .../src/optimizations/match_optimizer_test.rs | 6 +- .../optimizations/reorder_statements_test.rs | 6 +- .../optimizations/return_optimization_test.rs | 6 +- .../src/optimizations/scrub_units_test.rs | 6 +- .../src/optimizations/split_structs_test.rs | 6 +- crates/cairo-lang-lowering/src/test.rs | 14 +- .../cairo-lang-runner/src/profiling_test.rs | 5 +- .../src/diagnostic_test.rs | 25 +- crates/cairo-lang-semantic/src/expr/test.rs | 40 +- .../cairo-lang-semantic/src/items/enm_test.rs | 11 +- .../src/items/extern_function_test.rs | 11 +- .../src/items/extern_type_test.rs | 11 +- .../src/items/free_function_test.rs | 11 +- .../cairo-lang-semantic/src/items/imp_test.rs | 11 +- .../src/items/structure_test.rs | 11 +- .../cairo-lang-semantic/src/items/trt_test.rs | 11 +- .../cairo-lang-semantic/src/resolve/test.rs | 20 +- crates/cairo-lang-semantic/src/test.rs | 11 +- crates/cairo-lang-semantic/src/test_utils.rs | 386 ++++++++++++------ crates/cairo-lang-semantic/src/usage/test.rs | 6 +- .../src/ap_change_test.rs | 6 +- .../src/block_generator_test.rs | 6 +- .../src/function_generator_test_utils.rs | 6 +- .../src/lifetime_test.rs | 6 +- .../src/local_variables_test.rs | 6 +- .../src/statements_locations_test.rs | 6 +- .../src/test_utils.rs | 4 +- crates/cairo-lang-starknet/src/abi_test.rs | 8 +- .../cairo-lang-starknet/src/contract_test.rs | 8 +- crates/cairo-lang-starknet/src/plugin/test.rs | 6 +- tests/e2e_test.rs | 6 +- 40 files changed, 499 insertions(+), 237 deletions(-) diff --git a/crates/cairo-lang-compiler/src/test.rs b/crates/cairo-lang-compiler/src/test.rs index a5c57961b40..5524bc0aa9c 100644 --- a/crates/cairo-lang-compiler/src/test.rs +++ b/crates/cairo-lang-compiler/src/test.rs @@ -46,7 +46,7 @@ fn can_collect_executables() { suite.add_plugin::(); let mut db = RootDatabase::builder().detect_corelib().build().unwrap(); db.set_plugins_from_suite(suite); - let crate_id = setup_test_crate(&db, content); + let crate_id = setup_test_crate(&db, content, None); let config = CompilerConfig { replace_ids: true, ..CompilerConfig::default() }; let artefact = compile_prepared_db_program_artifact(&mut db, vec![crate_id], config).unwrap(); let executables = artefact.debug_info.unwrap().executables; diff --git a/crates/cairo-lang-executable/src/test.rs b/crates/cairo-lang-executable/src/test.rs index f84958f351a..df215a66f28 100644 --- a/crates/cairo-lang-executable/src/test.rs +++ b/crates/cairo-lang-executable/src/test.rs @@ -5,7 +5,7 @@ use cairo_lang_compiler::diagnostics::DiagnosticsReporter; use cairo_lang_plugins::test_utils::expand_module_text; use cairo_lang_semantic::db::PluginSuiteInput; use cairo_lang_semantic::inline_macros::get_default_plugin_suite; -use cairo_lang_semantic::test_utils::setup_test_module; +use cairo_lang_semantic::test_utils::TestModule; use cairo_lang_test_utils::parse_test_file::{TestFileRunner, TestRunnerResult}; use cairo_lang_test_utils::{get_direct_or_file_content, verify_diagnostics_expectation}; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -32,7 +32,9 @@ impl TestFileRunner for ExpandExecutableTestRunner { ) -> TestRunnerResult { let db = SHARED_DB.lock().unwrap().snapshot(); let (_, cairo_code) = get_direct_or_file_content(&inputs["cairo_code"]); - let (test_module, semantic_diagnostics) = setup_test_module(&db, &cairo_code).split(); + let (test_module, semantic_diagnostics) = TestModule::builder(&db, &cairo_code, None) + .build_and_check_for_diagnostics(&db) + .split(); let result = expand_module_text(&db, test_module.module_id, &mut vec![]); let error = verify_diagnostics_expectation(args, &semantic_diagnostics); TestRunnerResult { @@ -66,7 +68,9 @@ impl TestFileRunner for CompileExecutableTestRunner { ) -> TestRunnerResult { let db = SHARED_DB.lock().unwrap().snapshot(); let (_, cairo_code) = get_direct_or_file_content(&inputs["cairo_code"]); - let (test_module, semantic_diagnostics) = setup_test_module(&db, &cairo_code).split(); + let (test_module, semantic_diagnostics) = TestModule::builder(&db, &cairo_code, None) + .build_and_check_for_diagnostics(&db) + .split(); let result = compile::compile_executable_in_prepared_db( &db, None, diff --git a/crates/cairo-lang-lowering/src/borrow_check/test.rs b/crates/cairo-lang-lowering/src/borrow_check/test.rs index 75495ba5c97..67c2f2d6392 100644 --- a/crates/cairo-lang-lowering/src/borrow_check/test.rs +++ b/crates/cairo-lang-lowering/src/borrow_check/test.rs @@ -1,5 +1,5 @@ use cairo_lang_debug::DebugWithDb; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -23,12 +23,14 @@ fn test_borrow_check( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); diff --git a/crates/cairo-lang-lowering/src/inline/test.rs b/crates/cairo-lang-lowering/src/inline/test.rs index 3bf94818bac..2ceaef5b3de 100644 --- a/crates/cairo-lang-lowering/src/inline/test.rs +++ b/crates/cairo-lang-lowering/src/inline/test.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use cairo_lang_debug::DebugWithDb; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -26,12 +26,14 @@ fn test_function_inlining( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); diff --git a/crates/cairo-lang-lowering/src/lower/generated_test.rs b/crates/cairo-lang-lowering/src/lower/generated_test.rs index 207a1608f58..fe24f0f6bb4 100644 --- a/crates/cairo-lang-lowering/src/lower/generated_test.rs +++ b/crates/cairo-lang-lowering/src/lower/generated_test.rs @@ -4,7 +4,7 @@ use cairo_lang_debug::DebugWithDb; use cairo_lang_defs::ids::TopLevelLanguageElementId; use cairo_lang_diagnostics::get_location_marks; use cairo_lang_semantic::items::functions::GenericFunctionId; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; use cairo_lang_utils::{Intern, LookupIntern, extract_matches}; @@ -31,12 +31,14 @@ fn test_generated_function( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let mut writer = String::new(); diff --git a/crates/cairo-lang-lowering/src/optimizations/branch_inversion_test.rs b/crates/cairo-lang-lowering/src/optimizations/branch_inversion_test.rs index 8a2e3a7221b..73ef5a2b19d 100644 --- a/crates/cairo-lang-lowering/src/optimizations/branch_inversion_test.rs +++ b/crates/cairo-lang-lowering/src/optimizations/branch_inversion_test.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use cairo_lang_debug::DebugWithDb; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -28,12 +28,14 @@ fn test_branch_inversion( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); diff --git a/crates/cairo-lang-lowering/src/optimizations/cancel_ops_test.rs b/crates/cairo-lang-lowering/src/optimizations/cancel_ops_test.rs index b1536a3d4bb..45a6d4c59d9 100644 --- a/crates/cairo-lang-lowering/src/optimizations/cancel_ops_test.rs +++ b/crates/cairo-lang-lowering/src/optimizations/cancel_ops_test.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use cairo_lang_debug::DebugWithDb; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -29,12 +29,14 @@ fn test_cancel_ops( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); diff --git a/crates/cairo-lang-lowering/src/optimizations/const_folding_test.rs b/crates/cairo-lang-lowering/src/optimizations/const_folding_test.rs index bcc2030a657..1f6a28eb2c0 100644 --- a/crates/cairo-lang-lowering/src/optimizations/const_folding_test.rs +++ b/crates/cairo-lang-lowering/src/optimizations/const_folding_test.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use cairo_lang_debug::DebugWithDb; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -29,12 +29,14 @@ fn test_match_optimizer( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); diff --git a/crates/cairo-lang-lowering/src/optimizations/gas_redeposit_test.rs b/crates/cairo-lang-lowering/src/optimizations/gas_redeposit_test.rs index e6e07d9bf83..0fd1a9fc73f 100644 --- a/crates/cairo-lang-lowering/src/optimizations/gas_redeposit_test.rs +++ b/crates/cairo-lang-lowering/src/optimizations/gas_redeposit_test.rs @@ -5,7 +5,7 @@ use cairo_lang_debug::DebugWithDb; use cairo_lang_filesystem::db::FilesGroupEx; use cairo_lang_filesystem::flag::Flag; use cairo_lang_filesystem::ids::FlagId; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::{TestFileRunner, TestRunnerResult}; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -43,12 +43,14 @@ impl TestFileRunner for GetRedepositTestRunner { _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &self.db; - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); diff --git a/crates/cairo-lang-lowering/src/optimizations/match_optimizer_test.rs b/crates/cairo-lang-lowering/src/optimizations/match_optimizer_test.rs index 1ac08eb4500..0eb6ea14821 100644 --- a/crates/cairo-lang-lowering/src/optimizations/match_optimizer_test.rs +++ b/crates/cairo-lang-lowering/src/optimizations/match_optimizer_test.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use cairo_lang_debug::DebugWithDb; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -31,12 +31,14 @@ fn test_match_optimizer( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); diff --git a/crates/cairo-lang-lowering/src/optimizations/reorder_statements_test.rs b/crates/cairo-lang-lowering/src/optimizations/reorder_statements_test.rs index e05e4591183..bb7a694a0b2 100644 --- a/crates/cairo-lang-lowering/src/optimizations/reorder_statements_test.rs +++ b/crates/cairo-lang-lowering/src/optimizations/reorder_statements_test.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use cairo_lang_debug::DebugWithDb; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -28,12 +28,14 @@ fn test_reorder_statements( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); diff --git a/crates/cairo-lang-lowering/src/optimizations/return_optimization_test.rs b/crates/cairo-lang-lowering/src/optimizations/return_optimization_test.rs index 96092f0455e..1929da9a642 100644 --- a/crates/cairo-lang-lowering/src/optimizations/return_optimization_test.rs +++ b/crates/cairo-lang-lowering/src/optimizations/return_optimization_test.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use cairo_lang_debug::DebugWithDb; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -30,12 +30,14 @@ fn test_return_optimizer( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); diff --git a/crates/cairo-lang-lowering/src/optimizations/scrub_units_test.rs b/crates/cairo-lang-lowering/src/optimizations/scrub_units_test.rs index fe34a44af02..28859b38b36 100644 --- a/crates/cairo-lang-lowering/src/optimizations/scrub_units_test.rs +++ b/crates/cairo-lang-lowering/src/optimizations/scrub_units_test.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use cairo_lang_debug::DebugWithDb; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -26,12 +26,14 @@ fn test_scrub_units( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = diff --git a/crates/cairo-lang-lowering/src/optimizations/split_structs_test.rs b/crates/cairo-lang-lowering/src/optimizations/split_structs_test.rs index cea7760548b..9f2009c3c16 100644 --- a/crates/cairo-lang-lowering/src/optimizations/split_structs_test.rs +++ b/crates/cairo-lang-lowering/src/optimizations/split_structs_test.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use cairo_lang_debug::DebugWithDb; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -29,12 +29,14 @@ fn test_split_structs( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); diff --git a/crates/cairo-lang-lowering/src/test.rs b/crates/cairo-lang-lowering/src/test.rs index 7eaab96181f..03189191638 100644 --- a/crates/cairo-lang-lowering/src/test.rs +++ b/crates/cairo-lang-lowering/src/test.rs @@ -7,7 +7,7 @@ use cairo_lang_defs::ids::LanguageElementId; use cairo_lang_diagnostics::{DiagnosticNote, DiagnosticsBuilder}; use cairo_lang_semantic as semantic; use cairo_lang_semantic::db::SemanticGroup; -use cairo_lang_semantic::test_utils::{setup_test_expr, setup_test_function}; +use cairo_lang_semantic::test_utils::{TestExpr, TestFunction, TestModule}; use cairo_lang_syntax::node::{Terminal, TypedStablePtr}; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_test_utils::verify_diagnostics_expectation; @@ -15,7 +15,6 @@ use cairo_lang_utils::ordered_hash_map::OrderedHashMap; use cairo_lang_utils::{LookupIntern, Upcast, extract_matches}; use itertools::Itertools; use pretty_assertions::assert_eq; -use semantic::test_utils::setup_test_module_ex; use crate::FlatLowered; use crate::db::LoweringGroup; @@ -66,12 +65,14 @@ fn test_function_lowering( args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut LoweringDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let function_id = ConcreteFunctionWithBodyId::from_semantic(db, test_function.concrete_function_id); @@ -111,7 +112,9 @@ fn formatted_lowered(db: &dyn LoweringGroup, lowered: &FlatLowered) -> String { fn test_location_and_diagnostics() { let db = &mut LoweringDatabaseForTesting::default(); - let test_expr = setup_test_expr(db, "a = a * 3", "", "let mut a = 5;", None).unwrap(); + let test_expr = TestExpr::builder(db, "a = a * 3", "", "let mut a = 5;", None) + .build_and_check_for_diagnostics(db) + .unwrap(); let function_body = db.function_body(test_expr.function_id).unwrap(); @@ -192,7 +195,7 @@ fn test_sizes() { ("core::cmp::min::::Coupon", 0), ]; - let test_module = setup_test_module_ex( + let test_module = TestModule::builder( db, &type_to_size .iter() @@ -201,6 +204,7 @@ fn test_sizes() { .join(""), None, ) + .build_and_check_for_diagnostics(db) .unwrap(); let db: &LoweringDatabaseForTesting = db; let type_aliases = db.module_type_aliases(test_module.module_id).unwrap(); diff --git a/crates/cairo-lang-runner/src/profiling_test.rs b/crates/cairo-lang-runner/src/profiling_test.rs index 03d55954ba4..4f5790d9703 100644 --- a/crates/cairo-lang-runner/src/profiling_test.rs +++ b/crates/cairo-lang-runner/src/profiling_test.rs @@ -4,7 +4,7 @@ use cairo_lang_compiler::db::RootDatabase; use cairo_lang_compiler::diagnostics::DiagnosticsReporter; use cairo_lang_semantic::db::PluginSuiteInput; use cairo_lang_semantic::inline_macros::get_default_plugin_suite; -use cairo_lang_semantic::test_utils::setup_test_module; +use cairo_lang_semantic::test_utils::TestModule; use cairo_lang_sierra_generator::db::SierraGenGroup; use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug; use cairo_lang_sierra_generator::replace_ids::replace_sierra_ids_in_program; @@ -47,7 +47,8 @@ pub fn test_profiling( db.set_plugins_from_suite(get_default_plugin_suite() + starknet_plugin_suite()); let (_path, cairo_code) = get_direct_or_file_content(&inputs["cairo_code"]); - let test_module = setup_test_module(&db, &cairo_code).unwrap(); + let test_module = + TestModule::builder(&db, &cairo_code, None).build_and_check_for_diagnostics(&db).unwrap(); DiagnosticsReporter::stderr() .with_crates(&[test_module.crate_id]) .allow_warnings() diff --git a/crates/cairo-lang-semantic/src/diagnostic_test.rs b/crates/cairo-lang-semantic/src/diagnostic_test.rs index ddf662032ac..5167101b62b 100644 --- a/crates/cairo-lang-semantic/src/diagnostic_test.rs +++ b/crates/cairo-lang-semantic/src/diagnostic_test.rs @@ -46,6 +46,7 @@ fn test_missing_module_file() { mod a { mod abc; }", + None, ); let submodule_id = @@ -140,14 +141,18 @@ fn test_inline_module_diagnostics() { let mut db_val = SemanticDatabaseForTesting::new_empty(); let db = &mut db_val; db.set_macro_plugins(vec![Arc::new(AddInlineModuleDummyPlugin)]); - let crate_id = setup_test_crate(db, indoc! {" + let crate_id = setup_test_crate( + db, + indoc! {" mod a { #[test_change_return_type] fn bad() -> u128 { return 5_felt252; } } - "}); + "}, + None, + ); // Verify we get diagnostics both for the original and the generated code. assert_eq!(get_crate_semantic_diagnostics(db, crate_id).format(db), indoc! {r#" @@ -168,7 +173,9 @@ fn test_inline_module_diagnostics() { fn test_inline_inline_module_diagnostics() { let db_val = SemanticDatabaseForTesting::default(); let db = &db_val; - let crate_id = setup_test_crate(db, indoc! {" + let crate_id = setup_test_crate( + db, + indoc! {" mod a { fn bad_a() -> u128 { return 1_felt252; @@ -188,7 +195,9 @@ fn test_inline_inline_module_diagnostics() { fn foo() { b::c::bad_c(); } - "}); + "}, + None, + ); assert_eq!( get_crate_semantic_diagnostics(db, crate_id).format(db), @@ -240,7 +249,9 @@ fn test_analyzer_diagnostics() { let mut db_val = SemanticDatabaseForTesting::new_empty(); let db = &mut db_val; db.set_analyzer_plugins(vec![Arc::new(NoU128RenameAnalyzerPlugin)]); - let crate_id = setup_test_crate(db, indoc! {" + let crate_id = setup_test_crate( + db, + indoc! {" mod inner { use core::integer::u128 as long_u128_rename; use u128 as short_u128_rename; @@ -254,7 +265,9 @@ fn test_analyzer_diagnostics() { use core::integer::u64 as long_u64_rename; use u64 as short_u64_rename; use inner::long_u64_rename as additional_u64_rename; - "}); + "}, + None, + ); assert_eq!(get_crate_semantic_diagnostics(db, crate_id).format(db), indoc! {r#" error: Plugin diagnostic: Use items for u128 disallowed. diff --git a/crates/cairo-lang-semantic/src/expr/test.rs b/crates/cairo-lang-semantic/src/expr/test.rs index 0d66e58917b..0ae583a8944 100644 --- a/crates/cairo-lang-semantic/src/expr/test.rs +++ b/crates/cairo-lang-semantic/src/expr/test.rs @@ -11,7 +11,7 @@ use crate::db::SemanticGroup; use crate::expr::fmt::ExprFormatter; use crate::semantic; use crate::test_utils::{ - SemanticDatabaseForTesting, setup_test_expr, setup_test_function, test_function_diagnostics, + SemanticDatabaseForTesting, TestExpr, TestFunction, test_function_diagnostics, }; cairo_lang_test_utils::test_file_test!( @@ -96,13 +96,14 @@ fn test_expand_expr( args: &OrderedHashMap, ) -> TestRunnerResult { let db = &SemanticDatabaseForTesting::default(); - let (test_expr, diagnostics) = setup_test_expr( + let (test_expr, diagnostics) = TestExpr::builder( db, inputs["expr_code"].as_str(), inputs.get("module_code").map(|s| s.as_str()).unwrap_or(""), inputs.get("function_body").map(|s| s.as_str()).unwrap_or(""), inputs.get("crate_settings").map(|x| x.as_str()), ) + .build_and_check_for_diagnostics(db) .split(); let expr = db.expr_semantic(test_expr.function_id, test_expr.expr_id); @@ -125,13 +126,14 @@ fn test_expr_semantics( args: &OrderedHashMap, ) -> TestRunnerResult { let db = &SemanticDatabaseForTesting::default(); - let (test_expr, diagnostics) = setup_test_expr( + let (test_expr, diagnostics) = TestExpr::builder( db, inputs["expr_code"].as_str(), inputs.get("module_code").map(|s| s.as_str()).unwrap_or(""), inputs.get("function_body").map(|s| s.as_str()).unwrap_or(""), inputs.get("crate_settings").map(|x| x.as_str()), ) + .build_and_check_for_diagnostics(db) .split(); let expr = db.expr_semantic(test_expr.function_id, test_expr.expr_id); let expr_formatter = ExprFormatter { db, function_id: test_expr.function_id }; @@ -149,7 +151,9 @@ fn test_expr_semantics( #[test] fn test_function_with_param() { let db_val = SemanticDatabaseForTesting::default(); - let test_function = setup_test_function(&db_val, "fn foo(a: felt252) {}", "foo", "").unwrap(); + let test_function = TestFunction::builder(&db_val, "fn foo(a: felt252) {}", "foo", "", None) + .build_and_check_for_diagnostics(&db_val) + .unwrap(); let _db = &db_val; let signature = test_function.signature; @@ -162,9 +166,15 @@ fn test_function_with_param() { #[test] fn test_tuple_type() { let db_val = SemanticDatabaseForTesting::default(); - let test_function = - setup_test_function(&db_val, "fn foo(mut a: (felt252, (), (felt252,))) {}", "foo", "") - .unwrap(); + let test_function = TestFunction::builder( + &db_val, + "fn foo(mut a: (felt252, (), (felt252,))) {}", + "foo", + "", + None, + ) + .build_and_check_for_diagnostics(&db_val) + .unwrap(); let db = &db_val; let signature = test_function.signature; @@ -181,7 +191,9 @@ fn test_tuple_type() { fn test_function_with_return_type() { let db_val = SemanticDatabaseForTesting::default(); let test_function = - setup_test_function(&db_val, "fn foo() -> felt252 { 5 }", "foo", "").unwrap(); + TestFunction::builder(&db_val, "fn foo() -> felt252 { 5 }", "foo", "", None) + .build_and_check_for_diagnostics(&db_val) + .unwrap(); let _db = &db_val; let signature = test_function.signature; @@ -192,7 +204,7 @@ fn test_function_with_return_type() { #[test] fn test_expr_var() { let db_val = SemanticDatabaseForTesting::default(); - let test_function = setup_test_function( + let test_function = TestFunction::builder( &db_val, indoc! {" fn foo(a: felt252) -> felt252 { @@ -201,7 +213,9 @@ fn test_expr_var() { "}, "foo", "", + None, ) + .build_and_check_for_diagnostics(&db_val) .unwrap(); let db = &db_val; @@ -223,7 +237,9 @@ fn test_expr_var() { fn test_expr_call_failures() { let db_val = SemanticDatabaseForTesting::default(); // TODO(spapini): Add types. - let (test_expr, diagnostics) = setup_test_expr(&db_val, "foo()", "", "", None).split(); + let (test_expr, diagnostics) = TestExpr::builder(&db_val, "foo()", "", "", None) + .build_and_check_for_diagnostics(&db_val) + .split(); let db = &db_val; let expr_formatter = ExprFormatter { db, function_id: test_expr.function_id }; @@ -248,7 +264,7 @@ fn test_expr_call_failures() { #[test] fn test_function_body() { let db_val = SemanticDatabaseForTesting::default(); - let test_function = setup_test_function( + let test_function = TestFunction::builder( &db_val, indoc! {" fn foo(a: felt252) { @@ -257,7 +273,9 @@ fn test_function_body() { "}, "foo", "", + None, ) + .build_and_check_for_diagnostics(&db_val) .unwrap(); let db = &db_val; let item_id = db.module_item_by_name(test_function.module_id, "foo".into()).unwrap().unwrap(); diff --git a/crates/cairo-lang-semantic/src/items/enm_test.rs b/crates/cairo-lang-semantic/src/items/enm_test.rs index 31811877191..64aefabad61 100644 --- a/crates/cairo-lang-semantic/src/items/enm_test.rs +++ b/crates/cairo-lang-semantic/src/items/enm_test.rs @@ -6,13 +6,15 @@ use pretty_assertions::assert_eq; use test_log::test; use crate::db::SemanticGroup; -use crate::test_utils::{SemanticDatabaseForTesting, setup_test_module}; +use crate::test_utils::{SemanticDatabaseForTesting, TestModule}; #[test] fn test_enum() { let db_val = SemanticDatabaseForTesting::default(); let db = &db_val; - let (test_module, diagnostics) = setup_test_module(db, indoc::indoc! {" + let (test_module, diagnostics) = TestModule::builder( + db, + indoc::indoc! {" enum A { a: felt252, b: (felt252, felt252), @@ -24,7 +26,10 @@ fn test_enum() { fn foo(a: A) { 5; } - "}) + "}, + None, + ) + .build_and_check_for_diagnostics(db) .split(); assert_eq!(diagnostics, indoc! {r#" error: Redefinition of variant "a" on enum "test::A". diff --git a/crates/cairo-lang-semantic/src/items/extern_function_test.rs b/crates/cairo-lang-semantic/src/items/extern_function_test.rs index d8cfb1c553a..91eda310ae4 100644 --- a/crates/cairo-lang-semantic/src/items/extern_function_test.rs +++ b/crates/cairo-lang-semantic/src/items/extern_function_test.rs @@ -5,15 +5,20 @@ use pretty_assertions::assert_eq; use test_log::test; use crate::db::SemanticGroup; -use crate::test_utils::{SemanticDatabaseForTesting, setup_test_module}; +use crate::test_utils::{SemanticDatabaseForTesting, TestModule}; #[test] fn test_extern_function() { let db_val = SemanticDatabaseForTesting::default(); let db = &db_val; - let test_module = setup_test_module(db, indoc::indoc! {" + let test_module = TestModule::builder( + db, + indoc::indoc! {" extern fn foo() nopanic; - "}) + "}, + None, + ) + .build_and_check_for_diagnostics(db) .unwrap(); let module_id = test_module.module_id; diff --git a/crates/cairo-lang-semantic/src/items/extern_type_test.rs b/crates/cairo-lang-semantic/src/items/extern_type_test.rs index 4b9e358ae8e..1997be71d77 100644 --- a/crates/cairo-lang-semantic/src/items/extern_type_test.rs +++ b/crates/cairo-lang-semantic/src/items/extern_type_test.rs @@ -5,15 +5,20 @@ use pretty_assertions::assert_eq; use test_log::test; use crate::db::SemanticGroup; -use crate::test_utils::{SemanticDatabaseForTesting, setup_test_module}; +use crate::test_utils::{SemanticDatabaseForTesting, TestModule}; #[test] fn test_extern_type() { let db_val = SemanticDatabaseForTesting::default(); let db = &db_val; - let test_module = setup_test_module(db, indoc::indoc! {" + let test_module = TestModule::builder( + db, + indoc::indoc! {" extern type S; - "}) + "}, + None, + ) + .build_and_check_for_diagnostics(db) .unwrap(); let module_id = test_module.module_id; diff --git a/crates/cairo-lang-semantic/src/items/free_function_test.rs b/crates/cairo-lang-semantic/src/items/free_function_test.rs index 8d83601e5c5..ac6e653b3a8 100644 --- a/crates/cairo-lang-semantic/src/items/free_function_test.rs +++ b/crates/cairo-lang-semantic/src/items/free_function_test.rs @@ -7,13 +7,15 @@ use test_log::test; use crate::db::SemanticGroup; use crate::expr::fmt::ExprFormatter; use crate::items::function_with_body::SemanticExprLookup; -use crate::test_utils::{SemanticDatabaseForTesting, setup_test_module}; +use crate::test_utils::{SemanticDatabaseForTesting, TestModule}; #[test] fn test_expr_lookup() { let db_val = SemanticDatabaseForTesting::default(); let db = &db_val; - let test_module = setup_test_module(db, indoc::indoc! {" + let test_module = TestModule::builder( + db, + indoc::indoc! {" // `inline` is used just to have an allowed attribute. #[inline] fn foo(_a: felt252) -> felt252 { @@ -23,7 +25,10 @@ fn test_expr_lookup() { _ => {6} } } - "}) + "}, + None, + ) + .build_and_check_for_diagnostics(db) .unwrap(); let module_id = test_module.module_id; diff --git a/crates/cairo-lang-semantic/src/items/imp_test.rs b/crates/cairo-lang-semantic/src/items/imp_test.rs index 494165fe5f6..1ff97b76400 100644 --- a/crates/cairo-lang-semantic/src/items/imp_test.rs +++ b/crates/cairo-lang-semantic/src/items/imp_test.rs @@ -5,13 +5,15 @@ use pretty_assertions::assert_eq; use test_log::test; use crate::db::SemanticGroup; -use crate::test_utils::{SemanticDatabaseForTesting, setup_test_module}; +use crate::test_utils::{SemanticDatabaseForTesting, TestModule}; #[test] fn test_impl() { let db_val = SemanticDatabaseForTesting::default(); let db = &db_val; - let (test_module, diagnostics) = setup_test_module(db, indoc::indoc! {" + let (test_module, diagnostics) = TestModule::builder( + db, + indoc::indoc! {" trait IContract { fn foo(a: felt252); } @@ -20,7 +22,10 @@ fn test_impl() { fn foo(a: felt252) { } } - "}) + "}, + None, + ) + .build_and_check_for_diagnostics(db) .split(); assert!(diagnostics.is_empty()); diff --git a/crates/cairo-lang-semantic/src/items/structure_test.rs b/crates/cairo-lang-semantic/src/items/structure_test.rs index 668d0211e36..fa3d3c70f11 100644 --- a/crates/cairo-lang-semantic/src/items/structure_test.rs +++ b/crates/cairo-lang-semantic/src/items/structure_test.rs @@ -6,13 +6,15 @@ use pretty_assertions::assert_eq; use test_log::test; use crate::db::SemanticGroup; -use crate::test_utils::{SemanticDatabaseForTesting, setup_test_module}; +use crate::test_utils::{SemanticDatabaseForTesting, TestModule}; #[test] fn test_struct() { let db_val = SemanticDatabaseForTesting::default(); let db = &db_val; - let (test_module, diagnostics) = setup_test_module(db, indoc::indoc! {" + let (test_module, diagnostics) = TestModule::builder( + db, + indoc::indoc! {" #[inline(MyImpl1, MyImpl2)] struct A { a: felt252, @@ -25,7 +27,10 @@ fn test_struct() { fn foo(a: A) { 5; } - "}) + "}, + None, + ) + .build_and_check_for_diagnostics(db) .split(); assert_eq!(diagnostics, indoc! {r#" error: Redefinition of member "a" on struct "test::A". diff --git a/crates/cairo-lang-semantic/src/items/trt_test.rs b/crates/cairo-lang-semantic/src/items/trt_test.rs index fc9d5f0c486..dece84a8b5b 100644 --- a/crates/cairo-lang-semantic/src/items/trt_test.rs +++ b/crates/cairo-lang-semantic/src/items/trt_test.rs @@ -5,19 +5,24 @@ use pretty_assertions::assert_eq; use test_log::test; use crate::db::SemanticGroup; -use crate::test_utils::{SemanticDatabaseForTesting, setup_test_module}; +use crate::test_utils::{SemanticDatabaseForTesting, TestModule}; #[test] fn test_trait() { let db_val = SemanticDatabaseForTesting::default(); let db = &db_val; - let test_module = setup_test_module(db, indoc::indoc! {" + let test_module = TestModule::builder( + db, + indoc::indoc! {" // `inline` is used just to have an allowed attribute. #[inline] trait MyContract { fn foo(a: felt252); } - "}) + "}, + None, + ) + .build_and_check_for_diagnostics(db) .unwrap(); let trait_id = extract_matches!( diff --git a/crates/cairo-lang-semantic/src/resolve/test.rs b/crates/cairo-lang-semantic/src/resolve/test.rs index 687be4c84b8..2e00a4d7152 100644 --- a/crates/cairo-lang-semantic/src/resolve/test.rs +++ b/crates/cairo-lang-semantic/src/resolve/test.rs @@ -10,13 +10,15 @@ use test_log::test; use crate::db::SemanticGroup; use crate::expr::fmt::ExprFormatter; -use crate::test_utils::{SemanticDatabaseForTesting, setup_test_module}; +use crate::test_utils::{SemanticDatabaseForTesting, TestModule}; #[test] fn test_resolve_path() { let db_val = SemanticDatabaseForTesting::default(); let db = &db_val; - let test_module = setup_test_module(db, indoc! {" + let test_module = TestModule::builder( + db, + indoc! {" use core::Box; extern type S; extern fn bar(value: S::) -> S::<()> nopanic; @@ -25,7 +27,10 @@ fn test_resolve_path() { bar::<(felt252,Q)>(value); let _c = b; } - "}) + "}, + None, + ) + .build_and_check_for_diagnostics(db) .unwrap(); let module_id = test_module.module_id; @@ -97,7 +102,9 @@ fn test_resolve_path_super() { fn test_resolve_path_trait_impl() { let db_val = SemanticDatabaseForTesting::default(); let db = &db_val; - let test_module = setup_test_module(db, indoc! {" + let test_module = TestModule::builder( + db, + indoc! {" trait MyTrait { fn foo() -> felt252; } @@ -111,7 +118,10 @@ fn test_resolve_path_trait_impl() { fn main() -> felt252 { MyTrait::foo() + 1 } - "}) + "}, + None, + ) + .build_and_check_for_diagnostics(db) .unwrap(); let module_id = test_module.module_id; diff --git a/crates/cairo-lang-semantic/src/test.rs b/crates/cairo-lang-semantic/src/test.rs index 757e72a783e..9f40b4b0735 100644 --- a/crates/cairo-lang-semantic/src/test.rs +++ b/crates/cairo-lang-semantic/src/test.rs @@ -3,15 +3,20 @@ use cairo_lang_defs::ids::ModuleItemId; use indoc::indoc; use crate::db::SemanticGroup; -use crate::test_utils::{SemanticDatabaseForTesting, setup_test_module}; +use crate::test_utils::{SemanticDatabaseForTesting, TestModule}; #[test] fn test_resolve() { let db_val = SemanticDatabaseForTesting::default(); - let (test_module, _diagnostics) = setup_test_module(&db_val, indoc! {" + let (test_module, _diagnostics) = TestModule::builder( + &db_val, + indoc! {" fn foo() -> felt252 { 5 } extern fn felt252_add(a: felt252, b: felt252) -> felt252 nopanic; - "}) + "}, + None, + ) + .build_and_check_for_diagnostics(&db_val) .split(); let module_id = test_module.module_id; diff --git a/crates/cairo-lang-semantic/src/test_utils.rs b/crates/cairo-lang-semantic/src/test_utils.rs index 69097e35dab..6939833769f 100644 --- a/crates/cairo-lang-semantic/src/test_utils.rs +++ b/crates/cairo-lang-semantic/src/test_utils.rs @@ -114,14 +114,75 @@ impl WithStringDiagnostics { } } -/// Helper struct for the return value of [setup_test_module]. +/// A module with [`ModuleId`] being a root of a crate with [`CrateId`]. pub struct TestModule { pub crate_id: CrateId, pub module_id: ModuleId, } +impl TestModule { + /// Setups the build of the [`TestModule`]. Creates a one-module crate with `content` and + /// `crate_settings`. + pub fn builder( + db: &dyn SemanticGroup, + content: &str, + crate_settings: Option<&str>, + ) -> TestModuleBuilder { + TestModuleBuilder { crate_id: setup_test_crate(db, content, crate_settings) } + } +} + +/// A builder of [`TestModule`] performing additional syntactic and semantic verification +/// on the created module. +pub struct TestModuleBuilder { + crate_id: CrateId, +} + +impl TestModuleBuilder { + /// Checks the [`CrateId`] **before** verifying the code in the test module in terms of syntax + /// and semantics. + /// + /// # Safety + /// Use only when lookup of the test crate's ID is required before verifying its contents. + /// Do not use the [`CrateId`] to perform any tests on it before calling + /// `build_and_check_for_diagnostics`. + pub unsafe fn get_crate_id(&self) -> CrateId { + self.crate_id + } + + /// Checks the [`ModuleId`] **before** verifying the code in the test module in terms of syntax + /// and semantics. + /// + /// # Safety + /// Use only when lookup of the test module's ID is required before verifying its contents. + /// Do not use the [`ModuleId`] to perform any tests on it before calling + /// `build_and_check_for_diagnostics`. + pub unsafe fn get_module_id(&self) -> ModuleId { + ModuleId::CrateRoot(self.crate_id) + } + + /// Constructs the final [`TestModule`] and verifies it by computing syntactic and semantic + /// diagnostics. + pub fn build_and_check_for_diagnostics( + self, + db: &(dyn SemanticGroup + 'static), + ) -> WithStringDiagnostics { + let module_id = ModuleId::CrateRoot(self.crate_id); + let file_id = db.module_main_file(module_id).unwrap(); + + let syntax_diagnostics = db.file_syntax_diagnostics(file_id).format(db.upcast()); + let semantic_diagnostics = + get_recursive_module_semantic_diagnostics(db, module_id).format(db); + + WithStringDiagnostics { + value: TestModule { crate_id: module_id.owning_crate(db.upcast()), module_id }, + diagnostics: format!("{syntax_diagnostics}{semantic_diagnostics}"), + } + } +} + /// Sets up a crate with given content, and returns its crate id. -pub fn setup_test_crate_ex( +pub fn setup_test_crate( db: &dyn SemanticGroup, content: &str, crate_settings: Option<&str>, @@ -160,39 +221,8 @@ pub fn setup_test_crate_ex( .intern(db) } -/// See [setup_test_crate_ex]. -pub fn setup_test_crate(db: &dyn SemanticGroup, content: &str) -> CrateId { - setup_test_crate_ex(db, content, None) -} - -/// Sets up a module with given content, and returns its module id. -pub fn setup_test_module_ex( - db: &(dyn SemanticGroup + 'static), - content: &str, - crate_settings: Option<&str>, -) -> WithStringDiagnostics { - let crate_id = setup_test_crate_ex(db, content, crate_settings); - let module_id = ModuleId::CrateRoot(crate_id); - let file_id = db.module_main_file(module_id).unwrap(); - - let syntax_diagnostics = db.file_syntax_diagnostics(file_id).format(Upcast::upcast(db)); - let semantic_diagnostics = get_recursive_module_semantic_diagnostics(db, module_id).format(db); - - WithStringDiagnostics { - value: TestModule { crate_id, module_id }, - diagnostics: format!("{syntax_diagnostics}{semantic_diagnostics}"), - } -} - -/// See [setup_test_module_ex]. -pub fn setup_test_module( - db: &(dyn SemanticGroup + 'static), - content: &str, -) -> WithStringDiagnostics { - setup_test_module_ex(db, content, None) -} - -/// Helper struct for the return value of [setup_test_function]. +/// A single function with [`FunctionWithBodyId`] and body with [`semantic::ExprId`], +/// defined inside a module with [`ModuleId`]. pub struct TestFunction { pub module_id: ModuleId, pub function_id: FunctionWithBodyId, @@ -201,56 +231,96 @@ pub struct TestFunction { pub body: semantic::ExprId, } -/// Returns the semantic model of a given function. -/// function_name - name of the function. -/// module_code - extra setup code in the module context. -pub fn setup_test_function_ex( - db: &(dyn SemanticGroup + 'static), - function_code: &str, - function_name: &str, - module_code: &str, - crate_settings: Option<&str>, -) -> WithStringDiagnostics { - let content = if module_code.is_empty() { - function_code.to_string() - } else { - format!("{module_code}\n{function_code}") - }; - let (test_module, diagnostics) = setup_test_module_ex(db, &content, crate_settings).split(); - let generic_function_id = db - .module_item_by_name(test_module.module_id, function_name.into()) - .expect("Failed to load module") - .and_then(GenericFunctionId::option_from) - .unwrap_or_else(|| panic!("Function '{function_name}' was not found.")); - let free_function_id = extract_matches!(generic_function_id, GenericFunctionId::Free); - let function_id = FunctionWithBodyId::Free(free_function_id); - WithStringDiagnostics { - value: TestFunction { - module_id: test_module.module_id, - function_id, - concrete_function_id: ConcreteFunctionWithBodyId::from_no_generics_free( - db, - free_function_id, - ) - .unwrap(), - signature: db.function_with_body_signature(function_id).unwrap(), - body: db.function_body_expr(function_id).unwrap(), - }, - diagnostics, +impl TestFunction { + /// Setups the build of the [`TestFunction`]. Creates a one-module crate with `crate_settings`, + /// containing a function with `function_name` and `function_code` body. + pub fn builder<'name>( + db: &dyn SemanticGroup, + function_code: &str, + function_name: &'name str, + module_code: &str, + crate_settings: Option<&str>, + ) -> TestFunctionBuilder<'name> { + let content = if module_code.is_empty() { + function_code.to_string() + } else { + format!("{module_code}\n{function_code}") + }; + + let module_builder = TestModule::builder(db, &content, crate_settings); + TestFunctionBuilder { function_name, module_builder } } } -/// See [setup_test_function_ex]. -pub fn setup_test_function( - db: &(dyn SemanticGroup + 'static), - function_code: &str, - function_name: &str, - module_code: &str, -) -> WithStringDiagnostics { - setup_test_function_ex(db, function_code, function_name, module_code, None) +/// A builder of [`TestFunction`] performing additional syntactic and semantic verification +/// on the created function. +pub struct TestFunctionBuilder<'name> { + function_name: &'name str, + module_builder: TestModuleBuilder, +} + +impl TestFunctionBuilder<'_> { + /// Checks the [`CrateId`] **before** verifying the function code in terms of syntax + /// and semantics. + /// + /// # Safety + /// Use only when lookup of the test crate's ID is required before verifying its contents. + /// Do not use the [`CrateId`] to perform any tests on it before calling + /// `build_and_check_for_diagnostics`. + pub unsafe fn get_crate_id(&self) -> CrateId { + self.module_builder.get_crate_id() + } + + /// Checks the [`ModuleId`] **before** verifying the function code in terms of syntax + /// and semantics. + /// + /// # Safety + /// Use only when lookup of the test module's ID is required before verifying its contents. + /// Do not use the [`ModuleId`] to perform any tests on it before calling + /// `build_and_check_for_diagnostics`. + pub unsafe fn get_module_id(&self) -> ModuleId { + self.module_builder.get_module_id() + } + + /// Constructs the final [`TestFunction`] and verifies it by computing syntactic and semantic + /// diagnostics. + pub fn build_and_check_for_diagnostics( + self, + db: &(dyn SemanticGroup + 'static), + ) -> WithStringDiagnostics { + let Self { function_name, module_builder } = self; + + let (test_module, diagnostics) = module_builder.build_and_check_for_diagnostics(db).split(); + let module_id = test_module.module_id; + + let generic_function_id = db + .module_item_by_name(module_id, function_name.into()) + .expect("Failed to load module") + .and_then(GenericFunctionId::option_from) + .unwrap_or_else(|| panic!("Function '{function_name}' was not found.")); + + let free_function_id = extract_matches!(generic_function_id, GenericFunctionId::Free); + let function_id = FunctionWithBodyId::Free(free_function_id); + + WithStringDiagnostics { + value: TestFunction { + module_id, + function_id, + concrete_function_id: ConcreteFunctionWithBodyId::from_no_generics_free( + db, + free_function_id, + ) + .unwrap(), + signature: db.function_with_body_signature(function_id).unwrap(), + body: db.function_body_expr(function_id).unwrap(), + }, + diagnostics, + } + } } -/// Helper struct for the return value of [setup_test_expr] and [setup_test_block]. +/// A single expression placed in the context of a function with [`FunctionWithBodyId`] defined +/// inside a module with [`ModuleId`]. pub struct TestExpr { pub module_id: ModuleId, pub function_id: FunctionWithBodyId, @@ -259,58 +329,108 @@ pub struct TestExpr { pub expr_id: semantic::ExprId, } -/// Returns the semantic model of a given expression. -/// module_code - extra setup code in the module context. -/// function_body - extra setup code in the function context. -pub fn setup_test_expr( - db: &(dyn SemanticGroup + 'static), - expr_code: &str, - module_code: &str, - function_body: &str, - crate_settings: Option<&str>, -) -> WithStringDiagnostics { - let function_code = format!("fn test_func() {{ {function_body} {{\n{expr_code}\n}}; }}"); - let (test_function, diagnostics) = - setup_test_function_ex(db, &function_code, "test_func", module_code, crate_settings) - .split(); - let semantic::ExprBlock { statements, .. } = extract_matches!( - db.expr_semantic(test_function.function_id, test_function.body), - semantic::Expr::Block - ); - let statement_expr = extract_matches!( - db.statement_semantic(test_function.function_id, *statements.last().unwrap()), - semantic::Statement::Expr - ); - let semantic::ExprBlock { statements, tail, .. } = extract_matches!( - db.expr_semantic(test_function.function_id, statement_expr.expr), - semantic::Expr::Block - ); - assert!( - statements.is_empty(), - "expr_code is not a valid expression. Consider using setup_test_block()." - ); - WithStringDiagnostics { - value: TestExpr { - module_id: test_function.module_id, - function_id: test_function.function_id, - signature: test_function.signature, - body: test_function.body, - expr_id: tail.unwrap(), - }, - diagnostics, +impl TestExpr { + /// Setups the build of the [`TestExpr`]. Creates a one-module crate with `module_code` code and + /// `crate_settings`, containing a function with `function_body` body ending with + /// `expr_code` being a single expression. + pub fn builder( + db: &dyn SemanticGroup, + expr_code: &str, + module_code: &str, + function_body: &str, + crate_settings: Option<&str>, + ) -> TestExprBuilder<'static> { + let function_code = format!("fn test_func() {{ {function_body} {{\n{expr_code}\n}}; }}"); + let function_builder = + TestFunction::builder(db, &function_code, "test_func", module_code, crate_settings); + + TestExprBuilder { function_builder } + } + + pub fn build_block( + db: &dyn SemanticGroup, + expr_code: &str, + module_code: &str, + function_body: &str, + crate_settings: Option<&str>, + ) -> TestExprBuilder<'static> { + let expr_code = &format!("{{ \n{expr_code}\n }}"); + Self::builder(db, expr_code, module_code, function_body, crate_settings) } } -/// Returns the semantic model of a given block expression. -/// module_code - extra setup code in the module context. -/// function_body - extra setup code in the function context. -pub fn setup_test_block( - db: &(dyn SemanticGroup + 'static), - expr_code: &str, - module_code: &str, - function_body: &str, -) -> WithStringDiagnostics { - setup_test_expr(db, &format!("{{ \n{expr_code}\n }}"), module_code, function_body, None) +/// A builder of [`TestExpr`] performing additional syntactic and semantic verification +/// on the created module. +pub struct TestExprBuilder<'source> { + function_builder: TestFunctionBuilder<'source>, +} + +impl TestExprBuilder<'_> { + /// Checks the [`CrateId`] **before** verifying the function code in terms of syntax + /// and semantics. + /// + /// # Safety + /// Use only when lookup of the test crate's ID is required before verifying its contents. + /// Do not use the [`CrateId`] to perform any tests on it before calling + /// `build_and_check_for_diagnostics`. + pub unsafe fn get_crate_id(&self) -> CrateId { + self.function_builder.get_crate_id() + } + + /// Checks the [`ModuleId`] **before** verifying the function code in terms of syntax + /// and semantics. + /// + /// # Safety + /// Use only when lookup of the test module's ID is required before verifying its contents. + /// Do not use the [`ModuleId`] to perform any tests on it before calling + /// `build_and_check_for_diagnostics`. + pub unsafe fn get_module_id(&self) -> ModuleId { + self.function_builder.get_module_id() + } + + /// Constructs the final [`TestExpr`] and verifies it by computing syntactic and semantic + /// diagnostics. + pub fn build_and_check_for_diagnostics( + self, + db: &(dyn SemanticGroup + 'static), + ) -> WithStringDiagnostics { + let Self { function_builder } = self; + + let (test_function, diagnostics) = + function_builder.build_and_check_for_diagnostics(db).split(); + + let function_id = test_function.function_id; + let function_body = test_function.body; + + let semantic::ExprBlock { statements, .. } = + extract_matches!(db.expr_semantic(function_id, function_body), semantic::Expr::Block); + + let statement_expr = extract_matches!( + db.statement_semantic(function_id, *statements.last().unwrap()), + semantic::Statement::Expr + ); + + let semantic::ExprBlock { statements, tail, .. } = extract_matches!( + db.expr_semantic(function_id, statement_expr.expr), + semantic::Expr::Block + ); + + assert!( + statements.is_empty(), + "expr_code is not a valid expression. Consider using setup_test_block()." + ); + + WithStringDiagnostics { + value: TestExpr { + module_id: test_function.module_id, + function_id, + signature: test_function.signature, + body: function_body, + expr_id: tail.unwrap(), + }, + diagnostics, + } + } } pub fn test_expr_diagnostics( @@ -319,14 +439,16 @@ pub fn test_expr_diagnostics( ) -> TestRunnerResult { let db = &SemanticDatabaseForTesting::default(); - let diagnostics = setup_test_expr( + let diagnostics = TestExpr::builder( db, inputs["expr_code"].as_str(), inputs["module_code"].as_str(), inputs["function_body"].as_str(), inputs.get("crate_settings").map(|x| x.as_str()), ) + .build_and_check_for_diagnostics(db) .get_diagnostics(); + let error = verify_diagnostics_expectation(args, &diagnostics); TestRunnerResult { @@ -341,14 +463,16 @@ pub fn test_function_diagnostics( ) -> TestRunnerResult { let db = &SemanticDatabaseForTesting::default(); - let diagnostics = setup_test_function_ex( + let diagnostics = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), inputs.get("crate_settings").map(|x| x.as_str()), ) + .build_and_check_for_diagnostics(db) .get_diagnostics(); + let error = verify_diagnostics_expectation(args, &diagnostics); TestRunnerResult { diff --git a/crates/cairo-lang-semantic/src/usage/test.rs b/crates/cairo-lang-semantic/src/usage/test.rs index 330b9b1e328..ed813f6c606 100644 --- a/crates/cairo-lang-semantic/src/usage/test.rs +++ b/crates/cairo-lang-semantic/src/usage/test.rs @@ -11,7 +11,7 @@ use super::Usages; use crate::Expr; use crate::db::SemanticGroup; use crate::expr::fmt::ExprFormatter; -use crate::test_utils::{SemanticDatabaseForTesting, setup_test_function}; +use crate::test_utils::{SemanticDatabaseForTesting, TestFunction}; cairo_lang_test_utils::test_file_test!( usage, @@ -27,12 +27,14 @@ fn test_function_usage( _args: &OrderedHashMap, ) -> TestRunnerResult { let db = &mut SemanticDatabaseForTesting::default(); - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); let file_id = db.module_file(test_function.function_id.module_file_id(db)).unwrap(); diff --git a/crates/cairo-lang-sierra-generator/src/ap_change_test.rs b/crates/cairo-lang-sierra-generator/src/ap_change_test.rs index 8e8944f339a..2f7221cda13 100644 --- a/crates/cairo-lang-sierra-generator/src/ap_change_test.rs +++ b/crates/cairo-lang-sierra-generator/src/ap_change_test.rs @@ -2,7 +2,7 @@ use cairo_lang_defs::db::DefsGroup; use cairo_lang_defs::ids::NamedLanguageElementId; use cairo_lang_lowering::db::LoweringGroup; use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId; -use cairo_lang_semantic::test_utils::setup_test_module; +use cairo_lang_semantic::test_utils::TestModule; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; use itertools::Itertools; @@ -23,7 +23,9 @@ fn contains_cycles_test( ) -> TestRunnerResult { let db = &SierraGenDatabaseForTesting::default(); // Parse code and create semantic model. - let test_module = setup_test_module(db, inputs["module_code"].as_str()).unwrap(); + let test_module = TestModule::builder(db, inputs["module_code"].as_str(), None) + .build_and_check_for_diagnostics(db) + .unwrap(); db.module_lowering_diagnostics(test_module.module_id) .unwrap() diff --git a/crates/cairo-lang-sierra-generator/src/block_generator_test.rs b/crates/cairo-lang-sierra-generator/src/block_generator_test.rs index 1b6a149fa9b..1d8710e94cb 100644 --- a/crates/cairo-lang-sierra-generator/src/block_generator_test.rs +++ b/crates/cairo-lang-sierra-generator/src/block_generator_test.rs @@ -6,7 +6,7 @@ use cairo_lang_filesystem::flag::Flag; use cairo_lang_filesystem::ids::FlagId; use cairo_lang_lowering as lowering; use cairo_lang_lowering::db::LoweringGroup; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::UpcastMut; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -47,12 +47,14 @@ fn block_generator_test( db.set_flag(add_withdraw_gas_flag_id, Some(Arc::new(Flag::AddWithdrawGas(false)))); // Parse code and create semantic model. - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); // Lower code. diff --git a/crates/cairo-lang-sierra-generator/src/function_generator_test_utils.rs b/crates/cairo-lang-sierra-generator/src/function_generator_test_utils.rs index 09c4e992c29..39a474e07ee 100644 --- a/crates/cairo-lang-sierra-generator/src/function_generator_test_utils.rs +++ b/crates/cairo-lang-sierra-generator/src/function_generator_test_utils.rs @@ -1,6 +1,6 @@ use cairo_lang_lowering::db::LoweringGroup; use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -18,12 +18,14 @@ pub fn test_function_generator( let db = &SierraGenDatabaseForTesting::without_add_withdraw_gas(); // Parse code and create semantic model. - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .split(); // Verify that there are no diagnostics. diff --git a/crates/cairo-lang-sierra-generator/src/lifetime_test.rs b/crates/cairo-lang-sierra-generator/src/lifetime_test.rs index 6243222ff15..ce805a96b1d 100644 --- a/crates/cairo-lang-sierra-generator/src/lifetime_test.rs +++ b/crates/cairo-lang-sierra-generator/src/lifetime_test.rs @@ -1,7 +1,7 @@ use cairo_lang_debug::DebugWithDb; use cairo_lang_lowering as lowering; use cairo_lang_lowering::db::LoweringGroup; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; use itertools::Itertools; @@ -37,12 +37,14 @@ fn check_variable_lifetime( let db = &SierraGenDatabaseForTesting::without_add_withdraw_gas(); // Parse code and create semantic model. - let test_function = setup_test_function( + let test_function = TestFunction::builder( db, inputs["function_code"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .unwrap(); db.module_lowering_diagnostics(test_function.module_id) diff --git a/crates/cairo-lang-sierra-generator/src/local_variables_test.rs b/crates/cairo-lang-sierra-generator/src/local_variables_test.rs index 325f3c921fc..9731ebdb63a 100644 --- a/crates/cairo-lang-sierra-generator/src/local_variables_test.rs +++ b/crates/cairo-lang-sierra-generator/src/local_variables_test.rs @@ -1,7 +1,7 @@ use cairo_lang_debug::DebugWithDb; use cairo_lang_lowering as lowering; use cairo_lang_lowering::db::LoweringGroup; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; use itertools::Itertools; @@ -36,12 +36,14 @@ fn check_find_local_variables( let db = &SierraGenDatabaseForTesting::without_add_withdraw_gas(); // Parse code and create semantic model. - let test_function = setup_test_function( + let test_function = TestFunction::builder( db, inputs["function_code"].as_str(), inputs["function_name"].as_str(), inputs["module_code"].as_str(), + None, ) + .build_and_check_for_diagnostics(db) .unwrap(); db.module_lowering_diagnostics(test_function.module_id) diff --git a/crates/cairo-lang-sierra-generator/src/statements_locations_test.rs b/crates/cairo-lang-sierra-generator/src/statements_locations_test.rs index 64c940a44ac..c4d974aec75 100644 --- a/crates/cairo-lang-sierra-generator/src/statements_locations_test.rs +++ b/crates/cairo-lang-sierra-generator/src/statements_locations_test.rs @@ -1,7 +1,7 @@ use cairo_lang_diagnostics::get_location_marks; use cairo_lang_lowering::db::LoweringGroup; use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId; -use cairo_lang_semantic::test_utils::setup_test_function; +use cairo_lang_semantic::test_utils::TestFunction; use cairo_lang_test_utils::get_direct_or_file_content; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -20,12 +20,14 @@ pub fn test_sierra_locations( let db = &SierraGenDatabaseForTesting::without_add_withdraw_gas(); let (_path, module_code) = get_direct_or_file_content(&inputs["module_code"]); // Parse code and create semantic model. - let (test_function, semantic_diagnostics) = setup_test_function( + let (test_function, semantic_diagnostics) = TestFunction::builder( db, inputs["function"].as_str(), inputs["function_name"].as_str(), &module_code, + None, ) + .build_and_check_for_diagnostics(db) .split(); let lowering_diagnostics = db.module_lowering_diagnostics(test_function.module_id); diff --git a/crates/cairo-lang-sierra-generator/src/test_utils.rs b/crates/cairo-lang-sierra-generator/src/test_utils.rs index b9b39be2181..3e925417908 100644 --- a/crates/cairo-lang-sierra-generator/src/test_utils.rs +++ b/crates/cairo-lang-sierra-generator/src/test_utils.rs @@ -149,7 +149,7 @@ pub fn setup_db_and_get_crate_id( ) -> (SierraGenDatabaseForTesting, cairo_lang_filesystem::ids::CrateId) { let db_val = SierraGenDatabaseForTesting::default(); let db = &db_val; - let crate_id = setup_test_crate(db, content); + let crate_id = setup_test_crate(db, content, None); let module_id = ModuleId::CrateRoot(crate_id); db.module_semantic_diagnostics(module_id) .unwrap() @@ -161,7 +161,7 @@ pub fn setup_db_and_get_crate_id( } pub fn get_dummy_function(db: &dyn SierraGenGroup) -> FreeFunctionId { - let crate_id = setup_test_crate(db.upcast(), "fn test(){}"); + let crate_id = setup_test_crate(db.upcast(), "fn test(){}", None); let module_id = ModuleId::CrateRoot(crate_id); db.module_free_functions_ids(module_id).unwrap()[0] } diff --git a/crates/cairo-lang-starknet/src/abi_test.rs b/crates/cairo-lang-starknet/src/abi_test.rs index ee2da1480c0..c8a26e109eb 100644 --- a/crates/cairo-lang-starknet/src/abi_test.rs +++ b/crates/cairo-lang-starknet/src/abi_test.rs @@ -3,7 +3,7 @@ use cairo_lang_defs::db::DefsGroup; use cairo_lang_semantic::db::PluginSuiteInput; use cairo_lang_semantic::inline_macros::get_default_plugin_suite; use cairo_lang_semantic::items::attribute::SemanticQueryAttrs; -use cairo_lang_semantic::test_utils::setup_test_module; +use cairo_lang_semantic::test_utils::TestModule; use cairo_lang_test_utils::parse_test_file::TestRunnerResult; use cairo_lang_test_utils::{get_direct_or_file_content, verify_diagnostics_expectation}; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -21,7 +21,8 @@ pub fn test_abi_failure( db.set_plugins_from_suite(get_default_plugin_suite() + starknet_plugin_suite()); let (_, cairo_code) = get_direct_or_file_content(&inputs["cairo_code"]); - let (module, diagnostics) = setup_test_module(db, &cairo_code).split(); + let (module, diagnostics) = + TestModule::builder(db, &cairo_code, None).build_and_check_for_diagnostics(db).split(); let submodules = db.module_submodules_ids(module.module_id).unwrap(); let contract_submodule = submodules @@ -64,7 +65,8 @@ pub fn test_storage_path_check( db.set_plugins_from_suite(get_default_plugin_suite() + starknet_plugin_suite()); let (_, cairo_code) = get_direct_or_file_content(&inputs["cairo_code"]); - let (_, diagnostics) = setup_test_module(db, &cairo_code).split(); + let (_, diagnostics) = + TestModule::builder(db, &cairo_code, None).build_and_check_for_diagnostics(db).split(); let test_error = verify_diagnostics_expectation(args, &diagnostics); diff --git a/crates/cairo-lang-starknet/src/contract_test.rs b/crates/cairo-lang-starknet/src/contract_test.rs index e86070d06f4..a95d35a9d93 100644 --- a/crates/cairo-lang-starknet/src/contract_test.rs +++ b/crates/cairo-lang-starknet/src/contract_test.rs @@ -15,7 +15,9 @@ fn test_contract_resolving() { let db = &mut RootDatabase::builder().detect_corelib().build().unwrap(); db.set_plugins_from_suite(get_default_plugin_suite() + starknet_plugin_suite()); - let crate_id = setup_test_crate(db, indoc! {" + let crate_id = setup_test_crate( + db, + indoc! {" mod not_a_contract {} #[starknet::contract] @@ -32,7 +34,9 @@ fn test_contract_resolving() { #[external(v0)] fn ep2(ref self: ContractState) {} } - "}); + "}, + None, + ); let contracts = find_contracts(db, &[crate_id]); assert_eq!(contracts.len(), 1); diff --git a/crates/cairo-lang-starknet/src/plugin/test.rs b/crates/cairo-lang-starknet/src/plugin/test.rs index d68c79a7f75..94be3692fbd 100644 --- a/crates/cairo-lang-starknet/src/plugin/test.rs +++ b/crates/cairo-lang-starknet/src/plugin/test.rs @@ -9,7 +9,7 @@ use cairo_lang_filesystem::db::FilesGroup; use cairo_lang_filesystem::ids::FileLongId; use cairo_lang_filesystem::span::{TextOffset, TextSpan, TextWidth}; use cairo_lang_plugins::test_utils::expand_module_text; -use cairo_lang_semantic::test_utils::setup_test_module; +use cairo_lang_semantic::test_utils::TestModule; use cairo_lang_test_utils::parse_test_file::{TestFileRunner, TestRunnerResult}; use cairo_lang_test_utils::{get_direct_or_file_content, verify_diagnostics_expectation}; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; @@ -29,7 +29,9 @@ impl TestFileRunner for ExpandContractTestRunner { ) -> TestRunnerResult { let db = SHARED_DB.lock().unwrap().snapshot(); let (_, cairo_code) = get_direct_or_file_content(&inputs["cairo_code"]); - let (test_module, _semantic_diagnostics) = setup_test_module(&db, &cairo_code).split(); + let (test_module, _semantic_diagnostics) = TestModule::builder(&db, &cairo_code, None) + .build_and_check_for_diagnostics(&db) + .split(); let mut module_ids = vec![test_module.module_id]; if let Ok(submodules_ids) = db.module_submodules_ids(test_module.module_id) { diff --git a/tests/e2e_test.rs b/tests/e2e_test.rs index 50794ab9241..62b4d56b2be 100644 --- a/tests/e2e_test.rs +++ b/tests/e2e_test.rs @@ -7,7 +7,7 @@ use cairo_lang_lowering::db::LoweringGroup; use cairo_lang_lowering::optimizations::config::OptimizationConfig; use cairo_lang_semantic::db::PluginSuiteInput; use cairo_lang_semantic::inline_macros::get_default_plugin_suite; -use cairo_lang_semantic::test_utils::setup_test_module; +use cairo_lang_semantic::test_utils::TestModule; use cairo_lang_sierra::extensions::gas::CostTokenType; use cairo_lang_sierra::ids::FunctionId; use cairo_lang_sierra::program::{Function, Program}; @@ -227,7 +227,9 @@ fn run_e2e_test( &SHARED_DB_NO_GAS_NO_OPTS }); // Parse code and create semantic model. - let test_module = setup_test_module(locked_db.deref_mut(), inputs["cairo"].as_str()).unwrap(); + let test_module = TestModule::builder(locked_db.deref_mut(), inputs["cairo"].as_str(), None) + .build_and_check_for_diagnostics(&*locked_db) + .unwrap(); let db = locked_db.snapshot(); DiagnosticsReporter::stderr().with_crates(&[test_module.crate_id]).ensure(&db).unwrap();