From aa7dd3ef0e55dc9794b62dbcb33a4cb655db39e4 Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Mon, 1 May 2023 12:37:20 -0500 Subject: [PATCH] Move core tests to avoid running them in dojo projects (#275) --- crates/dojo-core/src/executor.cairo | 44 ---------- crates/dojo-core/src/storage/index.cairo | 69 ---------------- crates/dojo-core/src/storage/query.cairo | 37 --------- crates/dojo-core/src/world.cairo | 80 ------------------- crates/dojo-core/src/world_factory.cairo | 75 ----------------- crates/dojo-core/tests/README.md | 1 + crates/dojo-core/tests/Scarb.toml | 7 ++ crates/dojo-core/tests/src/executor.cairo | 42 ++++++++++ crates/dojo-core/tests/src/lib.cairo | 4 + crates/dojo-core/tests/src/storage.cairo | 2 + .../dojo-core/tests/src/storage/index.cairo | 66 +++++++++++++++ .../dojo-core/tests/src/storage/query.cairo | 55 +++++++++++++ crates/dojo-core/tests/src/world.cairo | 77 ++++++++++++++++++ .../dojo-core/tests/src/world_factory.cairo | 71 ++++++++++++++++ scripts/cairo_test.sh | 4 +- 15 files changed, 327 insertions(+), 307 deletions(-) create mode 100644 crates/dojo-core/tests/README.md create mode 100644 crates/dojo-core/tests/Scarb.toml create mode 100644 crates/dojo-core/tests/src/executor.cairo create mode 100644 crates/dojo-core/tests/src/lib.cairo create mode 100644 crates/dojo-core/tests/src/storage.cairo create mode 100644 crates/dojo-core/tests/src/storage/index.cairo create mode 100644 crates/dojo-core/tests/src/storage/query.cairo create mode 100644 crates/dojo-core/tests/src/world.cairo create mode 100644 crates/dojo-core/tests/src/world_factory.cairo diff --git a/crates/dojo-core/src/executor.cairo b/crates/dojo-core/src/executor.cairo index 7ae7830608..d79493a6d6 100644 --- a/crates/dojo-core/src/executor.cairo +++ b/crates/dojo-core/src/executor.cairo @@ -24,47 +24,3 @@ mod Executor { res } } - -mod tests { - use core::traits::Into; - use core::result::ResultTrait; - use array::ArrayTrait; - use option::OptionTrait; - use traits::TryInto; - - use starknet::syscalls::deploy_syscall; - use starknet::class_hash::Felt252TryIntoClassHash; - use dojo_core::interfaces::IExecutorDispatcher; - use dojo_core::interfaces::IExecutorDispatcherTrait; - - #[derive(Component)] - struct Foo { - a: felt252, - b: u128, - } - - #[system] - mod Bar { - use super::Foo; - - fn execute(foo: Foo) -> Foo { - foo - } - } - - #[test] - #[available_gas(30000000)] - fn test_executor() { - let constructor_calldata = array::ArrayTrait::::new(); - let (executor_address, _) = deploy_syscall( - super::Executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false - ).unwrap(); - - let executor = IExecutorDispatcher { contract_address: executor_address }; - - let mut system_calldata = ArrayTrait::new(); - system_calldata.append(42); - system_calldata.append(53); - let res = executor.execute(BarSystem::TEST_CLASS_HASH.try_into().unwrap(), system_calldata.span()); - } -} diff --git a/crates/dojo-core/src/storage/index.cairo b/crates/dojo-core/src/storage/index.cairo index 81d7ed5951..ead9582d29 100644 --- a/crates/dojo-core/src/storage/index.cairo +++ b/crates/dojo-core/src/storage/index.cairo @@ -64,72 +64,3 @@ mod Index { return _query(table, idx + 1_usize, table_len, ref res); } } - -mod tests { - use array::ArrayTrait; - use traits::Into; - - use dojo_core::integer::u250; - use super::Index; - - #[test] - #[available_gas(2000000)] - fn test_index_entity() { - let no_query = Index::query(69.into()); - assert(no_query.len() == 0_usize, 'entity indexed'); - - Index::create(69.into(), 420.into()); - let query = Index::query(69.into()); - assert(query.len() == 1_usize, 'entity not indexed'); - assert(*query.at(0_usize) == 420.into(), 'entity value incorrect'); - - Index::create(69.into(), 420.into()); - let noop_query = Index::query(69.into()); - assert(noop_query.len() == 1_usize, 'index should be noop'); - - Index::create(69.into(), 1337.into()); - let two_query = Index::query(69.into()); - assert(two_query.len() == 2_usize, 'index should have two query'); - assert(*two_query.at(1_usize) == 1337.into(), 'entity value incorrect'); - } - - #[test] - #[available_gas(2000000)] - fn test_entity_delete_basic() { - Index::create(69.into(), 420.into()); - let query = Index::query(69.into()); - assert(query.len() == 1_usize, 'entity not indexed'); - assert(*query.at(0_usize) == 420.into(), 'entity value incorrect'); - - assert(Index::exists(69.into(), 420.into()), 'entity should exist'); - - Index::delete(69.into(), 420.into()); - - assert(!Index::exists(69.into(), 420.into()), 'entity should not exist'); - let no_query = Index::query(69.into()); - assert(no_query.len() == 0_usize, 'index should have no query'); - } - - #[test] - #[available_gas(20000000)] - fn test_entity_query_delete_shuffle() { - let table = 1.into(); - Index::create(table, 10.into()); - Index::create(table, 20.into()); - Index::create(table, 30.into()); - assert(Index::query(table).len() == 3_usize, 'wrong size'); - - Index::delete(table, 10.into()); - let entities = Index::query(table); - assert(entities.len() == 2_usize, 'wrong size'); - assert(*entities.at(0_usize) == 30.into(), 'idx 0 not 30'); - assert(*entities.at(1_usize) == 20.into(), 'idx 1 not 20'); - } - - #[test] - #[available_gas(20000000)] - fn test_entity_query_delete_non_existing() { - assert(Index::query(69.into()).len() == 0_usize, 'table len != 0'); - Index::delete(69.into(), 999.into()); // deleting non-existing should not panic - } -} diff --git a/crates/dojo-core/src/storage/query.cairo b/crates/dojo-core/src/storage/query.cairo index 83bf2e304e..d9c5165b80 100644 --- a/crates/dojo-core/src/storage/query.cairo +++ b/crates/dojo-core/src/storage/query.cairo @@ -142,40 +142,3 @@ impl IntoPartitionedQuery< query } } - -#[test] -#[available_gas(2000000)] -fn test_query_id() { - let mut keys = ArrayTrait::new(); - keys.append(420.into()); - let query: u250 = QueryTrait::new(0, 0.into(), keys.span()).into(); - assert(query == 420.into(), 'Incorrect hash'); -} - -#[test] -#[available_gas(2000000)] -fn test_query_into() { - let query: Query = 420.into(); - assert(*query.keys.at(0_usize) == 420.into(), 'Incorrect query'); - let query1: Query = (69).into(); - assert(*query1.keys.at(0_usize) == 69.into(), 'Incorrect query'); - let query2: Query = (69, 420).into(); - assert(*query2.keys.at(0_usize) == 69.into(), 'Incorrect query'); - assert(*query2.keys.at(1_usize) == 420.into(), 'Incorrect query'); - let query3: Query = (69, 420, 777).into(); - assert(*query3.keys.at(0_usize) == 69.into(), 'Incorrect query'); - assert(*query3.keys.at(1_usize) == 420.into(), 'Incorrect query'); - assert(*query3.keys.at(2_usize) == 777.into(), 'Incorrect query'); -} - -#[test] -#[available_gas(2000000)] -fn test_partitioned_query_into() { - let query: Query = (69, (420, )).into_partitioned(); - assert(query.partition == 69.into(), 'Incorrect partition'); - assert(*query.keys.at(0_usize) == 420.into(), 'Incorrect query'); - - let query2: Query = (69, (420, 777)).into_partitioned(); - assert(query2.partition == 69.into(), 'Incorrect partition'); - assert(*query2.keys.at(1_usize) == 777.into(), 'Incorrect query'); -} diff --git a/crates/dojo-core/src/world.cairo b/crates/dojo-core/src/world.cairo index 65b6840404..0e223bdfdb 100644 --- a/crates/dojo-core/src/world.cairo +++ b/crates/dojo-core/src/world.cairo @@ -136,83 +136,3 @@ mod World { executor::write(contract_address); } } - -mod tests { - use array::ArrayTrait; - use core::result::ResultTrait; - use traits::Into; - use traits::TryInto; - use option::OptionTrait; - use starknet::class_hash::Felt252TryIntoClassHash; - use starknet::syscalls::deploy_syscall; - - use super::World; - use dojo_core::integer::u250; - use dojo_core::integer::U32IntoU250; - use dojo_core::storage::query::QueryTrait; - use dojo_core::interfaces::IWorldDispatcher; - use dojo_core::interfaces::IWorldDispatcherTrait; - use dojo_core::executor::Executor; - - #[derive(Component)] - struct Foo { - a: felt252, - b: u128, - } - - #[test] - #[available_gas(2000000)] - fn test_component() { - let name = 'Foo'.into(); - World::register_component(FooComponent::TEST_CLASS_HASH.try_into().unwrap()); - let mut data = ArrayTrait::::new(); - data.append(1337); - let id = World::uuid(); - World::set_entity(name, QueryTrait::new_from_id(id.into()), 0_u8, data.span()); - let stored = World::entity(name, QueryTrait::new_from_id(id.into()), 0_u8, 1_usize); - assert(*stored.snapshot.at(0_usize) == 1337, 'data not stored'); - } - - #[system] - mod Bar { - use super::Foo; - - fn execute(foo: Foo) -> Foo { - foo - } - } - - #[test] - #[available_gas(2000000)] - fn test_system() { - let executor_constructor_calldata = array::ArrayTrait::::new(); - let (executor_address, _) = deploy_syscall( - Executor::TEST_CLASS_HASH.try_into().unwrap(), 0, executor_constructor_calldata.span(), false - ).unwrap(); - - let mut constructor_calldata = array::ArrayTrait::::new(); - constructor_calldata.append('World'); - constructor_calldata.append(executor_address.into()); - let (world_address, _) = deploy_syscall( - super::World::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false - ).unwrap(); - let world = IWorldDispatcher { contract_address: world_address }; - - world.register_system(BarSystem::TEST_CLASS_HASH.try_into().unwrap()); - let mut data = ArrayTrait::::new(); - data.append(1337); - data.append(1337); - let id = world.uuid(); - world.execute('Bar'.into(), data.span()); - } - - #[test] - #[available_gas(2000000)] - fn test_constructor() { - starknet::testing::set_caller_address(starknet::contract_address_const::<0x420>()); - World::constructor( - 'World'.into(), - starknet::contract_address_const::<0x1337>(), - ); - } -} \ No newline at end of file diff --git a/crates/dojo-core/src/world_factory.cairo b/crates/dojo-core/src/world_factory.cairo index b96a62c1ab..6c2fa2a1b2 100644 --- a/crates/dojo-core/src/world_factory.cairo +++ b/crates/dojo-core/src/world_factory.cairo @@ -100,78 +100,3 @@ mod WorldFactory { return register_systems(systems, systems_len, index + 1_usize, world_address); } } - - - -mod tests { - use core::traits::Into; - use core::result::ResultTrait; - use array::ArrayTrait; - use option::OptionTrait; - use traits::TryInto; - - use starknet::syscalls::deploy_syscall; - use starknet::class_hash::ClassHash; - use starknet::class_hash::Felt252TryIntoClassHash; - use dojo_core::interfaces::IWorldDispatcher; - use dojo_core::interfaces::IWorldDispatcherTrait; - use dojo_core::executor::Executor; - use dojo_core::world::World; - use super::WorldFactory; - - #[derive(Component)] - struct Foo { - a: felt252, - b: u128, - } - - #[system] - mod Bar { - use super::Foo; - - fn execute(foo: Foo) -> Foo { - foo - } - } - - #[test] - #[available_gas(2000000)] - fn test_constructor() { - WorldFactory::constructor(starknet::class_hash_const::<0x420>(), starknet::contract_address_const::<0x69>()); - let world_class_hash = WorldFactory::world(); - assert(world_class_hash == starknet::class_hash_const::<0x420>(), 'wrong world class hash'); - let executor_address = WorldFactory::executor(); - assert(executor_address == starknet::contract_address_const::<0x69>(), 'wrong executor contract'); - } - - #[test] - #[available_gas(30000000)] - fn test_spawn_world() { - let constructor_calldata = array::ArrayTrait::::new(); - let (executor_address, _) = deploy_syscall( - Executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false - ).unwrap(); - - WorldFactory::constructor(World::TEST_CLASS_HASH.try_into().unwrap(), executor_address); - assert(WorldFactory::executor() == executor_address, 'wrong executor address'); - assert(WorldFactory::world() == World::TEST_CLASS_HASH.try_into().unwrap(), 'wrong world class hash'); - - let mut systems = array::ArrayTrait::::new(); - systems.append(BarSystem::TEST_CLASS_HASH.try_into().unwrap()); - - let mut components = array::ArrayTrait::::new(); - components.append(FooComponent::TEST_CLASS_HASH.try_into().unwrap()); - - let world_address = WorldFactory::spawn('TestWorld'.into(), components, systems); - - let foo_hash = IWorldDispatcher { - contract_address: world_address - }.component('Foo'.into()); - assert(foo_hash == FooComponent::TEST_CLASS_HASH.try_into().unwrap(), 'component not registered'); - - let bar_hash = IWorldDispatcher { - contract_address: world_address - }.system('Bar'.into()); - assert(bar_hash == BarSystem::TEST_CLASS_HASH.try_into().unwrap(), 'system not registered'); - } -} \ No newline at end of file diff --git a/crates/dojo-core/tests/README.md b/crates/dojo-core/tests/README.md new file mode 100644 index 0000000000..8cdaa8379f --- /dev/null +++ b/crates/dojo-core/tests/README.md @@ -0,0 +1 @@ +We implement tests as a separate crate due to a tooling limitation where including core contracts in a dojo project also includes their tests cases. This way, dojo projects don't need to inadvertantly run core tests. \ No newline at end of file diff --git a/crates/dojo-core/tests/Scarb.toml b/crates/dojo-core/tests/Scarb.toml new file mode 100644 index 0000000000..a593e279c1 --- /dev/null +++ b/crates/dojo-core/tests/Scarb.toml @@ -0,0 +1,7 @@ +[package] +name = "dojo_core_tests" +version = "0.1.0" +description = "Tests for Dojo Core" + +[dependencies] +dojo_core = { path = "../" } \ No newline at end of file diff --git a/crates/dojo-core/tests/src/executor.cairo b/crates/dojo-core/tests/src/executor.cairo new file mode 100644 index 0000000000..f5b8536800 --- /dev/null +++ b/crates/dojo-core/tests/src/executor.cairo @@ -0,0 +1,42 @@ +use core::traits::Into; +use core::result::ResultTrait; +use array::ArrayTrait; +use option::OptionTrait; +use traits::TryInto; + +use starknet::syscalls::deploy_syscall; +use starknet::class_hash::Felt252TryIntoClassHash; +use dojo_core::interfaces::IExecutorDispatcher; +use dojo_core::interfaces::IExecutorDispatcherTrait; +use dojo_core::executor::Executor; + +#[derive(Component)] +struct Foo { + a: felt252, + b: u128, +} + +#[system] +mod Bar { + use super::Foo; + + fn execute(foo: Foo) -> Foo { + foo + } +} + +#[test] +#[available_gas(30000000)] +fn test_executor() { + let constructor_calldata = array::ArrayTrait::::new(); + let (executor_address, _) = deploy_syscall( + Executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false + ).unwrap(); + + let executor = IExecutorDispatcher { contract_address: executor_address }; + + let mut system_calldata = ArrayTrait::new(); + system_calldata.append(42); + system_calldata.append(53); + let res = executor.execute(BarSystem::TEST_CLASS_HASH.try_into().unwrap(), system_calldata.span()); +} diff --git a/crates/dojo-core/tests/src/lib.cairo b/crates/dojo-core/tests/src/lib.cairo new file mode 100644 index 0000000000..5485a903e4 --- /dev/null +++ b/crates/dojo-core/tests/src/lib.cairo @@ -0,0 +1,4 @@ +mod executor; +mod storage; +mod world; +mod world_factory; \ No newline at end of file diff --git a/crates/dojo-core/tests/src/storage.cairo b/crates/dojo-core/tests/src/storage.cairo new file mode 100644 index 0000000000..769eaba8a2 --- /dev/null +++ b/crates/dojo-core/tests/src/storage.cairo @@ -0,0 +1,2 @@ +mod index; +mod query; diff --git a/crates/dojo-core/tests/src/storage/index.cairo b/crates/dojo-core/tests/src/storage/index.cairo new file mode 100644 index 0000000000..1e7e89d718 --- /dev/null +++ b/crates/dojo-core/tests/src/storage/index.cairo @@ -0,0 +1,66 @@ +use array::ArrayTrait; +use traits::Into; + +use dojo_core::integer::u250; +use dojo_core::storage::index::Index; + +#[test] +#[available_gas(2000000)] +fn test_index_entity() { + let no_query = Index::query(69.into()); + assert(no_query.len() == 0_usize, 'entity indexed'); + + Index::create(69.into(), 420.into()); + let query = Index::query(69.into()); + assert(query.len() == 1_usize, 'entity not indexed'); + assert(*query.at(0_usize) == 420.into(), 'entity value incorrect'); + + Index::create(69.into(), 420.into()); + let noop_query = Index::query(69.into()); + assert(noop_query.len() == 1_usize, 'index should be noop'); + + Index::create(69.into(), 1337.into()); + let two_query = Index::query(69.into()); + assert(two_query.len() == 2_usize, 'index should have two query'); + assert(*two_query.at(1_usize) == 1337.into(), 'entity value incorrect'); +} + +#[test] +#[available_gas(2000000)] +fn test_entity_delete_basic() { + Index::create(69.into(), 420.into()); + let query = Index::query(69.into()); + assert(query.len() == 1_usize, 'entity not indexed'); + assert(*query.at(0_usize) == 420.into(), 'entity value incorrect'); + + assert(Index::exists(69.into(), 420.into()), 'entity should exist'); + + Index::delete(69.into(), 420.into()); + + assert(!Index::exists(69.into(), 420.into()), 'entity should not exist'); + let no_query = Index::query(69.into()); + assert(no_query.len() == 0_usize, 'index should have no query'); +} + +#[test] +#[available_gas(20000000)] +fn test_entity_query_delete_shuffle() { + let table = 1.into(); + Index::create(table, 10.into()); + Index::create(table, 20.into()); + Index::create(table, 30.into()); + assert(Index::query(table).len() == 3_usize, 'wrong size'); + + Index::delete(table, 10.into()); + let entities = Index::query(table); + assert(entities.len() == 2_usize, 'wrong size'); + assert(*entities.at(0_usize) == 30.into(), 'idx 0 not 30'); + assert(*entities.at(1_usize) == 20.into(), 'idx 1 not 20'); +} + +#[test] +#[available_gas(20000000)] +fn test_entity_query_delete_non_existing() { + assert(Index::query(69.into()).len() == 0_usize, 'table len != 0'); + Index::delete(69.into(), 999.into()); // deleting non-existing should not panic +} diff --git a/crates/dojo-core/tests/src/storage/query.cairo b/crates/dojo-core/tests/src/storage/query.cairo new file mode 100644 index 0000000000..0c2a1aaf4d --- /dev/null +++ b/crates/dojo-core/tests/src/storage/query.cairo @@ -0,0 +1,55 @@ +use array::ArrayTrait; +use array::SpanTrait; +use hash::LegacyHash; +use option::OptionTrait; +use serde::Serde; +use traits::Into; +use zeroable::IsZeroResult; +use starknet::ClassHashIntoFelt252; +use poseidon::poseidon_hash_span; +use dojo_core::integer::u250; +use dojo_core::integer::Felt252IntoU250; +use dojo_core::integer::U250IntoFelt252; +use dojo_core::serde::SpanSerde; +use dojo_core::storage::query::IntoPartitioned; +use dojo_core::storage::query::TupleSize2IntoQuery; +use dojo_core::storage::query::TupleSize3IntoQuery; +use dojo_core::storage::query::Query; +use dojo_core::storage::query::QueryTrait; + +#[test] +#[available_gas(2000000)] +fn test_query_id() { + let mut keys = ArrayTrait::new(); + keys.append(420.into()); + let query: u250 = QueryTrait::new(0, 0.into(), keys.span()).into(); + assert(query == 420.into(), 'Incorrect hash'); +} + +#[test] +#[available_gas(2000000)] +fn test_query_into() { + let query: Query = 420.into(); + assert(*query.keys.at(0_usize) == 420.into(), 'Incorrect query'); + let query1: Query = (69).into(); + assert(*query1.keys.at(0_usize) == 69.into(), 'Incorrect query'); + let query2: Query = (69, 420).into(); + assert(*query2.keys.at(0_usize) == 69.into(), 'Incorrect query'); + assert(*query2.keys.at(1_usize) == 420.into(), 'Incorrect query'); + let query3: Query = (69, 420, 777).into(); + assert(*query3.keys.at(0_usize) == 69.into(), 'Incorrect query'); + assert(*query3.keys.at(1_usize) == 420.into(), 'Incorrect query'); + assert(*query3.keys.at(2_usize) == 777.into(), 'Incorrect query'); +} + +#[test] +#[available_gas(2000000)] +fn test_partitioned_query_into() { + let query: Query = (69, (420, )).into_partitioned(); + assert(query.partition == 69.into(), 'Incorrect partition'); + assert(*query.keys.at(0_usize) == 420.into(), 'Incorrect query'); + + let query2: Query = (69, (420, 777)).into_partitioned(); + assert(query2.partition == 69.into(), 'Incorrect partition'); + assert(*query2.keys.at(1_usize) == 777.into(), 'Incorrect query'); +} diff --git a/crates/dojo-core/tests/src/world.cairo b/crates/dojo-core/tests/src/world.cairo new file mode 100644 index 0000000000..88f711fc80 --- /dev/null +++ b/crates/dojo-core/tests/src/world.cairo @@ -0,0 +1,77 @@ +use array::ArrayTrait; +use core::result::ResultTrait; +use traits::Into; +use traits::TryInto; +use option::OptionTrait; +use starknet::class_hash::Felt252TryIntoClassHash; +use starknet::syscalls::deploy_syscall; + +use dojo_core::integer::u250; +use dojo_core::integer::U32IntoU250; +use dojo_core::storage::query::QueryTrait; +use dojo_core::interfaces::IWorldDispatcher; +use dojo_core::interfaces::IWorldDispatcherTrait; +use dojo_core::executor::Executor; +use dojo_core::world::World; + +#[derive(Component)] +struct Foo { + a: felt252, + b: u128, +} + +#[test] +#[available_gas(2000000)] +fn test_component() { + let name = 'Foo'.into(); + World::register_component(FooComponent::TEST_CLASS_HASH.try_into().unwrap()); + let mut data = ArrayTrait::::new(); + data.append(1337); + let id = World::uuid(); + World::set_entity(name, QueryTrait::new_from_id(id.into()), 0_u8, data.span()); + let stored = World::entity(name, QueryTrait::new_from_id(id.into()), 0_u8, 1_usize); + assert(*stored.snapshot.at(0_usize) == 1337, 'data not stored'); +} + +#[system] +mod Bar { + use super::Foo; + + fn execute(foo: Foo) -> Foo { + foo + } +} + +#[test] +#[available_gas(2000000)] +fn test_system() { + let executor_constructor_calldata = array::ArrayTrait::::new(); + let (executor_address, _) = deploy_syscall( + Executor::TEST_CLASS_HASH.try_into().unwrap(), 0, executor_constructor_calldata.span(), false + ).unwrap(); + + let mut constructor_calldata = array::ArrayTrait::::new(); + constructor_calldata.append('World'); + constructor_calldata.append(executor_address.into()); + let (world_address, _) = deploy_syscall( + World::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false + ).unwrap(); + let world = IWorldDispatcher { contract_address: world_address }; + + world.register_system(BarSystem::TEST_CLASS_HASH.try_into().unwrap()); + let mut data = ArrayTrait::::new(); + data.append(1337); + data.append(1337); + let id = world.uuid(); + world.execute('Bar'.into(), data.span()); +} + +#[test] +#[available_gas(2000000)] +fn test_constructor() { + starknet::testing::set_caller_address(starknet::contract_address_const::<0x420>()); + World::constructor( + 'World'.into(), + starknet::contract_address_const::<0x1337>(), + ); +} diff --git a/crates/dojo-core/tests/src/world_factory.cairo b/crates/dojo-core/tests/src/world_factory.cairo new file mode 100644 index 0000000000..3193c59764 --- /dev/null +++ b/crates/dojo-core/tests/src/world_factory.cairo @@ -0,0 +1,71 @@ + +use core::traits::Into; +use core::result::ResultTrait; +use array::ArrayTrait; +use option::OptionTrait; +use traits::TryInto; + +use starknet::syscalls::deploy_syscall; +use starknet::class_hash::ClassHash; +use starknet::class_hash::Felt252TryIntoClassHash; +use dojo_core::interfaces::IWorldDispatcher; +use dojo_core::interfaces::IWorldDispatcherTrait; +use dojo_core::executor::Executor; +use dojo_core::world::World; +use dojo_core::world_factory::WorldFactory; + +#[derive(Component)] +struct Foo { + a: felt252, + b: u128, +} + +#[system] +mod Bar { + use super::Foo; + + fn execute(foo: Foo) -> Foo { + foo + } +} + +#[test] +#[available_gas(2000000)] +fn test_constructor() { + WorldFactory::constructor(starknet::class_hash_const::<0x420>(), starknet::contract_address_const::<0x69>()); + let world_class_hash = WorldFactory::world(); + assert(world_class_hash == starknet::class_hash_const::<0x420>(), 'wrong world class hash'); + let executor_address = WorldFactory::executor(); + assert(executor_address == starknet::contract_address_const::<0x69>(), 'wrong executor contract'); +} + +#[test] +#[available_gas(30000000)] +fn test_spawn_world() { + let constructor_calldata = array::ArrayTrait::::new(); + let (executor_address, _) = deploy_syscall( + Executor::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_calldata.span(), false + ).unwrap(); + + WorldFactory::constructor(World::TEST_CLASS_HASH.try_into().unwrap(), executor_address); + assert(WorldFactory::executor() == executor_address, 'wrong executor address'); + assert(WorldFactory::world() == World::TEST_CLASS_HASH.try_into().unwrap(), 'wrong world class hash'); + + let mut systems = array::ArrayTrait::::new(); + systems.append(BarSystem::TEST_CLASS_HASH.try_into().unwrap()); + + let mut components = array::ArrayTrait::::new(); + components.append(FooComponent::TEST_CLASS_HASH.try_into().unwrap()); + + let world_address = WorldFactory::spawn('TestWorld'.into(), components, systems); + + let foo_hash = IWorldDispatcher { + contract_address: world_address + }.component('Foo'.into()); + assert(foo_hash == FooComponent::TEST_CLASS_HASH.try_into().unwrap(), 'component not registered'); + + let bar_hash = IWorldDispatcher { + contract_address: world_address + }.system('Bar'.into()); + assert(bar_hash == BarSystem::TEST_CLASS_HASH.try_into().unwrap(), 'system not registered'); +} \ No newline at end of file diff --git a/scripts/cairo_test.sh b/scripts/cairo_test.sh index 097eb3f44d..82a58f9f28 100755 --- a/scripts/cairo_test.sh +++ b/scripts/cairo_test.sh @@ -1,9 +1,9 @@ #!/bin/bash set -euxo pipefail -cargo +nightly-2022-11-03 run --bin dojo-test -- crates/dojo-core -# Uncomment once erc crate passes +cargo +nightly-2022-11-03 run --bin dojo-test -- crates/dojo-core/tests cargo +nightly-2022-11-03 run --bin dojo-test -- crates/dojo-defi +# Uncomment once erc crate passes # cargo +nightly-2022-11-03 run --bin dojo-test -- crates/dojo-erc # cargo +nightly-2022-11-03 run --bin dojo-test -- crates/dojo-physics cargo +nightly-2022-11-03 run --bin dojo-test -- examples