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: Test constructor function with and without values #749

Merged
merged 4 commits into from
Dec 2, 2024
Merged
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
25 changes: 25 additions & 0 deletions crates/forge/tests/fixtures/zk/Constructor.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;

import {Script} from "forge-std/Script.sol";
import {Bank} from "../src/Bank.sol";

contract ConstructorScript is Script {
function run() external {
vm.startBroadcast();

// Test constructor without value
Bank bankNoValue = new Bank();
assert(bankNoValue.balance() == 0);

// Test constructor with 1 ether
Bank bankWithEther = new Bank{value: 1 ether}();
assert(bankWithEther.balance() == 1 ether);

// Test constructor with smaller value
Bank bankSmallValue = new Bank{value: 0.1 ether}();
assert(bankSmallValue.balance() == 0.1 ether);

vm.stopBroadcast();
}
}
36 changes: 36 additions & 0 deletions crates/forge/tests/it/zk/constructor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Forge tests for constructor functionality with and without value.

use crate::{config::*, test_helpers::TEST_DATA_DEFAULT};
use foundry_test_utils::{forgetest_async, util, TestProject};

use crate::test_helpers::run_zk_script_test;
use forge::revm::primitives::SpecId;
use foundry_test_utils::Filter;

#[tokio::test(flavor = "multi_thread")]
async fn test_zk_constructor_works() {
let runner = TEST_DATA_DEFAULT.runner_zksync();
let filter = Filter::new("testZkConstructor", "ZkConstructorTest", ".*");

TestConfig::with_filter(runner, filter).evm_spec(SpecId::SHANGHAI).run().await;
}

forgetest_async!(test_zk_constructor_works_in_script, |prj, cmd| {
setup_deploy_prj(&mut prj);
run_zk_script_test(
prj.root(),
&mut cmd,
"./script/Constructor.s.sol",
"ConstructorScript",
None,
3,
Some(&["-vvvvv", "--broadcast"]),
);
});

fn setup_deploy_prj(prj: &mut TestProject) {
util::initialize(prj.root());
prj.add_script("Constructor.s.sol", include_str!("../../fixtures/zk/Constructor.s.sol"))
.unwrap();
prj.add_source("Bank.sol", include_str!("../../../../../testdata/zk/Bank.sol")).unwrap();
}
1 change: 1 addition & 0 deletions crates/forge/tests/it/zk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Forge tests for zkysnc functionality.
mod basic;
mod cheats;
mod constructor;
mod contracts;
mod create;
mod create2;
Expand Down
201 changes: 176 additions & 25 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions testdata/zk/Bank.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

contract Bank {
function balance() public view returns (uint256) {
return address(this).balance;
}

constructor() payable {}

receive() external payable {}
}
19 changes: 19 additions & 0 deletions testdata/zk/Constructor.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

import "ds-test/test.sol";
import "../cheats/Vm.sol";
import "../default/logs/console.sol";
import "./Bank.sol";

contract ZkConstructorTest is DSTest {
function testZkConstructorWorksWithValue() public {
Bank bank = new Bank{value: 1 ether}();
assertEq(bank.balance(), 1 ether);
}

function testZkConstructorWorksWithoutValue() public {
Bank bank = new Bank();
assertEq(bank.balance(), 0);
}
}
Loading