Skip to content

Commit

Permalink
⬆️ Upgrade done
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Oct 31, 2024
1 parent 647a0ad commit 8617e2f
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 319 deletions.
4 changes: 2 additions & 2 deletions packages/achievement/src/events/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use achievement::types::task::Task;

#[derive(Clone, Drop, Serde, Introspect, starknet::Event)]
#[derive(Clone, Drop, Serde, Introspect)]
#[dojo::event]
pub struct Trophy {
#[key]
Expand All @@ -20,7 +20,7 @@ pub struct Trophy {
data: ByteArray,
}

#[derive(Copy, Drop, Serde, Introspect, starknet::Event)]
#[derive(Copy, Drop, Serde, Introspect)]
#[dojo::event]
pub struct Progress {
#[key]
Expand Down
42 changes: 21 additions & 21 deletions packages/achievement/src/tests/setup.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -63,41 +63,41 @@ mod setup {
TestResource::Event(events::e_Trophy::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Event(events::e_Progress::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Contract(
ContractDefTrait::new(Achiever::TEST_CLASS_HASH, "achiever")
ContractDefTrait::new(Achiever::TEST_CLASS_HASH, "Achiever")
.with_writer_of([dojo::utils::bytearray_hash(@"namespace")].span())
),
TestResource::Contract(
ContractDefTrait::new(Controller::TEST_CLASS_HASH, "controller")
ContractDefTrait::new(Controller::TEST_CLASS_HASH, "Controller")
.with_writer_of([dojo::utils::bytearray_hash(@"namespace")].span())
),
TestResource::Contract(
ContractDefTrait::new(Registrer::TEST_CLASS_HASH, "registrer")
ContractDefTrait::new(Registrer::TEST_CLASS_HASH, "Registrer")
.with_writer_of([dojo::utils::bytearray_hash(@"namespace")].span())
),
].span()
}
}

#[inline]
fn spawn_game() { // -> (WorldStorage, Systems, Context) {
fn spawn_game() -> (WorldStorage, Systems, Context) {
// [Setup] World
// set_contract_address(OWNER());
set_contract_address(OWNER());
let namespace_def = setup_namespace();
let _world = spawn_test_world([namespace_def].span());
// // [Setup] Systems
// let (achiever_address, _) = world.dns(@"achiever").unwrap();
// let (controller_address, _) = world.dns(@"controller").unwrap();
// let (registrer_address, _) = world.dns(@"registrer").unwrap();
// let systems = Systems {
// achiever: IAchieverDispatcher { contract_address: achiever_address },
// controller: IControllerDispatcher { contract_address: controller_address },
// registrer: IRegistrerDispatcher { contract_address: registrer_address },
// };

// // [Setup] Context
// let context = Context { player_id: PLAYER().into() };

// // [Return]
// (world, systems, context)
let world = spawn_test_world([namespace_def].span());
// [Setup] Systems
let (achiever_address, _) = world.dns(@"Achiever").unwrap();
let (controller_address, _) = world.dns(@"Controller").unwrap();
let (registrer_address, _) = world.dns(@"Registrer").unwrap();
let systems = Systems {
achiever: IAchieverDispatcher { contract_address: achiever_address },
controller: IControllerDispatcher { contract_address: controller_address },
registrer: IRegistrerDispatcher { contract_address: registrer_address },
};

// [Setup] Context
let context = Context { player_id: PLAYER().into() };

// [Return]
(world, systems, context)
}
}
80 changes: 50 additions & 30 deletions packages/achievement/src/tests/test_achievable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use starknet::ContractAddress;
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use starknet::testing;

// Dojo imports

use dojo::world::world::Event;

// Internal imports

use achievement::types::task::{Task, TaskTrait};
Expand All @@ -33,35 +37,51 @@ const TITLE: felt252 = 'Title';
#[test]
fn test_achievable_create() {
spawn_game();
// let (_world, _systems, _context) = spawn_game();
// clear_events(world.dispatcher.contract_address);
// let tasks = array![TaskTrait::new(TASK_ID, TOTAL, "Description")].span();
// systems
// .achiever
// .create(TROPHY_ID, HIDDEN, INDEX, POINTS, GROUP, ICON, TITLE, "Description", tasks, "");
// let event = starknet::testing::pop_log::<Trophy>(world.dispatcher.contract_address).unwrap();
// // FIXME: Cannot check keys because they are shifted due to dojo macros
// assert_eq!(event.hidden, HIDDEN);
// assert_eq!(event.index, INDEX);
// assert_eq!(event.points, POINTS);
// assert_eq!(event.group, GROUP);
// assert_eq!(event.title, TITLE);
// assert_eq!(event.description, "Description");
// assert_eq!(event.icon, ICON);
// assert_eq!(event.tasks.len(), 1);
// assert_eq!(event.tasks[0].id, @TASK_ID);
// assert_eq!(event.tasks[0].total, @TOTAL);
// assert_eq!(event.tasks[0].description, @"Description");
let (world, systems, _context) = spawn_game();
clear_events(world.dispatcher.contract_address);
let tasks = array![TaskTrait::new(TASK_ID, TOTAL, "Description")].span();
systems
.achiever
.create(TROPHY_ID, HIDDEN, INDEX, POINTS, GROUP, ICON, TITLE, "Description", tasks, "");
let contract_event = starknet::testing::pop_log::<Event>(world.dispatcher.contract_address)
.unwrap();
match contract_event {
Event::EventEmitted(event) => {
assert_eq!(*event.keys.at(0), TROPHY_ID);
assert_eq!(*event.values.at(0), 0);
assert_eq!(*event.values.at(1), INDEX.into());
assert_eq!(*event.values.at(2), POINTS.into());
assert_eq!(*event.values.at(3), GROUP.into());
assert_eq!(*event.values.at(4), ICON.into());
assert_eq!(*event.values.at(5), TITLE.into());
assert_eq!(*event.values.at(6), 0);
assert_eq!(*event.values.at(7), 'Description');
assert_eq!(*event.values.at(8), 11);
assert_eq!(*event.values.at(9), 1);
assert_eq!(*event.values.at(10), TASK_ID);
assert_eq!(*event.values.at(11), TOTAL.into());
assert_eq!(*event.values.at(12), 0);
assert_eq!(*event.values.at(13), 'Description');
assert_eq!(*event.values.at(14), 11);
},
_ => {},
}
}
#[test]
fn test_achievable_update() {
let (world, systems, context) = spawn_game();
clear_events(world.dispatcher.contract_address);
systems.achiever.update(context.player_id, TASK_ID, COUNT);
let contract_event = starknet::testing::pop_log::<Event>(world.dispatcher.contract_address)
.unwrap();
match contract_event {
Event::EventEmitted(event) => {
assert_eq!(*event.keys.at(0), context.player_id);
assert_eq!(*event.keys.at(1), TASK_ID);
assert_eq!(*event.values.at(0), COUNT.into());
assert_eq!(*event.values.at(1), 0);
},
_ => {},
}
}
// #[test]
// fn test_achievable_update() {
// let (world, systems, context) = spawn_game();
// clear_events(world.dispatcher.contract_address);
// systems.achiever.update(context.player_id, TASK_ID, COUNT);
// let event =
// starknet::testing::pop_log::<Progress>(world.dispatcher.contract_address).unwrap();
// // FIXME: Cannot check keys because they are shifted due to dojo macros
// assert_eq!(event.count, COUNT);
// assert_eq!(event.time, 0);
// }

108 changes: 54 additions & 54 deletions packages/achievement/src/tests/test_controllable.cairo
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
// // Core imports

// use core::num::traits::Zero;

// // Starknet imports

// use starknet::ContractAddress;
// use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
// use starknet::testing;

// // Internal imports

// use achievement::store::{Store, StoreTrait};
// use achievement::models::game::{Game, GameTrait};
// use achievement::models::achievement::{Achievement, AchievementTrait};
// use achievement::tests::mocks::controller::{
// Controller, IControllerDispatcher, IControllerDispatcherTrait
// };
// use achievement::tests::setup::setup::{spawn_game, Systems, Context, OWNER, PLAYER};

// // Constants

// const WORLD_ADDRESS: felt252 = 'WORLD';
// const NAMEPSACE: felt252 = 'NAMESPACE';

// // Tests

// #[test]
// fn test_controllable_assert_is_owner() {
// // [Setup]
// let (world, systems, _context) = spawn_game();
// let mut store = StoreTrait::new(world);
// let game = GameTrait::new(
// WORLD_ADDRESS, NAMEPSACE, "NAME", "DESCRIPTION", "TORII_URL", "IMAGE_URI", OWNER().into()
// );
// store.set_game(game);
// // [Assert]
// systems.controller.assert_is_owner(WORLD_ADDRESS, NAMEPSACE);
// }

// #[test]
// #[should_panic(expected: ('Controllable: unauthorized call', 'ENTRYPOINT_FAILED'))]
// fn test_controllable_assert_is_owner_revert() {
// // [Setup]
// let (world, systems, _context) = spawn_game();
// let mut store = StoreTrait::new(world);
// let game = GameTrait::new(
// WORLD_ADDRESS, NAMEPSACE, "NAME", "DESCRIPTION", "TORII_URL", "IMAGE_URI", OWNER().into()
// );
// store.set_game(game);
// // [Assert]
// testing::set_contract_address(PLAYER());
// systems.controller.assert_is_owner(WORLD_ADDRESS, NAMEPSACE);
// }
// Core imports

use core::num::traits::Zero;

// Starknet imports

use starknet::ContractAddress;
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use starknet::testing;

// Internal imports

use achievement::store::{Store, StoreTrait};
use achievement::models::game::{Game, GameTrait};
use achievement::models::achievement::{Achievement, AchievementTrait};
use achievement::tests::mocks::controller::{
Controller, IControllerDispatcher, IControllerDispatcherTrait
};
use achievement::tests::setup::setup::{spawn_game, Systems, Context, OWNER, PLAYER};

// Constants

const WORLD_ADDRESS: felt252 = 'WORLD';
const NAMEPSACE: felt252 = 'NAMESPACE';

// Tests

#[test]
fn test_controllable_assert_is_owner() {
// [Setup]
let (world, systems, _context) = spawn_game();
let mut store = StoreTrait::new(world);
let game = GameTrait::new(
WORLD_ADDRESS, NAMEPSACE, "NAME", "DESCRIPTION", "TORII_URL", "IMAGE_URI", OWNER().into()
);
store.set_game(game);
// [Assert]
systems.controller.assert_is_owner(WORLD_ADDRESS, NAMEPSACE);
}

#[test]
#[should_panic(expected: ('Controllable: unauthorized call', 'ENTRYPOINT_FAILED'))]
fn test_controllable_assert_is_owner_revert() {
// [Setup]
let (world, systems, _context) = spawn_game();
let mut store = StoreTrait::new(world);
let game = GameTrait::new(
WORLD_ADDRESS, NAMEPSACE, "NAME", "DESCRIPTION", "TORII_URL", "IMAGE_URI", OWNER().into()
);
store.set_game(game);
// [Assert]
testing::set_contract_address(PLAYER());
systems.controller.assert_is_owner(WORLD_ADDRESS, NAMEPSACE);
}
Loading

0 comments on commit 8617e2f

Please sign in to comment.