Skip to content

Commit

Permalink
feat: Test custom type serde (#111)
Browse files Browse the repository at this point in the history
* feat: add Tuples in cheatsheet

* nit: trigger ci

* feat: tests of custom type serde

* appying improvement

* nit: fix embed
  • Loading branch information
julienbrs authored Nov 15, 2023
1 parent f6faa76 commit 25e9345
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#[starknet::interface]
trait ISerdeCustomType<TContractState> {
fn person_input(ref self: TContractState, person: SerdeCustomType::Person);
fn person_output(self: @TContractState) -> SerdeCustomType::Person;
}

#[starknet::contract]
mod SerdeCustomType {
#[storage]
Expand All @@ -11,13 +17,10 @@ mod SerdeCustomType {
name: felt252
}

#[abi(per_item)]
#[generate_trait]
impl SerdeCustomType of ISerdeCustomType {
#[external(v0)]
#[abi(embed_v0)]
impl SerdeCustomType of super::ISerdeCustomType<ContractState> {
fn person_input(ref self: ContractState, person: Person) {}

#[external(v0)]
fn person_output(self: @ContractState) -> Person {
Person { age: 10, name: 'Joe' }
}
Expand Down
50 changes: 49 additions & 1 deletion listings/ch00-getting-started/custom_type_serde/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
mod tests { // TODO
// The purpose of these tests is to demonstrate the capability of using custom types as inputs and outputs in contract calls.
// Therefore, we are not employing getters and setters for managing the contract's state.

mod tests {
use custom_type_serde::contract::{
SerdeCustomType, SerdeCustomType::Person, ISerdeCustomTypeDispatcher,
ISerdeCustomTypeDispatcherTrait
};
use core::result::ResultTrait;
use debug::PrintTrait;
use starknet::{deploy_syscall, ContractAddress};
use starknet::class_hash::Felt252TryIntoClassHash;

fn deploy() -> ISerdeCustomTypeDispatcher {
let calldata: Array<felt252> = array![];
let (address0, _) = deploy_syscall(
SerdeCustomType::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
)
.unwrap();
ISerdeCustomTypeDispatcher { contract_address: address0 }
}

#[test]
#[available_gas(2000000000)]
fn should_deploy() {
let init_value = 10;
let contract = deploy();
}

#[test]
#[available_gas(2000000000)]
fn should_get_person_output() {
let contract = deploy();
let expected_person = Person { age: 10, name: 'Joe' };
let received_person = contract.person_output();
let age_received = received_person.age;
let name_received = received_person.name;

assert(age_received == expected_person.age, 'Wrong age value');
assert(name_received == expected_person.name, 'Wrong name value');
}

#[test]
#[available_gas(2000000000)]
fn should_call_person_input() {
let contract = deploy();
let expected_person = Person { age: 10, name: 'Joe' };
let received_person = contract.person_input(expected_person);
}
}

0 comments on commit 25e9345

Please sign in to comment.