From ad692f4abd1274db5285f0f202ed9f72c7f02eb1 Mon Sep 17 00:00:00 2001 From: Charlotte Date: Wed, 21 Aug 2024 09:43:07 +0800 Subject: [PATCH] regestery test --- .../src/components/registry/interface.cairo | 2 +- .../src/components/registry/registry.cairo | 10 ++--- .../components/registry/registry_mock.cairo | 3 +- .../components/registry/registry_test.cairo | 42 ++++++++++++++++++- contracts/src/lib.cairo | 2 +- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/contracts/src/components/registry/interface.cairo b/contracts/src/components/registry/interface.cairo index 60c5304..d984d0d 100644 --- a/contracts/src/components/registry/interface.cairo +++ b/contracts/src/components/registry/interface.cairo @@ -2,7 +2,7 @@ use core::hash::HashStateExTrait; use starknet::ContractAddress; use zkramp::utils::hash::HashSerializable; -#[derive(Drop, Serde, Clone)] +#[derive(Drop, Serde, Clone, Debug, PartialEq)] pub enum OffchainId { Revolut: ByteArray } diff --git a/contracts/src/components/registry/registry.cairo b/contracts/src/components/registry/registry.cairo index d3b301e..24f43c6 100644 --- a/contracts/src/components/registry/registry.cairo +++ b/contracts/src/components/registry/registry.cairo @@ -28,16 +28,16 @@ pub mod RegistryComponent { // #[event] - #[derive(Drop, starknet::Event)] + #[derive(Drop, Debug, PartialEq, starknet::Event)] pub enum Event { RegistrationEvent: RegistrationEvent, } - #[derive(Drop, starknet::Event)] + #[derive(Drop, Debug, PartialEq, starknet::Event)] pub struct RegistrationEvent { #[key] - caller: ContractAddress, - offchain_id: OffchainId, + pub caller: ContractAddress, + pub offchain_id: OffchainId, } // @@ -68,7 +68,7 @@ pub mod RegistryComponent { self.Registry_registrations.write((caller, offchain_id.clone()), true); // emit registration event - self.emit(RegistrationEvent { caller : caller, offchain_id : offchain_id }); + self.emit(RegistrationEvent { caller: caller, offchain_id: offchain_id }); } } } diff --git a/contracts/src/components/registry/registry_mock.cairo b/contracts/src/components/registry/registry_mock.cairo index b5beece..bda9897 100644 --- a/contracts/src/components/registry/registry_mock.cairo +++ b/contracts/src/components/registry/registry_mock.cairo @@ -39,8 +39,7 @@ pub mod RegistryMock { // #[constructor] - fn constructor(ref self: ContractState) { - // Nothing to be done + fn constructor(ref self: ContractState) { // Nothing to be done } } diff --git a/contracts/src/components/registry/registry_test.cairo b/contracts/src/components/registry/registry_test.cairo index adfda4d..6677f04 100644 --- a/contracts/src/components/registry/registry_test.cairo +++ b/contracts/src/components/registry/registry_test.cairo @@ -1,12 +1,18 @@ +use core::starknet::contract_address_const; +use starknet::testing::{set_caller_address, set_contract_address}; use zkramp::components::registry::interface::{IRegistryDispatcher, IRegistryDispatcherTrait}; +use zkramp::components::registry::registry::{RegistryComponent::{Event, RegistrationEvent},}; use zkramp::components::registry::registry_mock::RegistryMock; -use zkramp::tests::utils; use zkramp::tests::constants; +use zkramp::tests::utils; + /// Deploys the registry mock contract. fn setup_contracts() -> IRegistryDispatcher { // deploy registry - let registry_contract_address = utils::deploy(RegistryMock::TEST_CLASS_HASH, calldata: array![]); + let registry_contract_address = utils::deploy( + RegistryMock::TEST_CLASS_HASH, calldata: array![] + ); IRegistryDispatcher { contract_address: registry_contract_address } } @@ -15,6 +21,38 @@ fn setup_contracts() -> IRegistryDispatcher { // Externals // +#[test] +fn test_is_registered() { + set_contract_address(contract_address_const::<'caller'>()); + let registry = setup_contracts(); + + registry.register(offchain_id: constants::REVOLUT_ID()); + + assert_eq!( + registry.is_registered(contract_address_const::<'caller'>(), constants::REVOLUT_ID()), true + ); +} + +#[test] +fn test_registration_event() { + set_contract_address(contract_address_const::<'caller'>()); + let registry = setup_contracts(); + + registry.register(offchain_id: constants::REVOLUT_ID()); + + assert_eq!( + starknet::testing::pop_log(registry.contract_address), + Option::Some( + Event::RegistrationEvent( + RegistrationEvent { + caller: contract_address_const::<'caller'>(), + offchain_id: constants::REVOLUT_ID() + } + ) + ) + ); +} + #[test] #[should_panic(expected: ('Caller is the zero address', 'ENTRYPOINT_FAILED'))] fn test_register_from_zero() { diff --git a/contracts/src/lib.cairo b/contracts/src/lib.cairo index c89034e..386364e 100644 --- a/contracts/src/lib.cairo +++ b/contracts/src/lib.cairo @@ -1,6 +1,6 @@ pub mod components; pub mod contracts; -pub mod utils; #[cfg(test)] pub mod tests; +pub mod utils;