diff --git a/listings/ch00-getting-started/custom_type_serde/src/contract.cairo b/listings/ch00-getting-started/custom_type_serde/src/contract.cairo index 2b35f76d..e4981b29 100644 --- a/listings/ch00-getting-started/custom_type_serde/src/contract.cairo +++ b/listings/ch00-getting-started/custom_type_serde/src/contract.cairo @@ -1,3 +1,9 @@ +#[starknet::interface] +trait ISerdeCustomType { + fn person_input(ref self: TContractState, person: SerdeCustomType::Person); + fn person_output(self: @TContractState) -> SerdeCustomType::Person; +} + #[starknet::contract] mod SerdeCustomType { #[storage] @@ -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 { fn person_input(ref self: ContractState, person: Person) {} - #[external(v0)] fn person_output(self: @ContractState) -> Person { Person { age: 10, name: 'Joe' } } diff --git a/listings/ch00-getting-started/custom_type_serde/src/tests.cairo b/listings/ch00-getting-started/custom_type_serde/src/tests.cairo index 361dba07..0ef4b63d 100644 --- a/listings/ch00-getting-started/custom_type_serde/src/tests.cairo +++ b/listings/ch00-getting-started/custom_type_serde/src/tests.cairo @@ -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 = 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); + } }