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

Test suite #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions tests_suite/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
.snfoundry_cache/
14 changes: 14 additions & 0 deletions tests_suite/Scarb.lock
Original file line number Diff line number Diff line change
@@ -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",
]
18 changes: 18 additions & 0 deletions tests_suite/Scarb.toml
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 25 additions & 0 deletions tests_suite/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[starknet::interface]
pub trait IHelloStarknet<TContractState> {
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<ContractState> {
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()
}
}
}
47 changes: 47 additions & 0 deletions tests_suite/tests/test_contract.cairo
Original file line number Diff line number Diff line change
@@ -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));
}
};
}