From 5893725079eb6dad9b3956e091ad69401ea1a7d6 Mon Sep 17 00:00:00 2001 From: Gerson2102 Date: Mon, 10 Jun 2024 11:09:34 -0600 Subject: [PATCH 1/2] updating starknet foundry version --- Scarb.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Scarb.toml b/Scarb.toml index a2e32df..8e1bd7d 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -8,7 +8,7 @@ starknet = "2.6.3" alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git" } [dev-dependencies] -snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.23.0" } +snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.24.0" } [scripts] test = "snforge test" From 9105a7f02820ceb5335d71d355291ca27e74f8a2 Mon Sep 17 00:00:00 2001 From: Gerson2102 Date: Mon, 10 Jun 2024 11:21:17 -0600 Subject: [PATCH 2/2] starknet foundry test suite --- tests_suite/.gitignore | 2 ++ tests_suite/Scarb.lock | 14 ++++++++ tests_suite/Scarb.toml | 18 ++++++++++ tests_suite/src/lib.cairo | 25 ++++++++++++++ tests_suite/tests/test_contract.cairo | 47 +++++++++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 tests_suite/.gitignore create mode 100644 tests_suite/Scarb.lock create mode 100644 tests_suite/Scarb.toml create mode 100644 tests_suite/src/lib.cairo create mode 100644 tests_suite/tests/test_contract.cairo diff --git a/tests_suite/.gitignore b/tests_suite/.gitignore new file mode 100644 index 0000000..73aa31e --- /dev/null +++ b/tests_suite/.gitignore @@ -0,0 +1,2 @@ +target +.snfoundry_cache/ diff --git a/tests_suite/Scarb.lock b/tests_suite/Scarb.lock new file mode 100644 index 0000000..969b7fa --- /dev/null +++ b/tests_suite/Scarb.lock @@ -0,0 +1,14 @@ +# Code generated by scarb DO NOT EDIT. +version = 1 + +[[package]] +name = "snforge_std" +version = "0.24.0" +source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.24.0#95e9fb09cb91b3c05295915179ee1b55bf923653" + +[[package]] +name = "tests_suite" +version = "0.1.0" +dependencies = [ + "snforge_std", +] diff --git a/tests_suite/Scarb.toml b/tests_suite/Scarb.toml new file mode 100644 index 0000000..4ffb0f4 --- /dev/null +++ b/tests_suite/Scarb.toml @@ -0,0 +1,18 @@ +[package] +name = "tests_suite" +version = "0.1.0" +edition = "2023_11" + +# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html + +[dependencies] +starknet = "2.6.3" + +[dev-dependencies] +snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.24.0" } + +[[target.starknet-contract]] +sierra = true + +[scripts] +test = "snforge test" diff --git a/tests_suite/src/lib.cairo b/tests_suite/src/lib.cairo new file mode 100644 index 0000000..8cd1124 --- /dev/null +++ b/tests_suite/src/lib.cairo @@ -0,0 +1,25 @@ +#[starknet::interface] +pub trait IHelloStarknet { + fn increase_balance(ref self: TContractState, amount: felt252); + fn get_balance(self: @TContractState) -> felt252; +} + +#[starknet::contract] +mod HelloStarknet { + #[storage] + struct Storage { + balance: felt252, + } + + #[abi(embed_v0)] + impl HelloStarknetImpl of super::IHelloStarknet { + fn increase_balance(ref self: ContractState, amount: felt252) { + assert(amount != 0, 'Amount cannot be 0'); + self.balance.write(self.balance.read() + amount); + } + + fn get_balance(self: @ContractState) -> felt252 { + self.balance.read() + } + } +} diff --git a/tests_suite/tests/test_contract.cairo b/tests_suite/tests/test_contract.cairo new file mode 100644 index 0000000..fb5ed7b --- /dev/null +++ b/tests_suite/tests/test_contract.cairo @@ -0,0 +1,47 @@ +use starknet::ContractAddress; + +use snforge_std::{declare, ContractClassTrait}; + +use tests_suite::IHelloStarknetSafeDispatcher; +use tests_suite::IHelloStarknetSafeDispatcherTrait; +use tests_suite::IHelloStarknetDispatcher; +use tests_suite::IHelloStarknetDispatcherTrait; + +fn deploy_contract(name: ByteArray) -> ContractAddress { + let contract = declare(name).unwrap(); + let (contract_address, _) = contract.deploy(@ArrayTrait::new()).unwrap(); + contract_address +} + +#[test] +fn test_increase_balance() { + let contract_address = deploy_contract("HelloStarknet"); + + let dispatcher = IHelloStarknetDispatcher { contract_address }; + + let balance_before = dispatcher.get_balance(); + assert(balance_before == 0, 'Invalid balance'); + + dispatcher.increase_balance(42); + + let balance_after = dispatcher.get_balance(); + assert(balance_after == 42, 'Invalid balance'); +} + +#[test] +#[feature("safe_dispatcher")] +fn test_cannot_increase_balance_with_zero_value() { + let contract_address = deploy_contract("HelloStarknet"); + + let safe_dispatcher = IHelloStarknetSafeDispatcher { contract_address }; + + let balance_before = safe_dispatcher.get_balance().unwrap(); + assert(balance_before == 0, 'Invalid balance'); + + match safe_dispatcher.increase_balance(0) { + Result::Ok(_) => core::panic_with_felt252('Should have panicked'), + Result::Err(panic_data) => { + assert(*panic_data.at(0) == 'Amount cannot be 0', *panic_data.at(0)); + } + }; +}