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

chore: cheat code documentation #308

Merged
merged 21 commits into from
Aug 13, 2024
Merged

Conversation

od-hunter
Copy link
Contributor

resolves #300

Copy link

vercel bot commented Jul 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
book ✅ Ready (Inspect) Visit Preview Aug 13, 2024 4:17pm

Copy link
Contributor

@glihm glihm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the revision @od-hunter. Most of the examples are still not using real functions from the core lib. I've made some corrections but not all, could you please take the time to check if all the examples compile and make the according corrections with the examples given?

Like so, the reader of the book will have accurate information. 🫡

Comment on lines 69 to 72
This cheat code helps one set the version to the provided value, enabling you to test contract behavior with different versions. You can apply this when:

- Testing a contract's behavior with different versions, such as checking if a certain function is only callable in a specific version.
- Simulating a scenario where a contract is upgraded to a different version.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this cheat code is about the version of the transaction, not the contract's version.
Could you please rephrase accordingly?

- Simulating a scenario where a contract is upgraded to a different version.

```
use starknet::{testing, get_version};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use starknet::{testing, get_version};
use starknet::{testing, get_tx_info};

get_version function doesn't exist. It's the version of the transaction inside the transaction execution info:

testing::set_version(1);
assert_eq!(get_tx_info().unbox().version, 1_felt252);


This cheat code helps you set the account contract address to the provided value, allowing you to test contract interactions with different account contracts. You can apply this when:

- Testing a contract's interaction with a specific account contract, such as a wallet contract.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Testing a contract's interaction with a specific account contract, such as a wallet contract.
- Testing a contract's interaction with a specific account contract.

```
use starknet::{testing, get_account_contract_address, contract_address_const};

const contract: ContractAddress = contract_address_const::<'contract'>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const contract: ContractAddress = contract_address_const::<'contract'>();
const contract = contract_address_const::<'contract'>();

Type is inferred, it makes the code more readable. 👍

- Simulating a scenario where a contract is called by a different account contract.

```
use starknet::{testing, get_account_contract_address, contract_address_const};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use starknet::{testing, get_account_contract_address, contract_address_const};
use starknet::{testing, get_tx_info, contract_address_const};

Same here, this function doesn't exist.


testing::set_account_contract_address(contract);

assert(get_account_contract_address() == contract, 'wrong contract address');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(get_account_contract_address() == contract, 'wrong contract address');
assert_eq!(get_tx_info().unbox().account_contract_address.into(), contract);

Using assert_eq! you don't always require to put a reason. Can be sometimes shorter and better. :)

Copy link
Contributor

@glihm glihm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@od-hunter thanks for the work here, in a good direction but not yet ready to be published.

I've prepared for you a test module that you can run to test all the cheat codes and adjust the documentation accordingly. Some of the examples are not working, could you please adjust with the given test module to ensure everything compiles? Thank you very much!

#[cfg(test)]
mod tests {
    use starknet::{testing, get_block_number, get_caller_address, get_contract_address, contract_address_const, get_block_timestamp, get_tx_info};

    #[test]
    fn f1() {
        testing::set_block_number(1234567);
        assert!(get_block_number() == 1234567, "bad block number");
    }

    #[test]
    fn f2() {
        let user_one = contract_address_const::<'user1'>();
        testing::set_caller_address(user_one);
        assert!(get_caller_address() == user_one, "bad caller");
    }

    #[test]
    fn f3() {
        let hub_address = contract_address_const::<'hub'>();
        testing::set_contract_address(hub_address);
        assert!(get_contract_address() == hub_address, "bad address");
    }

    #[test]
    fn f4() {
        testing::set_block_timestamp(123456);
        assert!(get_block_timestamp() == 123456, "bad timestamp");
    }

    #[test]
    fn f5() {
        testing::set_version('0.1.0');
        assert_eq!(get_tx_info().unbox().version, '0.1.0');
    }

    #[test]
    fn f6() {
        let contract = contract_address_const::<'contract'>(); 
        testing::set_account_contract_address(contract);
        assert_eq!(get_tx_info().unbox().account_contract_address.into(), contract);
    }

    #[test]
    fn f7() {
        testing::set_max_fee(123456);
        assert_eq!(get_tx_info().unbox().max_fee.into(), 123456);
    }

    #[test]
    fn f8() {
        testing::set_transaction_hash('12345678');
        assert_eq!(get_tx_info().unbox().transaction_hash, '12345678');
    }

    #[test]
    fn f9() {
        testing::set_chain_id('test_chain_id');
        assert_eq!(get_tx_info().unbox().chain_id.into(), 'test_chain_id');
    }

    #[test]
    fn f10() {
        testing::set_nonce('test_nonce');
        assert_eq!(get_tx_info().unbox().nonce, 'test_nonce');
    }

    #[test]
    fn f11() {
        testing::set_signature(array!['r', 's'].span());
        let s = get_tx_info().unbox().signature;
        assert_eq!(*s[0], 'r');
        assert_eq!(*s[1], 's');
    }

    #[test]
    fn f12() {
        testing::set_block_hash(12345678, 'value');
        assert_eq!(get_tx_info().unbox().block_hash, 12345678);
    }

    #[test]
    fn f13() {
        let contract_address = starknet::contract_address_const::<0x42>();
        let _log = testing::pop_log_raw(contract_address);
    }

    #[derive(Drop, starknet::Event)]
    struct TestEvent {
        #[key]
        id: u32,
        data: felt252,
    }

    #[test]
    fn f14() {
        let contract_address = starknet::contract_address_const::<0x42>();
        let _log = testing::pop_log::<TestEvent>(contract_address);
    }

    #[test]
    fn f15() {
        let contract_address = starknet::contract_address_const::<0x42>();
        let _msg = testing::pop_l2_to_l1_message(contract_address);
    }
}

docs/pages/framework/testing-cheat-codes.md Show resolved Hide resolved
Co-authored-by: glihm <dev@glihm.net>
@od-hunter
Copy link
Contributor Author

od-hunter commented Aug 7, 2024

gm sir @glihm
i am a bit stuck in the last 3 there test examples: pop_log_raw, pop_log, pop_l2_to_l1_message

i don’t know how to go about writing the assert statments for them
please can you assist me with resources to solve it

@glihm
Copy link
Contributor

glihm commented Aug 10, 2024

gm sir @glihm i am a bit stuck in the last 3 there test examples: pop_log_raw, pop_log, pop_l2_to_l1_message

i don’t know how to go about writing the assert statments for them please can you assist me with resources to solve it

Sorry for the delay here.

For the messages, you can skip them for now. These are not used that much for the moment in Dojo.

In the meantime, the pop_log is very used and important.
To test it correctly, you should have a contract that actually emits an event. And then the event will be caught by this cheat code.

What you can do to make the full test, write a minimal contract that emits an event, call the function that emits the event, and then pop the log.

A minimal contract could look something like this:

#[starknet::interface]
pub trait IContract<T> {
    fn emit_log(ref self: T);
}

#[starknet::contract]
pub mod MyContract {
    #[abi(embed_v0)]
    impl IContractImpl of IContract<ContractState> {
        fn emit_log(ref self: ContractState) {
            starknet::emit(.....);
        }
    }
}

You can use the demo event, don't forget to declare inside the contract. 👍

@od-hunter
Copy link
Contributor Author

@glihm , please review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[testing] add details on cairo test runner cheatcodes
2 participants