Skip to content

Commit

Permalink
Fixed test collection, added basic test (#245)
Browse files Browse the repository at this point in the history
Co-authored-by: Jakub Ptak <[email protected]>
  • Loading branch information
Radinyn and Solpatium authored Jul 24, 2023
1 parent dfe5ff3 commit 54ef1f7
Show file tree
Hide file tree
Showing 17 changed files with 385 additions and 15 deletions.
2 changes: 1 addition & 1 deletion starknet-foundry/crates/forge/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main_execution() -> Result<()> {
// TODO #1997
let corelib_dir = load_corelib()?;
let corelib_path: PathBuf = corelib_dir.path().into();
let corelib = Utf8PathBuf::try_from(corelib_path.clone())
let corelib = Utf8PathBuf::try_from(corelib_path)
.context("Failed to convert corelib path to Utf8PathBuf")?;

let predeployed_contracts_dir = load_predeployed_contracts()?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "test_multiple"
version = "0.1.0"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest

[dependencies]
# foo = { path = "vendor/foo" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use test_multiple::fob::fob_impl::fob_fn;

#[test]
fn test_fob() {
assert(fob_fn(0, 1, 10) == 55, fob_fn(0, 1, 10));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod fab_impl;

#[test]
fn test_simple() {
assert(1 == 1, 1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use test_multiple::fib::fib_fn;

fn fab_fn(a: felt252, b: felt252, n: felt252) -> felt252 {
match n {
0 => a,
_ => fab_fn(b, a + b, n - 1),
}
}

#[test]
fn test_fab() {
assert(fab_fn(0, 1, 10) == 55, fab_fn(0, 1, 10));
}

#[test]
fn test_how_does_this_work() {
assert(fib_fn(0, 1, 10) == 55, fib_fn(0, 1, 10));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use test_multiple::fob::fob_impl::fob_fn;
use test_multiple::fab::fab_impl::fab_fn;
use test_multiple::fib::fib_fn;

#[test]
fn test_fib() {
assert(fib_fn(0, 1, 10) == 55, fib_fn(0, 1, 10));
}

#[test]
fn test_fob() {
assert(fob_fn(0, 1, 10) == 55, fob_fn(0, 1, 10));
}

#[test]
fn test_fab() {
assert(fab_fn(0, 1, 10) == 55, fab_fn(0, 1, 10));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use test_multiple::fob::fob_impl::fob_fn;
use super::fab::fab_impl::fab_fn;

fn fib_fn(a: felt252, b: felt252, n: felt252) -> felt252 {
match n {
0 => a,
_ => fib_fn(b, a + b, n - 1),
}
}

#[test]
fn test_fib() {
assert(fib_fn(0, 1, 10) == 55, fib_fn(0, 1, 10));
}

#[test]
fn test_fob_in_fib() {
assert(fob_fn(0, 1, 10) == 55, fob_fn(0, 1, 10));
}

#[test]
fn test_fab_in_fib() {
assert(fab_fn(0, 1, 10) == 55, fab_fn(0, 1, 10));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod fob_impl;

#[test]
fn test_simple() {
assert(1 == 1, 1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use test_multiple::fob::fob_impl::fob_fn;
use test_multiple::fab::fab_impl::fab_fn;
use test_multiple::fib::fib_fn;

#[test]
fn test_fib() {
assert(fib_fn(0, 1, 10) == 55, fib_fn(0, 1, 10));
}

#[test]
fn test_fob() {
assert(fob_fn(0, 1, 10) == 55, fob_fn(0, 1, 10));
}

#[test]
fn test_fab() {
assert(fab_fn(0, 1, 10) == 55, fab_fn(0, 1, 10));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn fob_fn(a: felt252, b: felt252, n: felt252) -> felt252 {
match n {
0 => a,
_ => fob_fn(b, a + b, n - 1),
}
}

#[test]
fn test_fob() {
assert(fob_fn(0, 1, 10) == 55, fob_fn(0, 1, 10));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
mod fib;
mod fob;
mod fab;

use fob::fob_impl::fob_fn;
use fib::fib_fn;

#[test]
fn test_simple() {
assert(1 == 1, 1);
}

#[test]
fn test_fob_in_lib() {
assert(fob_fn(0, 1, 10) == 55, fob_fn(0, 1, 10));
}

#[test]
fn test_fib_in_lib() {
assert(fib_fn(0, 1, 10) == 55, fib_fn(0, 1, 10));
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use test_multiple::fob::fob_impl::fob_fn;
use test_multiple::fab::fab_impl::fab_fn;
use test_multiple::fib::fib_fn;

#[test]
fn test_fib() {
assert(fib_fn(0, 1, 10) == 55, fib_fn(0, 1, 10));
}

#[test]
fn test_fob() {
assert(fob_fn(0, 1, 10) == 55, fob_fn(0, 1, 10));
}

#[test]
fn test_fab() {
assert(fab_fn(0, 1, 10) == 55, fab_fn(0, 1, 10));
}
56 changes: 56 additions & 0 deletions starknet-foundry/crates/forge/tests/e2e/collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use assert_fs::fixture::PathCopy;
use indoc::indoc;

use crate::e2e::common::runner::runner;

#[test]
fn complex_structure() {
let temp = assert_fs::TempDir::new().unwrap();
temp.copy_from(
"tests/data/complex_structure_test",
&["**/*.cairo", "**/*.toml"],
)
.unwrap();

let snapbox = runner();

snapbox
.current_dir(&temp)
.assert()
.success()
.stdout_matches(indoc! {r#"Collected 21 test(s) and 11 test file(s)
Running 1 test(s) from fob.cairo
[PASS] fob::fob::test_fob
Running 2 test(s) from src/fab/fab_impl.cairo
[PASS] fab_impl::fab_impl::test_fab
[PASS] fab_impl::fab_impl::test_how_does_this_work
Running 3 test(s) from src/fab/fibfabfob.cairo
[PASS] fibfabfob::fibfabfob::test_fib
[PASS] fibfabfob::fibfabfob::test_fob
[PASS] fibfabfob::fibfabfob::test_fab
Running 1 test(s) from src/fab.cairo
[PASS] fab::fab::test_simple
Running 3 test(s) from src/fib.cairo
[PASS] fib::fib::test_fib
[PASS] fib::fib::test_fob_in_fib
[PASS] fib::fib::test_fab_in_fib
Running 3 test(s) from src/fob/fibfabfob.cairo
[PASS] fibfabfob::fibfabfob::test_fib
[PASS] fibfabfob::fibfabfob::test_fob
[PASS] fibfabfob::fibfabfob::test_fab
Running 1 test(s) from src/fob/fob_impl.cairo
[PASS] fob_impl::fob_impl::test_fob
Running 1 test(s) from src/fob.cairo
[PASS] fob::fob::test_simple
Running 3 test(s) from src/lib.cairo
[PASS] src::test_simple
[PASS] src::test_fob_in_lib
[PASS] src::test_fib_in_lib
Running 0 test(s) from tests/fab.cairo
Running 3 test(s) from tests/fibfabfob.cairo
[PASS] fibfabfob::fibfabfob::test_fib
[PASS] fibfabfob::fibfabfob::test_fob
[PASS] fibfabfob::fibfabfob::test_fab
Tests: 21 passed, 0 failed, 0 skipped
"#});
}
1 change: 1 addition & 0 deletions starknet-foundry/crates/forge/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod collection;
pub(crate) mod common;
mod running;
47 changes: 33 additions & 14 deletions starknet-foundry/crates/test-collector/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use crate::project::setup_project;
use crate::project::{
get_main_crate_ids_from_project, setup_single_file_project,
update_crate_roots_from_project_config, ProjectError,
};
use crate::sierra_casm_generator::SierraCasmGenerator;
use anyhow::{anyhow, Context, Result};
use cairo_felt::Felt252;
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
use cairo_lang_compiler::project::setup_project;
use cairo_lang_compiler::project::{
get_main_crate_ids_from_project, setup_single_file_project,
update_crate_roots_from_project_config, ProjectError,
};
use cairo_lang_debug::DebugWithDb;
use cairo_lang_defs::ids::ModuleId;
use cairo_lang_defs::ids::{FreeFunctionId, FunctionWithBodyId, ModuleItemId};
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_diagnostics::ToOption;
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
use cairo_lang_filesystem::db::init_dev_corelib;
use cairo_lang_filesystem::db::FilesGroup;
use cairo_lang_filesystem::detect::detect_corelib;
use cairo_lang_filesystem::ids::CrateId;
use cairo_lang_filesystem::ids::{CrateId, FileId, FileLongId};
use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId;
use cairo_lang_project::{DeserializationError, ProjectConfig, ProjectConfigContent};
use cairo_lang_semantic::db::SemanticGroup;
Expand All @@ -36,10 +38,14 @@ use cairo_lang_utils::OptionHelper;
use itertools::Itertools;
use num_traits::ToPrimitive;
use smol_str::SmolStr;
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;

#[allow(clippy::all, clippy::pedantic, clippy::nursery)]
pub mod project;
pub mod sierra_casm_generator;

pub fn build_project_config(
Expand Down Expand Up @@ -109,23 +115,33 @@ pub struct SingleTestConfig {
/// Finds the tests in the requested crates.
pub fn find_all_tests(
db: &dyn SemanticGroup,
main_crates: Vec<CrateId>,
main_crates: &[CrateId],
file_id: FileId,
) -> Vec<(FreeFunctionId, SingleTestConfig)> {
let mut tests = vec![];
for crate_id in main_crates {
let modules = db.crate_modules(crate_id);
for module_id in modules.iter() {
let Ok(module_items) = db.module_items(*module_id) else {

let crate_modules = db.crate_modules(main_crates[0]);

let Ok(file_modules) = db.file_modules(file_id) else { panic!("Error getting file modules! Make sure there are no test files that are in the same directory as the lib.cairo file while not being a part of this package themselves.") };

// Somehow only the intersection of those two sets gives us tests from that single file :D
let crate_modules_set = crate_modules.iter().collect::<HashSet<_>>();
let file_modules_set: HashSet<&ModuleId> = file_modules.iter().collect::<HashSet<_>>();
let modules = crate_modules_set
.intersection(&file_modules_set)
.collect::<Vec<_>>();

for module_id in &modules {
let Ok(module_items) = db.module_items(***module_id) else {
continue;
};
tests.extend(
tests.extend(
module_items.iter().filter_map(|item| {
let ModuleItemId::FreeFunction(func_id) = item else { return None };
let Ok(attrs) = db.function_with_body_attributes(FunctionWithBodyId::Free(*func_id)) else { return None };
Some((*func_id, try_extract_test_config(db.upcast(), &attrs).unwrap()?))
}),
);
}
}
tests
}
Expand Down Expand Up @@ -277,6 +293,7 @@ pub struct TestCase {
pub available_gas: Option<usize>,
}

// must be provided with CAIRO FILE INPUT PATHS
// returns tuple[sierra if no output_path, list[test_name, test_config]]
pub fn collect_tests(
input_path: &str,
Expand Down Expand Up @@ -322,7 +339,9 @@ pub fn collect_tests(
above"
));
}
let all_tests = find_all_tests(db, main_crate_ids);

let file_id = db.intern_file(FileLongId::OnDisk(PathBuf::from_str(input_path)?));
let all_tests = find_all_tests(db, &main_crate_ids, file_id);

let z: Vec<ConcreteFunctionWithBodyId> = all_tests
.iter()
Expand Down
Loading

0 comments on commit 54ef1f7

Please sign in to comment.