Skip to content

Commit

Permalink
fix: StoringCustomTypes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Nov 15, 2023
1 parent 2a3f076 commit 7d7bc64
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
#[starknet::interface]
trait IStoringCustomType<TContractState> {
fn set_person(ref self: TContractState);
fn get_person(self: @TContractState);
fn set_person(ref self: TContractState, person: Person);
fn get_person(self: @TContractState) -> Person;
}

// Deriving the starknet::Store trait
// allows us to store the `Person` struct in the contract's storage.
#[derive(Drop, Serde, Copy, starknet::Store)]
struct Person {
age: u8,
name: felt252
}

#[starknet::contract]
mod StoringCustomType {
use super::Person;

#[storage]
struct Storage {
person: Person
}

// Deriving the starknet::Store trait
// allows us to store the `Person` struct in the contract's storage.
#[derive(Drop, starknet::Store)]
struct Person {
age: u8,
name: felt252
}

#[abi(embed_v0)]
impl StoringCustomType of super::IStoringCustomType<ContractState> {
fn set_person(ref self: ContractState) {
self.person.write(Person { age: 10, name: 'Joe' });
fn set_person(ref self: ContractState, person: Person) {
self.person.write(person);
}

fn get_person(self: @ContractState) {
self.person.read();
fn get_person(self: @ContractState) -> Person {
self.person.read()
}
}
}
53 changes: 26 additions & 27 deletions listings/ch00-getting-started/storing_custom_types/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,40 @@
// However, because Serde trait is not implemented for the custom type, we can't use custom types as parameters or return values.

mod tests {
use storing_custom_types::contract::IStoringCustomTypeDispatcherTrait;
use storing_custom_types::contract::{
StoringCustomType, IStoringCustomTypeDispatcher, StoringCustomType::Person
use storing_custom_types::{
contract::{
IStoringCustomType,
StoringCustomType,
StoringCustomType::{
Person,
person::InternalContractMemberStateTrait
}
}
};
use core::result::ResultTrait;
use starknet::{deploy_syscall, ContractAddress};
use starknet::class_hash::Felt252TryIntoClassHash;

fn deploy() -> IStoringCustomTypeDispatcher {
let calldata: Array<felt252> = array![];
let (address0, _) = deploy_syscall(
StoringCustomType::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
)
.unwrap();
IStoringCustomTypeDispatcher { contract_address: address0 }
}
use starknet::{
ContractAddress,
contract_address_const,
testing::{set_contract_address}
};

#[test]
#[available_gas(2000000000)]
fn should_deploy() {
let init_value = 10;
let contract = deploy();
fn setup() -> StoringCustomType::ContractState {
let mut state = StoringCustomType::contract_state_for_testing();
let contract_address = contract_address_const::<0x1>();
set_contract_address(contract_address);
state
}

#[test]
#[available_gas(2000000000)]
fn can_call_set_person() {
let contract = deploy();
let received_person = contract.set_person();
}
let mut state = setup();
let person = Person { age: 10, name: 'Joe' };

#[test]
#[available_gas(2000000000)]
fn can_call_get_person() {
let contract = deploy();
contract.get_person();
state.set_person(person);
let read_person = state.person.read();

assert(person.age == read_person.age, 'wrong age');
assert(person.name == read_person.name, 'wrong name');
}
}

0 comments on commit 7d7bc64

Please sign in to comment.