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

add tests for engine and processors #2918

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions crates/dojo/core-cairo-test/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ mod tests {
test_contract, test_contract_with_dojo_init_args, Sword, Case, Character, Abilities,
Stats, Weapon, Ibar, IbarDispatcher, IbarDispatcherTrait, bar, deploy_world,
deploy_world_and_bar, deploy_world_and_foo, drop_all_events, IFooSetter,
IFooSetterDispatcher, IFooSetterDispatcherTrait, NotCopiable
IFooSetterDispatcher, IFooSetterDispatcherTrait, NotCopiable,
};

mod event;
pub use event::{
FooEventBadLayoutType, e_FooEventBadLayoutType, deploy_world_for_event_upgrades
FooEventBadLayoutType, e_FooEventBadLayoutType, deploy_world_for_event_upgrades,
};

mod model;
Expand Down
36 changes: 19 additions & 17 deletions crates/dojo/core-cairo-test/src/tests/benchmarks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::serde::Serde;
use starknet::{ContractAddress, SyscallResultTrait};
use starknet::storage_access::{
storage_base_address_from_felt252, storage_address_from_base,
storage_address_from_base_and_offset
storage_address_from_base_and_offset,
};
use starknet::syscalls::{storage_read_syscall, storage_write_syscall};

Expand Down Expand Up @@ -44,11 +44,13 @@ struct ComplexModel {

fn deploy_world() -> IWorldDispatcher {
let namespace_def = NamespaceDef {
namespace: "dojo", resources: [
namespace: "dojo",
resources: [
TestResource::Model(case::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Model(case_not_packed::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Model(complex_model::TEST_CLASS_HASH.try_into().unwrap()),
].span(),
]
.span(),
};

spawn_test_world([namespace_def].span())
Expand Down Expand Up @@ -184,7 +186,7 @@ fn bench_simple_struct() {
let caller = starknet::contract_address_const::<0x42>();

let gas = GasCounterTrait::start();
let mut foo = Foo { caller, a: 0x123456789abcdef, b: 0x123456789abcdef, };
let mut foo = Foo { caller, a: 0x123456789abcdef, b: 0x123456789abcdef };
gas.end("foo init");

let gas = GasCounterTrait::start();
Expand Down Expand Up @@ -285,7 +287,7 @@ fn bench_nested_struct_packed() {

let gas = GasCounterTrait::start();
let mut case = Case {
owner: caller, sword: Sword { swordsmith: caller, damage: 0x12345678, }, material: 'wooden',
owner: caller, sword: Sword { swordsmith: caller, damage: 0x12345678 }, material: 'wooden',
};
gas.end("case init");

Expand Down Expand Up @@ -360,8 +362,8 @@ fn bench_complex_struct_packed() {
},
Sword {
swordsmith: starknet::contract_address_const::<0x69>(), damage: 0x12345678,
}
)
},
),
),
gold: 0x12345678,
};
Expand Down Expand Up @@ -412,11 +414,11 @@ fn test_benchmark_set_entity() {
let bob = starknet::contract_address_const::<0xb0b>();

let simple_entity_packed = Case {
owner: bob, sword: Sword { swordsmith: bob, damage: 42, }, material: 'iron'
owner: bob, sword: Sword { swordsmith: bob, damage: 42 }, material: 'iron',
};

let simple_entity_not_packed = CaseNotPacked {
owner: bob, sword: Sword { swordsmith: bob, damage: 42, }, material: 'iron'
owner: bob, sword: Sword { swordsmith: bob, damage: 42 }, material: 'iron',
};

let complex_entity = ComplexModel {
Expand All @@ -426,11 +428,11 @@ fn test_benchmark_set_entity() {
last_name: "Doe",
weapons: array![
Weapon::DualWield(
(Sword { swordsmith: bob, damage: 42 }, Sword { swordsmith: bob, damage: 800 })
(Sword { swordsmith: bob, damage: 42 }, Sword { swordsmith: bob, damage: 800 }),
),
Weapon::Fists(
(Sword { swordsmith: bob, damage: 300 }, Sword { swordsmith: bob, damage: 1200 })
)
(Sword { swordsmith: bob, damage: 300 }, Sword { swordsmith: bob, damage: 1200 }),
),
],
abilities: (
Abilities {
Expand All @@ -448,7 +450,7 @@ fn test_benchmark_set_entity() {
intelligence: 2,
wisdom: 1,
charisma: 43,
}
},
),
stats: Stats {
kills: 99,
Expand All @@ -459,7 +461,7 @@ fn test_benchmark_set_entity() {
walked: 12,
runned: 32,
finished: true,
romances: 65
romances: 65,
},
};

Expand All @@ -469,7 +471,7 @@ fn test_benchmark_set_entity() {
model_selector: Model::<Case>::selector(DOJO_NSH),
index: ModelIndex::Keys(simple_entity_packed.serialized_keys()),
values: simple_entity_packed.serialized_values(),
layout: Model::<Case>::layout()
layout: Model::<Case>::layout(),
);
gas.end("World::SetEntity::SimplePacked");

Expand All @@ -479,7 +481,7 @@ fn test_benchmark_set_entity() {
model_selector: Model::<CaseNotPacked>::selector(),
index: ModelIndex::Keys(simple_entity_not_packed.serialized_keys()),
values: simple_entity_not_packed.serialized_values(),
layout: Model::<CaseNotPacked>::layout()
layout: Model::<CaseNotPacked>::layout(),
);
gas.end("World::SetEntity::SimpleNotPacked");

Expand All @@ -489,7 +491,7 @@ fn test_benchmark_set_entity() {
model_selector: Model::<ComplexModel>::selector(),
index: ModelIndex::Keys(complex_entity.keys()),
values: complex_entity.serialized_values(),
layout: Model::<ComplexModel>::layout()
layout: Model::<ComplexModel>::layout(),
);
gas.end("World::SetEntity::ComplexModel");

Expand Down
2 changes: 1 addition & 1 deletion crates/dojo/core-cairo-test/src/tests/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod invalid_model_world {
expected: (
"Namespace `` is invalid according to Dojo naming rules: ^[a-zA-Z0-9_]+$",
'ENTRYPOINT_FAILED',
)
),
)]
fn test_register_namespace_empty_name() {
let world = deploy_world();
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo/core-cairo-test/src/tests/event/event.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct FooEvent {
#[key]
k2: felt252,
v1: u128,
v2: u32
v2: u32,
}

#[test]
Expand Down
8 changes: 5 additions & 3 deletions crates/dojo/core-cairo-test/src/tests/helpers/event.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ struct FooEventMemberAdded {

pub fn deploy_world_for_event_upgrades() -> IWorldDispatcher {
let namespace_def = NamespaceDef {
namespace: "dojo", resources: [
namespace: "dojo",
resources: [
TestResource::Event(old_foo_event_bad_layout_type::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Event(e_FooEventMemberRemoved::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Event(
e_FooEventMemberAddedButRemoved::TEST_CLASS_HASH.try_into().unwrap()
e_FooEventMemberAddedButRemoved::TEST_CLASS_HASH.try_into().unwrap(),
),
TestResource::Event(e_FooEventMemberAddedButMoved::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Event(e_FooEventMemberAdded::TEST_CLASS_HASH.try_into().unwrap()),
].span()
]
.span(),
};
spawn_test_world([namespace_def].span()).dispatcher
}
Expand Down
20 changes: 12 additions & 8 deletions crates/dojo/core-cairo-test/src/tests/helpers/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dojo::world::{IWorldDispatcher, WorldStorage, WorldStorageTrait};
use dojo::model::Model;

use crate::world::{
spawn_test_world, NamespaceDef, TestResource, ContractDefTrait, WorldStorageTestTrait
spawn_test_world, NamespaceDef, TestResource, ContractDefTrait, WorldStorageTestTrait,
};

pub const DOJO_NSH: felt252 = 0x309e09669bc1fdc1dd6563a7ef862aa6227c97d099d08cc7b81bad58a7443fa;
Expand Down Expand Up @@ -164,7 +164,7 @@ pub struct Stats {
#[derive(IntrospectPacked, Copy, Drop, Serde)]
pub enum Weapon {
DualWield: (Sword, Sword),
Fists: (Sword, Sword), // Introspect requires same arms
Fists: (Sword, Sword) // Introspect requires same arms
}

#[starknet::interface]
Expand Down Expand Up @@ -196,7 +196,7 @@ pub mod bar {
fn delete_foo(self: @ContractState) {
let mut world = self.world(@"dojo");
let ptr = ModelPtr::<
Foo
Foo,
> { id: core::poseidon::poseidon_hash_span([get_caller_address().into()].span()) };
world.erase_model_ptr(ptr);
}
Expand All @@ -205,7 +205,7 @@ pub mod bar {

/// Deploys an empty world with the `dojo` namespace.
pub fn deploy_world() -> WorldStorage {
let namespace_def = NamespaceDef { namespace: "dojo", resources: [].span(), };
let namespace_def = NamespaceDef { namespace: "dojo", resources: [].span() };

spawn_test_world([namespace_def].span())
}
Expand All @@ -214,10 +214,12 @@ pub fn deploy_world() -> WorldStorage {
/// No permissions are granted.
pub fn deploy_world_and_foo() -> (WorldStorage, felt252) {
let namespace_def = NamespaceDef {
namespace: "dojo", resources: [
namespace: "dojo",
resources: [
TestResource::Model(m_Foo::TEST_CLASS_HASH),
TestResource::Model(m_NotCopiable::TEST_CLASS_HASH),
].span(),
]
.span(),
};

(spawn_test_world([namespace_def].span()), Model::<Foo>::selector(DOJO_NSH))
Expand All @@ -227,10 +229,12 @@ pub fn deploy_world_and_foo() -> (WorldStorage, felt252) {
/// Grants the `bar` contract writer permissions to the `foo` model.
pub fn deploy_world_and_bar() -> (WorldStorage, IbarDispatcher) {
let namespace_def = NamespaceDef {
namespace: "dojo", resources: [
namespace: "dojo",
resources: [
TestResource::Model(m_Foo::TEST_CLASS_HASH),
TestResource::Contract(bar::TEST_CLASS_HASH),
].span(),
]
.span(),
};

let bar_def = ContractDefTrait::new(@"dojo", @"bar")
Expand Down
8 changes: 5 additions & 3 deletions crates/dojo/core-cairo-test/src/tests/helpers/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ struct FooModelMemberAdded {

pub fn deploy_world_for_model_upgrades() -> IWorldDispatcher {
let namespace_def = NamespaceDef {
namespace: "dojo", resources: [
namespace: "dojo",
resources: [
TestResource::Model(m_FooModelBadLayoutType::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Model(m_FooModelMemberRemoved::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Model(
m_FooModelMemberAddedButRemoved::TEST_CLASS_HASH.try_into().unwrap()
m_FooModelMemberAddedButRemoved::TEST_CLASS_HASH.try_into().unwrap(),
),
TestResource::Model(m_FooModelMemberAddedButMoved::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Model(m_FooModelMemberAdded::TEST_CLASS_HASH.try_into().unwrap()),
].span()
]
.span(),
};
spawn_test_world([namespace_def].span()).dispatcher
}
34 changes: 17 additions & 17 deletions crates/dojo/core-cairo-test/src/tests/meta/introspect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,70 @@ struct Base {
#[derive(Drop, Introspect)]
struct WithArray {
value: u32,
arr: Array<u8>
arr: Array<u8>,
}

#[derive(Drop, Introspect)]
struct WithByteArray {
value: u32,
arr: ByteArray
arr: ByteArray,
}

#[derive(Drop, Introspect)]
struct WithTuple {
value: u32,
arr: (u8, u16, u32)
arr: (u8, u16, u32),
}

#[derive(Drop, Introspect)]
struct WithNestedTuple {
value: u32,
arr: (u8, (u16, u128, u256), u32)
arr: (u8, (u16, u128, u256), u32),
}

#[derive(Drop, Introspect)]
struct WithNestedArrayInTuple {
value: u32,
arr: (u8, (u16, Array<u128>, u256), u32)
arr: (u8, (u16, Array<u128>, u256), u32),
}

#[derive(Drop, IntrospectPacked)]
struct Vec3 {
x: u32,
y: u32,
z: u32
z: u32,
}

#[derive(IntrospectPacked)]
struct Translation {
from: Vec3,
to: Vec3
to: Vec3,
}

#[derive(Drop, IntrospectPacked)]
struct StructInnerNotPacked {
x: Base
x: Base,
}

#[derive(Drop, Introspect)]
enum EnumNoData {
One,
Two,
Three
Three,
}

#[derive(Drop, Introspect)]
enum EnumWithSameData {
One: u256,
Two: u256,
Three: u256
Three: u256,
}

#[derive(Drop, Introspect)]
enum EnumWithSameTupleData {
One: (u256, u32),
Two: (u256, u32),
Three: (u256, u32)
Three: (u256, u32),
}

#[derive(Drop, Introspect)]
Expand Down Expand Up @@ -103,7 +103,7 @@ enum EnumInnerNotPacked {

#[derive(Drop, Introspect)]
struct StructWithOption {
x: Option<u16>
x: Option<u16>,
}

#[derive(Drop, Introspect)]
Expand Down Expand Up @@ -135,7 +135,7 @@ fn _enum(values: Array<Option<Layout>>) -> Layout {
let v = *values.at(i);
match v {
Option::Some(v) => { items.append(field(i.into(), v)); },
Option::None => { items.append(field(i.into(), fixed(array![]))) }
Option::None => { items.append(field(i.into(), fixed(array![]))) },
}

i += 1;
Expand Down Expand Up @@ -230,8 +230,8 @@ fn test_layout_of_enum_without_variant_data() {
let layout = Introspect::<EnumNoData>::layout();
let expected = _enum(array![ // One
Option::None, // Two
Option::None, // Three
Option::None,]);
Option::None, // Three
Option::None]);

assert!(layout == expected);
}
Expand All @@ -247,7 +247,7 @@ fn test_layout_of_enum_with_variant_data() {
Option::Some(tuple(array![fixed(array![8]), fixed(array![16])])),
// Three
Option::Some(arr(fixed(array![128]))),
]
],
);

assert!(layout == expected);
Expand All @@ -258,7 +258,7 @@ fn test_layout_of_struct_with_option() {
let layout = Introspect::<StructWithOption>::layout();
let expected = Layout::Struct(
array![field(selector!("x"), _enum(array![Option::Some(fixed(array![16])), Option::None]))]
.span()
.span(),
);

assert!(layout == expected);
Expand Down
Loading