Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test storing custom types #112

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
#[starknet::interface]
trait IStoringCustomType<TContractState> {
fn set_person(ref self: TContractState, person: 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(per_item)]
#[generate_trait]
impl StoringCustomType of IStoringCustomType {
#[external(v0)]
fn set_person(ref self: ContractState) {
self.person.write(Person { age: 10, name: 'Joe' });
}

#[external(v0)]
fn get_person(self: @ContractState) {
self.person.read();
#[abi(embed_v0)]
impl StoringCustomType of super::IStoringCustomType<ContractState> {
fn set_person(ref self: ContractState, person: Person) {
self.person.write(person);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
mod tests { // TODO
// The purpose of these tests is to demonstrate the capability to store custom types in the contract's state.

mod tests {
use storing_custom_types::{
contract::{
IStoringCustomType, StoringCustomType,
StoringCustomType::{Person, person::InternalContractMemberStateTrait}
}
};

use starknet::{ContractAddress, contract_address_const, testing::{set_contract_address}};

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 mut state = setup();
let person = Person { age: 10, name: 'Joe' };

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');
}
}