Skip to content

Commit 15b4758

Browse files
authored
test: Test constructor function with and without values (#749)
* Add test for constructor * Create bank contract to reuse and remove extra event * Forge fmt
1 parent 77198c6 commit 15b4758

File tree

6 files changed

+269
-25
lines changed

6 files changed

+269
-25
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
pragma solidity ^0.8.18;
3+
4+
import {Script} from "forge-std/Script.sol";
5+
import {Bank} from "../src/Bank.sol";
6+
7+
contract ConstructorScript is Script {
8+
function run() external {
9+
vm.startBroadcast();
10+
11+
// Test constructor without value
12+
Bank bankNoValue = new Bank();
13+
assert(bankNoValue.balance() == 0);
14+
15+
// Test constructor with 1 ether
16+
Bank bankWithEther = new Bank{value: 1 ether}();
17+
assert(bankWithEther.balance() == 1 ether);
18+
19+
// Test constructor with smaller value
20+
Bank bankSmallValue = new Bank{value: 0.1 ether}();
21+
assert(bankSmallValue.balance() == 0.1 ether);
22+
23+
vm.stopBroadcast();
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Forge tests for constructor functionality with and without value.
2+
3+
use crate::{config::*, test_helpers::TEST_DATA_DEFAULT};
4+
use foundry_test_utils::{forgetest_async, util, TestProject};
5+
6+
use crate::test_helpers::run_zk_script_test;
7+
use forge::revm::primitives::SpecId;
8+
use foundry_test_utils::Filter;
9+
10+
#[tokio::test(flavor = "multi_thread")]
11+
async fn test_zk_constructor_works() {
12+
let runner = TEST_DATA_DEFAULT.runner_zksync();
13+
let filter = Filter::new("testZkConstructor", "ZkConstructorTest", ".*");
14+
15+
TestConfig::with_filter(runner, filter).evm_spec(SpecId::SHANGHAI).run().await;
16+
}
17+
18+
forgetest_async!(test_zk_constructor_works_in_script, |prj, cmd| {
19+
setup_deploy_prj(&mut prj);
20+
run_zk_script_test(
21+
prj.root(),
22+
&mut cmd,
23+
"./script/Constructor.s.sol",
24+
"ConstructorScript",
25+
None,
26+
3,
27+
Some(&["-vvvvv", "--broadcast"]),
28+
);
29+
});
30+
31+
fn setup_deploy_prj(prj: &mut TestProject) {
32+
util::initialize(prj.root());
33+
prj.add_script("Constructor.s.sol", include_str!("../../fixtures/zk/Constructor.s.sol"))
34+
.unwrap();
35+
prj.add_source("Bank.sol", include_str!("../../../../../testdata/zk/Bank.sol")).unwrap();
36+
}

crates/forge/tests/it/zk/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Forge tests for zkysnc functionality.
22
mod basic;
33
mod cheats;
4+
mod constructor;
45
mod contracts;
56
mod create;
67
mod create2;

testdata/cheats/Vm.sol

Lines changed: 176 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/zk/Bank.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.18;
3+
4+
contract Bank {
5+
function balance() public view returns (uint256) {
6+
return address(this).balance;
7+
}
8+
9+
constructor() payable {}
10+
11+
receive() external payable {}
12+
}

testdata/zk/Constructor.t.sol

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.18;
3+
4+
import "ds-test/test.sol";
5+
import "../cheats/Vm.sol";
6+
import "../default/logs/console.sol";
7+
import "./Bank.sol";
8+
9+
contract ZkConstructorTest is DSTest {
10+
function testZkConstructorWorksWithValue() public {
11+
Bank bank = new Bank{value: 1 ether}();
12+
assertEq(bank.balance(), 1 ether);
13+
}
14+
15+
function testZkConstructorWorksWithoutValue() public {
16+
Bank bank = new Bank();
17+
assertEq(bank.balance(), 0);
18+
}
19+
}

0 commit comments

Comments
 (0)