Skip to content

Commit

Permalink
Move core tests to avoid running them in dojo projects (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev authored May 1, 2023
1 parent f9fc0cc commit aa7dd3e
Show file tree
Hide file tree
Showing 15 changed files with 327 additions and 307 deletions.
44 changes: 0 additions & 44 deletions crates/dojo-core/src/executor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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::<felt252>::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());
}
}
69 changes: 0 additions & 69 deletions crates/dojo-core/src/storage/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
37 changes: 0 additions & 37 deletions crates/dojo-core/src/storage/query.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
80 changes: 0 additions & 80 deletions crates/dojo-core/src/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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::<felt252>::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::<felt252>::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::<felt252>::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::<felt252>::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>(),
);
}
}
75 changes: 0 additions & 75 deletions crates/dojo-core/src/world_factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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::<felt252>::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::<ClassHash>::new();
systems.append(BarSystem::TEST_CLASS_HASH.try_into().unwrap());

let mut components = array::ArrayTrait::<ClassHash>::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');
}
}
1 change: 1 addition & 0 deletions crates/dojo-core/tests/README.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions crates/dojo-core/tests/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dojo_core_tests"
version = "0.1.0"
description = "Tests for Dojo Core"

[dependencies]
dojo_core = { path = "../" }
42 changes: 42 additions & 0 deletions crates/dojo-core/tests/src/executor.cairo
Original file line number Diff line number Diff line change
@@ -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::<felt252>::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());
}
4 changes: 4 additions & 0 deletions crates/dojo-core/tests/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod executor;
mod storage;
mod world;
mod world_factory;
2 changes: 2 additions & 0 deletions crates/dojo-core/tests/src/storage.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod index;
mod query;
Loading

0 comments on commit aa7dd3e

Please sign in to comment.