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

feat: Test custom type serde #111

Merged
merged 6 commits into from
Nov 15, 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
julienbrs marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}