From 9d2a47077865197c4de9aa032ad4136d166d592d Mon Sep 17 00:00:00 2001 From: Charlotte <49371958+chachaleo@users.noreply.github.com> Date: Mon, 26 Aug 2024 20:46:07 +0800 Subject: [PATCH] tests using snforge (#37) * tests using snforge * create `CALLER` test constant --------- Co-authored-by: 0xChqrles --- contracts/.snfoundry_cache/.prev_tests_failed | 0 contracts/Scarb.lock | 6 ++ contracts/Scarb.toml | 4 ++ .../components/registry/registry_mock.cairo | 8 ++- .../components/registry/registry_test.cairo | 69 ++++++++++--------- contracts/src/tests/constants.cairo | 5 ++ 6 files changed, 57 insertions(+), 35 deletions(-) create mode 100644 contracts/.snfoundry_cache/.prev_tests_failed diff --git a/contracts/.snfoundry_cache/.prev_tests_failed b/contracts/.snfoundry_cache/.prev_tests_failed new file mode 100644 index 0000000..e69de29 diff --git a/contracts/Scarb.lock b/contracts/Scarb.lock index b8b5b28..b78af83 100644 --- a/contracts/Scarb.lock +++ b/contracts/Scarb.lock @@ -87,9 +87,15 @@ name = "openzeppelin_utils" version = "0.15.0" source = "git+https://github.com/openzeppelin/cairo-contracts?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634" +[[package]] +name = "snforge_std" +version = "0.26.0" +source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.26.0#50eb589db65e113efe4f09241feb59b574228c7e" + [[package]] name = "zkramp" version = "0.1.0" dependencies = [ "openzeppelin", + "snforge_std", ] diff --git a/contracts/Scarb.toml b/contracts/Scarb.toml index adae74e..43d6c5a 100644 --- a/contracts/Scarb.toml +++ b/contracts/Scarb.toml @@ -11,6 +11,7 @@ openzeppelin = { git = "https://github.com/openzeppelin/cairo-contracts", tag = [dev-dependencies] cairo_test = "2.7.0" +snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.26.0" } [tool.fmt] sort-module-level-items = true @@ -18,3 +19,6 @@ sort-module-level-items = true [[target.starknet-contract]] sierra = true casm = true + +[scripts] +test = "snforge test" diff --git a/contracts/src/components/registry/registry_mock.cairo b/contracts/src/components/registry/registry_mock.cairo index bda9897..a1e7165 100644 --- a/contracts/src/components/registry/registry_mock.cairo +++ b/contracts/src/components/registry/registry_mock.cairo @@ -43,8 +43,10 @@ pub mod RegistryMock { } } -type ComponentState = RegistryComponent::ComponentState; +pub type ComponentState = RegistryComponent::ComponentState; -fn COMPONENT() -> ComponentState { - RegistryComponent::component_state_for_testing() +pub impl TestingStateDefault of Default { + fn default() -> ComponentState { + RegistryComponent::component_state_for_testing() + } } diff --git a/contracts/src/components/registry/registry_test.cairo b/contracts/src/components/registry/registry_test.cairo index 10ab8a2..30847eb 100644 --- a/contracts/src/components/registry/registry_test.cairo +++ b/contracts/src/components/registry/registry_test.cairo @@ -1,20 +1,18 @@ -use core::starknet::contract_address_const; -use starknet::testing::{set_caller_address, set_contract_address}; +use core::starknet::{ContractAddress, contract_address_const}; +use snforge_std::{ + declare, ContractClassTrait, ContractClass, start_cheat_caller_address, EventSpy, + EventSpyAssertionsTrait, spy_events, test_address, EventSpyTrait +}; use zkramp::components::registry::interface::{IRegistryDispatcher, IRegistryDispatcherTrait}; -use zkramp::components::registry::registry::{RegistryComponent::{Event, RegistrationEvent},}; -use zkramp::components::registry::registry_mock::RegistryMock; +use zkramp::components::registry::registry::{ + RegistryComponent, RegistryComponent::{Event, RegistrationEvent, RegistryImpl} +}; +use zkramp::components::registry::registry_mock::{ + RegistryMock, TestingStateDefault, ComponentState +}; 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![] - ); - - IRegistryDispatcher { contract_address: registry_contract_address } -} // // Externals @@ -22,40 +20,47 @@ fn setup_contracts() -> IRegistryDispatcher { #[test] fn test_is_registered() { - set_contract_address(contract_address_const::<'caller'>()); - let registry = setup_contracts(); + let test_address: ContractAddress = test_address(); + + start_cheat_caller_address(test_address, constants::CALLER()); + + let mut registry: ComponentState = Default::default(); registry.register(offchain_id: constants::REVOLUT_ID()); - assert_eq!( - registry.is_registered(contract_address_const::<'caller'>(), constants::REVOLUT_ID()), true - ); + assert_eq!(registry.is_registered(constants::CALLER(), constants::REVOLUT_ID()), true); } #[test] fn test_registration_event() { - set_contract_address(contract_address_const::<'caller'>()); - let registry = setup_contracts(); + let test_address: ContractAddress = test_address(); + let mut spy = spy_events(); + + start_cheat_caller_address(test_address, constants::CALLER()); + + let mut registry: ComponentState = Default::default(); 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() - } - ) + spy + .assert_emitted( + @array![ + ( + test_address, + Event::RegistrationEvent( + RegistrationEvent { + caller: constants::CALLER(), offchain_id: constants::REVOLUT_ID() + } + ) + ) + ] ) - ); } #[test] -#[should_panic(expected: ('Caller is the zero address', 'ENTRYPOINT_FAILED'))] +#[should_panic(expected: 'Caller is the zero address')] fn test_register_from_zero() { - let registry = setup_contracts(); + let mut registry: ComponentState = Default::default(); registry.register(offchain_id: constants::REVOLUT_ID()); } diff --git a/contracts/src/tests/constants.cairo b/contracts/src/tests/constants.cairo index af8821f..defb4f0 100644 --- a/contracts/src/tests/constants.cairo +++ b/contracts/src/tests/constants.cairo @@ -1,3 +1,4 @@ +use core::starknet::{ContractAddress, contract_address_const}; use zkramp::components::registry::interface::OffchainId; pub fn REVTAG() -> ByteArray { @@ -7,3 +8,7 @@ pub fn REVTAG() -> ByteArray { pub fn REVOLUT_ID() -> OffchainId { OffchainId::Revolut(REVTAG()) } + +pub fn CALLER() -> ContractAddress { + contract_address_const::<'caller'>() +}