Skip to content

Commit

Permalink
tests using snforge (#37)
Browse files Browse the repository at this point in the history
* tests using snforge

* create `CALLER` test constant

---------

Co-authored-by: 0xChqrles <[email protected]>
  • Loading branch information
chachaleo and 0xChqrles authored Aug 26, 2024
1 parent 12abb7b commit 9d2a470
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 35 deletions.
Empty file.
6 changes: 6 additions & 0 deletions contracts/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
4 changes: 4 additions & 0 deletions contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ 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

[[target.starknet-contract]]
sierra = true
casm = true

[scripts]
test = "snforge test"
8 changes: 5 additions & 3 deletions contracts/src/components/registry/registry_mock.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ pub mod RegistryMock {
}
}

type ComponentState = RegistryComponent::ComponentState<RegistryMock::ContractState>;
pub type ComponentState = RegistryComponent::ComponentState<RegistryMock::ContractState>;

fn COMPONENT() -> ComponentState {
RegistryComponent::component_state_for_testing()
pub impl TestingStateDefault of Default<ComponentState> {
fn default() -> ComponentState {
RegistryComponent::component_state_for_testing()
}
}
69 changes: 37 additions & 32 deletions contracts/src/components/registry/registry_test.cairo
Original file line number Diff line number Diff line change
@@ -1,61 +1,66 @@
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
//

#[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());
}
5 changes: 5 additions & 0 deletions contracts/src/tests/constants.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::starknet::{ContractAddress, contract_address_const};
use zkramp::components::registry::interface::OffchainId;

pub fn REVTAG() -> ByteArray {
Expand All @@ -7,3 +8,7 @@ pub fn REVTAG() -> ByteArray {
pub fn REVOLUT_ID() -> OffchainId {
OffchainId::Revolut(REVTAG())
}

pub fn CALLER() -> ContractAddress {
contract_address_const::<'caller'>()
}

0 comments on commit 9d2a470

Please sign in to comment.