-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global cheatcodes example and extend appendix (#2533)
<!-- Reference any GitHub issues resolved by this PR --> Closes #2394 ## Introduced changes <!-- A brief description of the changes --> - Add example of global cheatcode usage in `Using cheatcodes` section - Extend appendix with global cheatcodes reference ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md` --------- Co-authored-by: Jan Smółka <[email protected]>
- Loading branch information
1 parent
aedb87b
commit f0cbefb
Showing
5 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
docs/listings/snforge_overview/crates/using_cheatcodes/tests/caller_address.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod failing; | ||
pub mod proper_use; | ||
pub mod proper_use_global; | ||
pub mod cancel; | ||
pub mod span; |
34 changes: 34 additions & 0 deletions
34
...ngs/snforge_overview/crates/using_cheatcodes/tests/caller_address/proper_use_global.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use snforge_std::{ | ||
declare, ContractClassTrait, DeclareResultTrait, start_cheat_caller_address_global, | ||
stop_cheat_caller_address_global | ||
}; | ||
use using_cheatcodes::{ICheatcodeCheckerDispatcher, ICheatcodeCheckerDispatcherTrait}; | ||
|
||
#[test] | ||
fn call_and_invoke_global() { | ||
let contract = declare("CheatcodeChecker").unwrap().contract_class(); | ||
let (contract_address_a, _) = contract.deploy(@array![]).unwrap(); | ||
let (contract_address_b, _) = contract.deploy(@array![]).unwrap(); | ||
let dispatcher_a = ICheatcodeCheckerDispatcher { contract_address: contract_address_a }; | ||
let dispatcher_b = ICheatcodeCheckerDispatcher { contract_address: contract_address_b }; | ||
|
||
let balance_a = dispatcher_a.get_balance(); | ||
let balance_b = dispatcher_b.get_balance(); | ||
assert_eq!(balance_a, 0); | ||
assert_eq!(balance_b, 0); | ||
|
||
// Change the caller address to 123, both targets a and b will be affected | ||
// global cheatcodes work indefinitely until stopped | ||
start_cheat_caller_address_global(123.try_into().unwrap()); | ||
|
||
dispatcher_a.increase_balance(100); | ||
dispatcher_b.increase_balance(100); | ||
|
||
let balance_a = dispatcher_a.get_balance(); | ||
let balance_b = dispatcher_b.get_balance(); | ||
assert_eq!(balance_a, 100); | ||
assert_eq!(balance_b, 100); | ||
|
||
// Cancel the cheat | ||
stop_cheat_caller_address_global(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Cheating Globally | ||
|
||
Cheatcodes which have `_global` suffix allow to change specific properties in blockchain state for all targets and for indefinite time span. Therefore, you don't pass the target address, nor the span. | ||
|
||
See the [Cheating Addresses Globally](../../testing/using-cheatcodes.md#cheating-addresses-globally) example. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters