Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dojo-lang): add all crates to test database for Cairo test runner #2296

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions bin/sozo/src/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use dojo_lang::compiler::{collect_core_crate_ids, collect_external_crate_ids, Pr
use dojo_lang::plugin::dojo_plugin_suite;
use dojo_lang::scarb_internal::{cfg_set_from_component, crates_config_for_compilation_unit};
use dojo_world::metadata::dojo_metadata_from_package;
use scarb::compiler::helpers::collect_main_crate_ids;
use scarb::compiler::helpers::{collect_all_crate_ids, collect_main_crate_ids};
use scarb::compiler::{CairoCompilationUnit, CompilationUnit, CompilationUnitAttributes};
use scarb::core::{Config, Package, TargetKind};
use scarb::ops::{self, CompileOpts};
Expand Down Expand Up @@ -152,8 +152,8 @@ impl TestArgs {
bail!("failed to compile");
}

let mut main_crate_ids = collect_main_crate_ids(&unit, &db);
let test_crate_ids = main_crate_ids.clone();
let mut main_crate_ids = collect_all_crate_ids(&unit, &db);
let test_crate_ids = collect_main_crate_ids(&unit, &db);

if unit.main_package_id.name.to_string() != "dojo" {
let core_crate_ids = collect_core_crate_ids(&db);
Expand All @@ -176,7 +176,8 @@ impl TestArgs {
let compiler =
TestCompiler { db: db.snapshot(), main_crate_ids, test_crate_ids, starknet: true };

let runner = CompiledTestRunner { compiled: compiler.build()?, config };
let compiled = compiler.build()?;
let runner = CompiledTestRunner { compiled, config };

// Database is required here for the profiler to work.
runner.run(Some(&db))?;
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/src/tests/base.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use contract_upgrade::{IQuantumLeapDispatcher, IQuantumLeapDispatcherTrait};

// Utils
fn deploy_world() -> IWorldDispatcher {
spawn_test_world("dojo", array![])
spawn_test_world(["dojo"].span(), [].span())
}

// A test contract needs to be used instead of previously used base contract since.
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/src/tests/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub mod bar {
}

pub fn deploy_world() -> IWorldDispatcher {
spawn_test_world("dojo", array![])
spawn_test_world(["dojo"].span(), [].span())
}

pub fn deploy_world_and_bar() -> (IWorldDispatcher, IbarDispatcher) {
Expand Down
6 changes: 3 additions & 3 deletions crates/dojo-core/src/tests/world/resources.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn test_set_metadata_world() {

#[test]
fn test_set_metadata_resource_owner() {
let world = spawn_test_world("dojo", array![foo::TEST_CLASS_HASH],);
let world = spawn_test_world(["dojo"].span(), [foo::TEST_CLASS_HASH].span(),);

let bob = starknet::contract_address_const::<0xb0b>();

Expand Down Expand Up @@ -63,7 +63,7 @@ fn test_set_metadata_resource_owner() {
)
)]
fn test_set_metadata_not_possible_for_resource_writer() {
let world = spawn_test_world("dojo", array![foo::TEST_CLASS_HASH],);
let world = spawn_test_world(["dojo"].span(), [foo::TEST_CLASS_HASH].span(),);

let bob = starknet::contract_address_const::<0xb0b>();

Expand Down Expand Up @@ -102,7 +102,7 @@ fn test_set_metadata_not_possible_for_random_account() {
#[test]
#[should_panic(expected: ("Caller `57005` is not an account", 'ENTRYPOINT_FAILED',))]
fn test_set_metadata_through_malicious_contract() {
let world = spawn_test_world("dojo", array![foo::TEST_CLASS_HASH],);
let world = spawn_test_world(["dojo"].span(), [foo::TEST_CLASS_HASH].span(),);

let bob = starknet::contract_address_const::<0xb0b>();
let malicious_contract = starknet::contract_address_const::<0xdead>();
Expand Down
8 changes: 4 additions & 4 deletions crates/dojo-core/src/tests/world/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,21 @@ fn test_emit() {

// Utils
fn deploy_world() -> IWorldDispatcher {
spawn_test_world("dojo", array![])
spawn_test_world(["dojo"].span(), [].span())
}

#[test]
fn test_execute_multiple_worlds() {
// Deploy world contract
let world1 = spawn_test_world("dojo", array![foo::TEST_CLASS_HASH],);
let world1 = spawn_test_world(["dojo"].span(), [foo::TEST_CLASS_HASH].span(),);
let contract_address = deploy_with_world_address(bar::TEST_CLASS_HASH, world1);

world1.grant_writer(Model::<Foo>::selector(), contract_address);

let bar1_contract = IbarDispatcher { contract_address };

// Deploy another world contract
let world2 = spawn_test_world("dojo", array![foo::TEST_CLASS_HASH],);
let world2 = spawn_test_world(["dojo"].span(), [foo::TEST_CLASS_HASH].span(),);
let contract_address = deploy_with_world_address(bar::TEST_CLASS_HASH, world2);

world2.grant_writer(Model::<Foo>::selector(), contract_address);
Expand Down Expand Up @@ -202,7 +202,7 @@ fn bench_execute() {

#[test]
fn bench_execute_complex() {
let world = spawn_test_world("dojo", array![character::TEST_CLASS_HASH],);
let world = spawn_test_world(["dojo"].span(), [character::TEST_CLASS_HASH].span(),);
let contract_address = deploy_with_world_address(bar::TEST_CLASS_HASH, world);
let bar_contract = IbarDispatcher { contract_address };

Expand Down
7 changes: 5 additions & 2 deletions crates/dojo-core/src/utils/test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn deploy_with_world_address(class_hash: felt252, world: IWorldDispatcher) -
deploy_contract(class_hash, array![world.contract_address.into()].span())
}

pub fn spawn_test_world(namespace: ByteArray, models: Array<felt252>) -> IWorldDispatcher {
pub fn spawn_test_world(namespaces: Span<ByteArray>, models: Span<felt252>) -> IWorldDispatcher {
let salt = core::testing::get_available_gas();

// deploy world
Expand All @@ -55,7 +55,10 @@ pub fn spawn_test_world(namespace: ByteArray, models: Array<felt252>) -> IWorldD
let world = IWorldDispatcher { contract_address: world_address };

// register namespace
world.register_namespace(namespace);
let mut namespaces = namespaces;
while let Option::Some(namespace) = namespaces.pop_front() {
world.register_namespace(namespace.clone());
};

// register models
let mut index = 0;
Expand Down
9 changes: 7 additions & 2 deletions examples/spawn-and-move/src/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ mod tests {
use dojo::utils::test::{spawn_test_world, deploy_contract};

use super::{actions, IActionsDispatcher, IActionsDispatcherTrait};
use armory::flatbow;
use dojo_examples::models::{Position, position, Moves, moves, Direction, Vec2};

#[test]
Expand All @@ -200,9 +201,13 @@ mod tests {
let caller = starknet::contract_address_const::<0x0>();

// models
let mut models = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH,];
let mut models = array![
position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH, flatbow::TEST_CLASS_HASH
];
// deploy world with models
let world = spawn_test_world("dojo_examples", models);
let world = spawn_test_world(
["dojo_examples", "dojo_examples_weapons"].span(), models.span()
);

// deploy systems contract
let contract_address = world
Expand Down
Loading