Skip to content

Commit 5be149f

Browse files
committed
Add tests for diagnostics
commit-id:34dd50be
1 parent bd12e0a commit 5be149f

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "simple_package"
3+
version = "0.1.0"
4+
edition = "2024_07"
5+
6+
[dependencies]
7+
starknet = "2.9.3"
8+
9+
[dev-dependencies]
10+
snforge_std = { path = "../../../../../snforge_std" }
11+
12+
[[target.starknet-contract]]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#[starknet::interface]
2+
pub trait IHelloStarknet<TContractState> {
3+
fn increase_balance(ref self: TContractState, amount: felt252);
4+
fn get_balance(self: @TContractState) -> felt252;
5+
fn do_a_panic(self: @TContractState);
6+
fn do_a_panic_with(self: @TContractState, panic_data: Array<felt252>);
7+
}
8+
9+
#[starknet::contract]
10+
pub mod HelloStarknet {
11+
use core::array::ArrayTrait;
12+
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
13+
14+
#[storage]
15+
struct Storage {
16+
balance: felt252,
17+
}
18+
19+
#[abi(embed_v0)]
20+
impl IHelloStarknetImpl of super::IHelloStarknet<ContractState> {
21+
// Increases the balance by the given amount
22+
fn increase_balance(ref self: ContractState, amount: felt252) {
23+
self.balance.write(self.balance.read() + amount);
24+
}
25+
26+
// Returns the current balance
27+
fn get_balance(self: @ContractState) -> felt252 {
28+
self.balance.read()
29+
}
30+
31+
// Panics
32+
fn do_a_panic(self: @ContractState) {
33+
let mut arr = ArrayTrait::new();
34+
arr.append('PANIC');
35+
arr.append('DAYTAH');
36+
panic(arr);
37+
}
38+
39+
// Panics with given array data
40+
fn do_a_panic_with(self: @ContractState, panic_data: Array<felt252>) {
41+
panic(panic_data);
42+
}
43+
}
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod hello_starknet;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use core::array::ArrayTrait;
2+
use core::result::ResultTrait;
3+
use simple_package::hello_starknet::{IHelloStarknetDispatcher, IHelloStarknetDispatcherTrait};
4+
use snforge_std::cheatcodes::contract_class::DeclareResultTrait;
5+
use snforge_std::{ContractClassTrait, declare};
6+
7+
#[test]
8+
#[fuzzer]
9+
#[fork(url: "http://127.0.0.1:3030", block_tag: latest)]
10+
#[ignore]
11+
fn call_and_invoke(_a: felt252, b: u256) {
12+
let contract = declare("HelloStarknet").unwrap().contract_class();
13+
let constructor_calldata = @ArrayTrait::new();
14+
let (contract_address, _) = contract.deploy(constructor_calldata),unwrap();
15+
let dispatcher = IHelloStarknetDispatcher { contract_address };
16+
17+
let balance = dispatcher.get_balance();
18+
assert(balance == 0, 'balance == 0');
19+
20+
dispatcher.increase_balance(100);
21+
22+
let balance = dispatcher.get_balance();
23+
assert(balance == 100, 'balance == 100');
24+
}

crates/forge/tests/e2e/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod forking;
2222
mod fuzzing;
2323
mod io_operations;
2424
mod new;
25+
mod plugin_diagnostics;
2526
mod requirements;
2627
mod running;
2728
mod steps;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use crate::e2e::common::runner::setup_package;
2+
use indoc::indoc;
3+
use shared::test_utils::output_assert::assert_stdout_contains;
4+
use snapbox::cmd::Command as SnapboxCommand;
5+
6+
#[test]
7+
fn simple_package() {
8+
let temp = setup_package("plugin_diagnostics");
9+
let output = SnapboxCommand::new("scarb")
10+
.current_dir(&temp)
11+
.args(["build", "--test"])
12+
.assert()
13+
.failure();
14+
15+
assert_stdout_contains(
16+
output,
17+
indoc! {r#"
18+
error: Missing token ';'.
19+
--> [..]/tests/contract.cairo:14:69
20+
let (contract_address, _) = contract.deploy(constructor_calldata),unwrap();
21+
^
22+
note: this error originates in the attribute macro: `test`
23+
note: this error originates in the attribute macro: `fuzzer`
24+
note: this error originates in the attribute macro: `__fuzzer_config`
25+
note: this error originates in the attribute macro: `__fuzzer_wrapper`
26+
note: this error originates in the attribute macro: `fork`
27+
note: this error originates in the attribute macro: `ignore`
28+
note: this error originates in the attribute macro: `__internal_config_statement`
29+
30+
error: Skipped tokens. Expected: statement.
31+
--> [..]/tests/contract.cairo:14:70
32+
let (contract_address, _) = contract.deploy(constructor_calldata),unwrap();
33+
^^^^^^^
34+
note: this error originates in the attribute macro: `test`
35+
note: this error originates in the attribute macro: `fuzzer`
36+
note: this error originates in the attribute macro: `__fuzzer_config`
37+
note: this error originates in the attribute macro: `__fuzzer_wrapper`
38+
note: this error originates in the attribute macro: `fork`
39+
note: this error originates in the attribute macro: `ignore`
40+
note: this error originates in the attribute macro: `__internal_config_statement`
41+
42+
error: Unexpected type for tuple pattern. "core::result::Result::<(core::starknet::contract_address::ContractAddress, core::array::Span::<core::felt252>), core::array::Array::<core::felt252>>" is not a tuple.
43+
--> [..]/tests/contract.cairo:13:33-14:68
44+
let constructor_calldata = @ArrayTrait::new();
45+
_________________________________^
46+
| let (contract_address, _) = contract.deploy(constructor_calldata),unwrap();
47+
|____________________________________________________________________^
48+
note: this error originates in the attribute macro: `test`
49+
note: this error originates in the attribute macro: `fuzzer`
50+
note: this error originates in the attribute macro: `__fuzzer_config`
51+
note: this error originates in the attribute macro: `__fuzzer_wrapper`
52+
note: this error originates in the attribute macro: `fork`
53+
note: this error originates in the attribute macro: `ignore`
54+
note: this error originates in the attribute macro: `__internal_config_statement`
55+
56+
error[E0006]: Function not found.
57+
--> [..]/tests/contract.cairo:14:70
58+
let (contract_address, _) = contract.deploy(constructor_calldata),unwrap();
59+
^^^^^^^
60+
note: this error originates in the attribute macro: `test`
61+
note: this error originates in the attribute macro: `fuzzer`
62+
note: this error originates in the attribute macro: `__fuzzer_config`
63+
note: this error originates in the attribute macro: `__fuzzer_wrapper`
64+
note: this error originates in the attribute macro: `fork`
65+
note: this error originates in the attribute macro: `ignore`
66+
note: this error originates in the attribute macro: `__internal_config_statement`
67+
68+
error: Variable "contract_address" not found.
69+
--> [..]/tests/contract.cairo:15:49
70+
let dispatcher = IHelloStarknetDispatcher { contract_address };
71+
^^^^^^^^^^^^^^^^^
72+
note: this error originates in the attribute macro: `test`
73+
note: this error originates in the attribute macro: `fuzzer`
74+
note: this error originates in the attribute macro: `__fuzzer_config`
75+
note: this error originates in the attribute macro: `__fuzzer_wrapper`
76+
note: this error originates in the attribute macro: `fork`
77+
note: this error originates in the attribute macro: `ignore`
78+
note: this error originates in the attribute macro: `__internal_config_statement`
79+
80+
error: could not compile `simple_package_integrationtest` due to previous error
81+
"#},
82+
);
83+
}

0 commit comments

Comments
 (0)