Skip to content

Commit

Permalink
Make contract tests visibility compatible. (#5932)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbens-starkware authored Jul 2, 2024
1 parent 43cf361 commit 715662f
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 90 deletions.
8 changes: 4 additions & 4 deletions corelib/src/starknet/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use starknet::storage_access::storage_base_address_from_felt252;
/// A pointer to an address in storage, can be used to read and write values, if the generic type
/// supports it (e.g. basic types like `felt252`).
pub struct StoragePointer<T> {
address: StorageBaseAddress,
offset: u8,
pub address: StorageBaseAddress,
pub offset: u8,
}

impl StoragePointerCopy<T> of Copy<StoragePointer<T>> {}
Expand Down Expand Up @@ -520,8 +520,8 @@ impl MutableImpl<T> of MutableTrait<Mutable<T>> {
/// Implementation of SubPointers for core types.
#[derive(Drop, Copy)]
struct u256SubPointers {
low: starknet::storage::StoragePointer<u128>,
high: starknet::storage::StoragePointer<u128>,
pub low: starknet::storage::StoragePointer<u128>,
pub high: starknet::storage::StoragePointer<u128>,
}

#[feature("derive-storage")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use starknet::{account::Call, ContractAddress};
use core::test::test_utils::{assert_eq, assert_ne};

use super::utils::serialized;

Expand Down Expand Up @@ -53,22 +52,14 @@ mod test_contract {
fn test_dispatcher_serialization() {
let a = starknet::contract_address_const::<11>();
test_contract::__external::set_another_address(serialized(a));
assert_eq(
@test_contract::__external::get_another_address(serialized(())),
@serialized(a),
'Wrong result'
);
assert_eq!(test_contract::__external::get_another_address(serialized(())), serialized(a),);
}

#[test]
fn test_library_dispatcher_serialization() {
let a = starknet::contract_address_const::<11>();
test_contract::__external::set_another_class_hash(serialized(a));
assert_eq(
@test_contract::__external::get_another_class_hash(serialized(())),
@serialized(a),
'Wrong result'
);
assert_eq!(test_contract::__external::get_another_class_hash(serialized(())), serialized(a),);
}


Expand Down Expand Up @@ -116,7 +107,7 @@ fn test_validate_gas_cost() {
let serialized_args = serialized(calls);
let post_serialization_gas = withdraw_and_get_available_gas();

test_contract::__wrapper____validate__(serialized_args);
test_contract::__external::__validate__(serialized_args);
let post_call_gas = withdraw_and_get_available_gas();

let call_building_gas_usage = base_gas - post_call_building_gas;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
cairo_level_tests = "."

[config.global]
edition = "2023_10"
edition = "2023_11"
71 changes: 32 additions & 39 deletions crates/cairo-lang-starknet/cairo_level_tests/contract_tests.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::traits::TryInto;
use core::num::traits::Zero;
use starknet::StorageAddress;
use core::test::test_utils::{assert_eq, assert_ne};

use super::utils::serialized;

Expand Down Expand Up @@ -93,9 +93,7 @@ fn test_wrapper_too_many_args() {

#[test]
fn test_wrapper_valid_args() {
assert_eq(
@test_contract::__external::get_plus_2(serialized(1)), @serialized(3), 'Wrong result'
);
assert_eq!(test_contract::__external::get_plus_2(serialized(1)), serialized(3));
}

#[test]
Expand All @@ -107,53 +105,47 @@ fn test_wrapper_valid_args_out_of_gas() {

#[test]
fn test_wrapper_array_arg_and_output() {
assert_eq(
@test_contract::__external::get_appended_array(serialized(array![2])),
@serialized(array![2, 1]),
'Wrong result'
assert_eq!(
test_contract::__external::get_appended_array(serialized(array![2])),
serialized(array![2, 1])
);
}

#[test]
fn read_first_value() {
assert_eq(
@test_contract::__external::get_value(serialized(())), @serialized(0), 'Wrong result'
);
assert_eq!(test_contract::__external::get_value(serialized(())), serialized(0));
}

#[test]
fn write_read_value() {
assert(test_contract::__external::set_value(serialized(4)).is_empty(), 'Not empty');
assert_eq(
@test_contract::__external::get_value(serialized(())), @serialized(4), 'Wrong result'
);
assert_eq!(test_contract::__external::get_value(serialized(())), serialized(4));
}

#[test]
fn empty_start() {
assert_eq(@test_contract::__external::contains(serialized(4)), @serialized(0), 'Wrong result');
assert_eq!(test_contract::__external::contains(serialized(4)), serialized(0));
}

#[test]
fn contains_added() {
assert(test_contract::__external::insert(serialized(4)).is_empty(), 'Not empty');
assert_eq(@test_contract::__external::contains(serialized(4)), @serialized(1), 'Wrong result');
assert_eq(@test_contract::__external::contains(serialized(5)), @serialized(0), 'Wrong result');
assert_eq!(test_contract::__external::contains(serialized(4)), serialized(1));
assert_eq!(test_contract::__external::contains(serialized(5)), serialized(0));
}

#[test]
fn not_contains_removed() {
assert(test_contract::__external::insert(serialized(4)).is_empty(), 'Not empty');
assert(test_contract::__external::remove(serialized(4)).is_empty(), 'Not empty');
assert_eq(@test_contract::__external::contains(serialized(4)), @serialized(0), 'Wrong result');
assert_eq!(test_contract::__external::contains(serialized(4)), serialized(0));
}

#[test]
fn read_large_first_value() {
assert_eq(
@test_contract::__external::get_large(serialized(0x200000000000000000000000000000001_u256)),
@serialized(0_u256),
'Wrong result'
assert_eq!(
test_contract::__external::get_large(serialized(0x200000000000000000000000000000001_u256)),
serialized(0_u256)
);
}

Expand All @@ -168,10 +160,9 @@ fn write_read_large_value() {
.is_empty(),
'Array not empty'
);
assert_eq(
@test_contract::__external::get_large(serialized(0x200000000000000000000000000000001_u256)),
@serialized(0x400000000000000000000000000000003_u256),
'Wrong result'
assert_eq!(
test_contract::__external::get_large(serialized(0x200000000000000000000000000000001_u256)),
serialized(0x400000000000000000000000000000003_u256)
);
}

Expand Down Expand Up @@ -215,9 +206,7 @@ fn test_get_version() {
fn test_get_account_contract_address() {
assert(starknet::get_tx_info().unbox().account_contract_address.is_zero(), 'non default value');
starknet::testing::set_account_contract_address(starknet::contract_address_const::<1>());
assert_eq(
@starknet::get_tx_info().unbox().account_contract_address.into(), @1, 'not set value'
);
assert_eq!(starknet::get_tx_info().unbox().account_contract_address.into(), 1);
}

#[test]
Expand Down Expand Up @@ -254,8 +243,8 @@ fn test_get_signature() {
starknet::testing::set_signature(array!['some', 'signature'].span());
let read_signature = starknet::get_tx_info().unbox().signature;
assert_eq!(read_signature.len(), 2);
assert_eq(read_signature.at(0), @'some', 'unexpected element 0');
assert_eq(read_signature.at(1), @'signature', 'unexpected element 1');
assert_eq!(read_signature.at(0), @'some');
assert_eq!(read_signature.at(1), @'signature');
}

#[test]
Expand Down Expand Up @@ -291,45 +280,49 @@ fn test_pop_l2_to_l1_message_empty_messages() {
#[test]
#[should_panic]
fn test_out_of_range_storage_address_from_felt252() -> starknet::StorageAddress {
starknet::storage_address_try_from_felt252(-1).unwrap()
(-1).try_into().unwrap()
}

#[test]
fn test_storage_address() {
let mut args = array![0x17];
let _storage_address = starknet::storage_address_try_from_felt252(0x17).unwrap();
let _storage_address: starknet::StorageAddress = 0x17.try_into().unwrap();
let ret_data = test_contract::__external::test_storage_address(args.span());

assert_eq(args[0_u32], ret_data[0_u32], 'Unexpected ret_data.');
assert_eq!(args[0_u32], ret_data[0_u32]);
}

#[derive(starknet::Event, PartialEq, Drop, Clone, Serde)]
#[derive(starknet::Event, PartialEq, Drop, Clone, Serde, Debug)]
struct A {
x: felt252,
#[key]
data: usize,
}

#[derive(starknet::Event, PartialEq, Drop, Clone, Serde)]
#[derive(starknet::Event, PartialEq, Drop, Clone, Serde, Debug)]
struct B {
x: felt252,
}

#[derive(starknet::Event, PartialEq, Drop, Clone)]
#[derive(starknet::Event, PartialEq, Drop, Clone, Debug)]
enum MyEventEnum {
A: A,
B: B,
}

fn event_serde_tester<T, +starknet::Event<T>, +Clone<T>, +PartialEq<T>, +Drop<T>>(event: T) {
fn event_serde_tester<
T, +starknet::Event<T>, +Clone<T>, +PartialEq<T>, +Drop<T>, +core::fmt::Debug<T>
>(
event: T
) {
let original_event = event.clone();
let mut keys = Default::default();
let mut data = Default::default();
event.append_keys_and_data(ref keys, ref data);
let mut keys = keys.span();
let mut data = data.span();
let mut event = starknet::Event::deserialize(ref keys, ref data).unwrap();
assert_eq(@event, @original_event, 'Event deserialization failed');
assert_eq!(event, original_event);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod test_contract;
mod new_syntax_test_contract;
mod minimal_contract;
mod hello_starknet;
mod erc20;
pub mod erc20;
mod token_bridge;
mod with_erc20;
mod with_ownable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use starknet::ContractAddress;

#[starknet::interface]
trait IERC20<TContractState> {
pub trait IERC20<TContractState> {
fn get_name(self: @TContractState) -> felt252;
fn get_symbol(self: @TContractState) -> felt252;
fn get_decimals(self: @TContractState) -> u8;
Expand All @@ -20,7 +20,7 @@ trait IERC20<TContractState> {
}

#[starknet::contract]
mod erc_20 {
pub mod erc_20 {
use core::num::traits::Zero;
use starknet::get_caller_address;
use starknet::contract_address_const;
Expand Down Expand Up @@ -82,7 +82,7 @@ mod erc_20 {
}

#[abi(embed_v0)]
impl IERC20Impl of super::IERC20<ContractState> {
pub impl IERC20Impl of super::IERC20<ContractState> {
fn get_name(self: @ContractState) -> felt252 {
self.name.read()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-starknet/cairo_level_tests/deployment.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::result::ResultTrait;
use starknet::deploy_syscall;
use starknet::syscalls::deploy_syscall;

#[starknet::interface]
trait IValue<TContractState> {
Expand All @@ -10,7 +10,7 @@ trait IValue<TContractState> {
#[starknet::contract]
mod self_caller {
use core::traits::TryInto;
use starknet::{ClassHash, deploy_syscall};
use starknet::{ClassHash, syscalls::deploy_syscall};
use super::{IValueDispatcher, IValueDispatcherTrait};
#[storage]
struct Storage {
Expand Down
8 changes: 3 additions & 5 deletions crates/cairo-lang-starknet/cairo_level_tests/events.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::debug::PrintTrait;
use core::test::test_utils::{assert_eq, assert_ne};
use starknet::syscalls::{deploy_syscall, get_block_hash_syscall};
use starknet::SyscallResultTrait;

Expand All @@ -11,7 +9,7 @@ trait IContractWithEvent<T> {

#[starknet::contract]
mod contract_with_event {
use starknet::info::get_contract_address;
use starknet::get_contract_address;
#[storage]
struct Storage {
value: u128,
Expand Down Expand Up @@ -126,8 +124,8 @@ fn test_pop_log() {
let contract_address = starknet::contract_address_const::<0x1234>();
starknet::testing::set_contract_address(contract_address);
let (keys, data) = ([1234].span(), [2345].span());
starknet::emit_event_syscall(keys, data).unwrap_syscall();
starknet::emit_event_syscall(keys, data).unwrap_syscall();
starknet::syscalls::emit_event_syscall(keys, data).unwrap_syscall();
starknet::syscalls::emit_event_syscall(keys, data).unwrap_syscall();

assert_eq!(starknet::testing::pop_log_raw(contract_address), Option::Some((keys, data)));
assert_eq!(starknet::testing::pop_log_raw(contract_address), Option::Some((keys, data)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait IContract<T> {

#[starknet::contract]
mod contract_a {
use starknet::info::get_contract_address;
use starknet::get_contract_address;
#[storage]
struct Storage {
value: u128,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use core::traits::{Into, TryInto, PartialEq};
use core::array::{ArrayTrait, SpanTrait, SpanPartialEq};
use core::array::{ArrayTrait, SpanTrait};
use core::option::OptionTrait;
use core::result::ResultTrait;
use starknet::{testing, SyscallResultTrait};
use starknet::syscalls::{deploy_syscall, get_block_hash_syscall};
use starknet::class_hash::Felt252TryIntoClassHash;
use core::test::test_utils::{assert_eq, assert_ne};

use contract_with_messages_sent_to_l1::IContractWithMessagesSentToL1;

Expand Down Expand Up @@ -34,7 +32,7 @@ mod contract_with_messages_sent_to_l1 {
fn send_message_to_l1(ref self: ContractState) {
let value_ = self.value.read();

starknet::send_message_to_l1_syscall(
starknet::syscalls::send_message_to_l1_syscall(
to_address: value_.into(), payload: generate_payload(n: value_).span()
)
.unwrap_syscall();
Expand Down Expand Up @@ -97,8 +95,8 @@ fn test_pop_l2_to_l1_message() {
let mut to_address = 1234;
let mut payload = [2345].span();

starknet::send_message_to_l1_syscall(to_address, payload).unwrap_syscall();
starknet::send_message_to_l1_syscall(to_address, payload).unwrap_syscall();
starknet::syscalls::send_message_to_l1_syscall(to_address, payload).unwrap_syscall();
starknet::syscalls::send_message_to_l1_syscall(to_address, payload).unwrap_syscall();

assert_eq!(
testing::pop_l2_to_l1_message(contract_address), Option::Some((to_address, payload))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cairo_level_tests::contracts::multi_component::{
fn test_flow() {
// Set up.
let recipient = starknet::contract_address_const::<0x1337>();
let (contract_address, _) = starknet::deploy_syscall(
let (contract_address, _) = starknet::syscalls::deploy_syscall(
contract_with_4_components::TEST_CLASS_HASH.try_into().unwrap(),
0,
serialized((('name', 'symbol'), (18_u8, 1000_u256, recipient), recipient)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod contract_a {
#[abi(embed_v0)]
impl IWithReplaceImpl of super::IWithReplace<ContractState> {
fn replace(ref self: ContractState, class_hash: ClassHash) {
starknet::replace_class_syscall(class_hash).unwrap_syscall();
starknet::syscalls::replace_class_syscall(class_hash).unwrap_syscall();
}
}
}
Expand Down
Loading

0 comments on commit 715662f

Please sign in to comment.