Skip to content

Commit

Permalink
Redesigned semantic testing utilities
Browse files Browse the repository at this point in the history
commit-id:46150109
  • Loading branch information
integraledelebesgue committed Dec 6, 2024
1 parent 913ac66 commit 90b0f4d
Show file tree
Hide file tree
Showing 40 changed files with 475 additions and 237 deletions.
2 changes: 1 addition & 1 deletion crates/cairo-lang-compiler/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn can_collect_executables() {
suite.add_plugin::<MockExecutablePlugin>();
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;
Expand Down
10 changes: 7 additions & 3 deletions crates/cairo-lang-executable/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions crates/cairo-lang-lowering/src/borrow_check/test.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -23,12 +23,14 @@ fn test_borrow_check(
_args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down
6 changes: 4 additions & 2 deletions crates/cairo-lang-lowering/src/inline/test.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -26,12 +26,14 @@ fn test_function_inlining(
_args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down
6 changes: 4 additions & 2 deletions crates/cairo-lang-lowering/src/lower/generated_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -31,12 +31,14 @@ fn test_generated_function(
_args: &OrderedHashMap<String, String>,
) -> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -28,12 +28,14 @@ fn test_branch_inversion(
_args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -29,12 +29,14 @@ fn test_cancel_ops(
_args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -29,12 +29,14 @@ fn test_match_optimizer(
_args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -43,12 +43,14 @@ impl TestFileRunner for GetRedepositTestRunner {
_args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -31,12 +31,14 @@ fn test_match_optimizer(
_args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -28,12 +28,14 @@ fn test_reorder_statements(
_args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -30,12 +30,14 @@ fn test_return_optimizer(
_args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -26,12 +26,14 @@ fn test_scrub_units(
_args: &OrderedHashMap<String, String>,
) -> 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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -29,12 +29,14 @@ fn test_split_structs(
_args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down
14 changes: 9 additions & 5 deletions crates/cairo-lang-lowering/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ 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;
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;
Expand Down Expand Up @@ -66,12 +65,14 @@ fn test_function_lowering(
args: &OrderedHashMap<String, String>,
) -> 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);
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -192,7 +195,7 @@ fn test_sizes() {
("core::cmp::min::<u8>::Coupon", 0),
];

let test_module = setup_test_module_ex(
let test_module = TestModule::builder(
db,
&type_to_size
.iter()
Expand All @@ -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();
Expand Down
Loading

0 comments on commit 90b0f4d

Please sign in to comment.